Microsoft Small Basic

Program Listing: GZN668-0
' BATTLESHIP in Small Basic
' written by Nonki Takahashi
' Last update 2014-03-25
' Version 0.2a
' Prigram ID GZN668-0
'
GraphicsWindow.Title = "BATTLESHIP in Small Basic 0.2a"
bg = "Gray"
GraphicsWindow.BackgroundColor = bg
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
bc = "Turquoise"
GraphicsWindow.PenColor = bg
GraphicsWindow.FontName = "Trebuchet MS"
fc = "White"
fs = 20
GraphicsWindow.FontSize = fs
size = 28
y0 = Math.Floor((gh - size * 11) / 2) + size
x0 = Math.Floor((gw - size * 21) / 2)
DrawBoard()
radar = Shapes.AddImage("http://www.nonkit.com/smallbasic.files/Radar.PNG")
Shapes.SetOpacity(radar, 50)
Shapes.Move(radar, x0, y0)
bc = "Blue"
x0 = Math.Floor((gw - size * 21) / 2) + size * 11
DrawBoard()
InitShip()
DrawShip()
angleRadar = 0
Timer.Interval = 100
Timer.Tick = OnTick
mouseDown = "False"
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseUp = OnMouseUp
While "True"
PrepareForBattle()
EndWhile
Sub PrepareForBattle
If mouseDown Then
dx = GraphicsWindow.MouseX
dy = GraphicsWindow.MouseY
col = Math.Floor((dx - x0) / size) + 1
row = Math.Floor((dy - y0) / size) + 1
If -10 <= col And col <= 10 And -10 <= row And row <= 10 Then
grid = 0 ' ocean grid
HitShip()
If iShip < 6 Then
color = "Red"
Else
color = "White"
EndIf
DrawPeg()
dragged = "False"
mouseMove = "False
While mouseDown
If mouseMove Then
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
mouseMove = "False"
Else
Program.Delay(300)
EndIf
EndWhile
EndIF
Else
Program.Delay(300)
EndIf
EndSub
Sub DrawBoard
GraphicsWindow.BrushColor = fc
y = y0 - fs * 1.2
For i = 1 To 10
If i = 9 Then
x = x0 + (i - 1) * size + 10
Else
x = x0 + (i - 1) * size + 7
EndIf
GraphicsWindow.DrawText(x, y, Text.GetSubText("ABCDEFGHIJ", i, 1))
EndFor
For j = 1 To 10
y = y0 + (j - 1) * size
If gw / 2 < x0 Then
GraphicsWindow.BrushColor = fc
If j = 10 Then
x = x0 - fs * 1.3
Else
x = x0 - fs
EndIF
GraphicsWindow.DrawText(x, y, j)
EndIf
GraphicsWindow.BrushColor = bc
For i = 1 To 10
x = x0 + (i - 1) * size
GraphicsWindow.FillRectangle(x, y, size, size)
GraphicsWindow.DrawRectangle(x, y, size, size)
EndFor
EndFor
EndSub
Sub DrawPeg
' param col, row
' param color
margin = 8
GraphicsWindow.BrushColor = color
peg = Shapes.AddEllipse(size - margin * 2, size - margin * 2)
x = x0 + (col - 1) * size + margin
y = y0 + (row - 1) * size + margin
Shapes.Move(peg, x, y)
EndSub
Sub DrawShip
For iShip = 1 To 5
shi = ship[iShip]
col = Text.GetIndexOf("ABCDEFGHIJ", Text.GetSubText(shi["pos"], 1, 1))
row = Text.GetSubTextToEnd(shi["pos"], 2)
x = x0 + (col - 1) * size
y = y0 + (row - 1) * size
For iShape = shi["iMin"] To shi["iMax"]
sha = shape[iShape]
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "DimGray"
If sha["func"] = "rect" Then
sha["obj"] = Shapes.AddRectangle(sha["width"], sha["height"])
ElseIf sha["func"] = "ell" Then
sha["obj"] = Shapes.AddEllipse(sha["width"], sha["height"])
EndIf
If shi["dir"] = "E" Then
Shapes.Move(sha["obj"], x + sha["x"], y + sha["y"])
ElseIf shi["dir"] = "S" Then
ox = x + sha["y"] + sha["height"] / 2
oy = y + sha["x"] + sha["width"] / 2
Shapes.Move(sha["obj"], ox - sha["width"] / 2, oy - sha["height"] / 2)
Shapes.Rotate(sha["obj"], 90)
EndIf
EndFor
EndFor
EndSub
Sub HitShip
' param col, row - co-ordinate mouse clicked
' param grid - 0 for ocean grid (player), 1 for target grid (CPU)
' return iShip - 1..5 if hit, 6 if miss
For iShip = (grid * 5) + 1 To (grid * 5) + 5
shi = ship[iShip]
_col = Text.GetIndexOf("ABCDEFGHIJ", Text.GetSubText(shi["pos"], 1, 1))
_row = Text.GetSubTextToEnd(shi["pos"], 2)
len = shi["length"]
For i = 1 To len
If _col = col And _row = row Then
Goto found
EndIf
_col = _col + dirx[shi["dir"]]
_row = _row + diry[shi["dir"]]
EndFor
EndFor
found:
EndSub
Sub InitShip
ship[1] = "id=Carrier;length=5;iMin=1;iMax=1;pos=E1;dir=S;"
ship[2] = "id=Battleship;length=4;iMin=2;iMax=4;pos=A2;dir=E;"
ship[3] = "id=Destroyer;length=3;iMin=5;iMax=7;pos=A3;dir=E;"
ship[4] = "id=Submarine;length=3;iMin=8;iMax=10;pos=G4;dir=S;"
ship[5] = "id=Patrol boat;length=2;iMin=11;iMax=13;pos=A5;dir=S;"
dirx = "E=1;S=0;W=-1;N=0;"
diry = "E=0;S=1;W=0;N=-1;"
shape[1] = "func=rect;x=2;y=2;width=136;height=24;"
shape[2] = "func=ell;x=2;y=2;width=24;height=24;"
shape[3] = "func=rect;x=14;y=2;width=70;height=24;"
shape[4] = "func=ell;x=58;y=2;width=52;height=24;"
shape[5] = "func=ell;x=2;y=2;width=24;height=24;"
shape[6] = "func=rect;x=14;y=2;width=42;height=24;"
shape[7] = "func=ell;x=30;y=2;width=52;height=24;"
shape[8] = "func=ell;x=2;y=4;width=24;height=20;"
shape[9] = "func=rect;x=14;y=4;width=56;height=20;"
shape[10] = "func=ell;x=58;y=4;width=24;height=20;"
shape[11] = "func=ell;x=2;y=2;width=24;height=24;"
shape[12] = "func=rect;x=14;y=2;width=14;height=24;"
shape[13] = "func=ell;x=2;y=2;width=52;height=24;"
EndSub
Sub OnMouseDown
mouseDown = "True"
EndSub
Sub OnMouseUp
mouseDown = "False"
EndSub
Sub OnTick
angleRadar = angleRadar + 5
If 360 <= angleRadar Then
angleRadar = 0
EndIf
Shapes.Rotate(radar, angleRadar)
EndSub