Microsoft Small Basic

Program Listing: LKX813
' Draughts
' Version 0.1
' Copyright © 2016 Nonki Takahashi. The MIT License.
'
' History :
' 0.1 2016-11-28 Simple version created.
'
GraphicsWindow.Title = "Draughts 0.1"
GraphicsWindow.BackgroundColor = "#003300"
InitBoard()
InitChessmen()
picked = ""
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.MouseUp = OnMouseUp
Sub InitBoard
pos = "x0=120;y0=30;" ' left, top
board[8] = "b=B;d=B;f=B;h=B;"
board[7] = "a=B;c=B;e=B;g=B;"
board[6] = "b=B;d=B;f=B;h=B;"
board[3] = "a=W;c=W;e=W;g=W;"
board[2] = "b=W;d=W;f=W;h=W;"
board[1] = "a=W;c=W;e=W;g=W;"
color = "W=white;B=#330000;0=SaddleBrown;1=BurlyWood;"
size = 48 ' font height and square size
For j = 8 To 1 Step - 1
y = pos["y0"] + (8 - j) * size
For i = 1 To 8
x = pos["x0"] + (i - 1) * size
GraphicsWindow.BrushColor = color[Math.Remainder((i + j), 2)]
GraphicsWindow.FillRectangle(x, y, size, size)
EndFor
EndFor
EndSub
Sub InitChessmen
' param size - font height and square size
' return nMen - number of chessmen
' return index - index of chessmen
GraphicsWindow.PenWidth = 0
For j = 8 To 1 Step - 1
y = pos["y0"] + (8 - j) * size
For i = 1 To 8
x = pos["x0"] + (i - 1) * size
p = board[j][Text.GetSubText("abcdefgh", i, 1)]
If p <> "" Then
GraphicsWindow.BrushColor = color[Text.GetSubText(p, 1, 1)]
obj = Shapes.AddEllipse(size * 0.8, size * 0.8)
men[obj]["x"] = x
men[obj]["y"] = y
Shapes.Move(obj, men[obj]["x"] + size * 0.1, men[obj]["y"] + size * 0.1)
EndIF
EndFor
EndFor
nMen = Array.GetItemCount(men)
index = Array.GetAllIndices(men)
EndSub
Sub OnMouseDown
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
For i = 1 To nMen
obj = index[i]
x = men[obj]["x"]
y = men[obj]["y"]
If x <= mx And mx <= x + size And y <= my And my <= y + size Then
picked = obj
dx = mx - x
dy = my - y
i = nMen + 1 ' break
EndIf
EndFor
EndSub
Sub OnMouseMove
If picked <> "" Then
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
x = mx - dx
y = my - dy
men[picked]["x"] = x
men[picked]["y"] = y
Shapes.Move(picked, x, y)
EndIf
EndSub
Sub OnMouseUp
picked = ""
EndSub