Microsoft Small Basic

Program Listing: JJT479-1
' Sudoku
' Modified by Nonki Takahashi
' Program ID JJT479-1

GraphicsWindow.Title="sudoku"

Init()
Form()
Redraw()
fc = "Black"

'Create the timer
Timer.Interval = 1000
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.KeyDown = OnKeyDown

Sub Check
If Array.GetItemCount(box) = 81 Then
complete = "True"
' vertical check
For col = 1 To 9
found = ""
For row = 1 To 9
n = (row - 1) * 9 + col
found[box[n]] = "True"
EndFor
If Array.GetItemCount(found) < 9 Then
complete = "False"
col = 9 ' exit For
EndIf
EndFor
If complete Then
' horizontal check
For row = 1 To 9
found = ""
For col = 1 To 9
n = (row - 1) * 9 + col
found[box[n]] = "True"
EndFor
If Array.GetItemCount(found) < 9 Then
complete = "False"
row = 9 ' exit For
EndIf
EndFor
EndIf
If complete Then
' block check
For row1 = 1 To 7 Step 3
For col1 = 1 To 7 Step 3
found = ""
For row = row1 To row1 + 2
For col = col1 To col1 + 2
n = (row - 1) * 9 + col
found[box[n]] = "True"
EndFor
EndFor
If Array.GetItemCount(found) < 9 Then
complete = "False"
row1 = 7 ' exit For
col1 = 7 ' exit For
EndIf
EndFor
EndFor
EndIf
If complete Then
GraphicsWindow.BrushColor = "#66000000"
GraphicsWindow.PenWidth = 0
Shapes.AddRectangle(650, 490)
GraphicsWindow.BrushColor = "#66FFFFFF"
GraphicsWindow.FontSize = 80
comp = Shapes.AddText("COMPLETED!")
Shapes.Move(comp, 80, 170)
Timer.Pause()
EndIf
EndIf
EndSub

Sub CreateGrid
'Create the Grid
GraphicsWindow.PenColor = "Black"
GraphicsWindow.PenWidth = 2
param = "x=22;y=22;size=40;nx=9;ny=9;"
DrawGrid()
GraphicsWindow.PenWidth = 6
param = "x=22;y=22;size=120;nx=3;ny=3;"
DrawGrid()
GraphicsWindow.PenWidth = 2
Endsub

Sub DrawBox
' param col, row - selected box
x = 22 + (col - 1) * 40
y = 22 + (row - 1) * 40
GraphicsWindow.BrushColor = bc
GraphicsWindow.FillRectangle(x + 3, y + 3, 34, 34)
GraphicsWindow.FontSize = 25
GraphicsWindow.BrushColor = fc
GraphicsWindow.DrawText(x + 10, y + 6, box[n])
EndSub

Sub DrawGrid
pw = Math.Floor(GraphicsWindow.PenWidth / 2)
x1 = param["x"]
dx = param["size"]
x2 = x1 + param["nx"] * dx
y1 = param["y"]
dy = param["size"]
y2 = x1 + param["ny"] * dy
For x = x1 To x2 Step dx
GraphicsWindow.DrawLine(x, y1 - pw, x, y2 + pw)
EndFor
For y = y1 To y2 Step dy
GraphicsWindow.DrawLine(x1 - pw, y, x2 + pw, y)
EndFor
If "False" And (num = "") Then
n = 1
GraphicsWindow.FontSize = 12
GraphicsWindow.BrushColor = "Blue"
For y = y1 To y2 - dy Step dy
For x = x1 To x2 - dx Step dx
num = Shapes.AddText(n)
Shapes.Move(num, x + 3, y + 3)
n = n + 1
EndFor
EndFor
EndIf
EndSub

Sub Form
GraphicsWindow.BackgroundColor = bg[1]["c"]
GraphicsWindow.Width = 640
GraphicsWindow.Height = 480
GraphicsWindow.Title = "Small Basic Sudoku"
GraphicsWindow.CanResize = "False"
GraphicsWindow.Show()
bc = "White"
GraphicsWindow.BrushColor = bc
GraphicsWindow.FillRectangle(20, 20, 360, 360)
CreateGrid()

'Section (choise the background color)
GraphicsWindow.BrushColor = "GainsBoro"
GraphicsWindow.FillRectangle(403, 51, 139, 119)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillRectangle(402, 25, 142, 25)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FontSize = 13
GraphicsWindow.DrawText(436, 30, "Background")
GraphicsWindow.PenColor = "Black"
GraphicsWindow.DrawRectangle(403, 50, 140, 120)

'Section (choise the grid color)
GraphicsWindow.BrushColor = "GainsBoro"
GraphicsWindow.FillRectangle(402, 205, 139, 119)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillRectangle(402, 180, 142, 25)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FontSize = 13
GraphicsWindow.DrawText(459, 185, "Grid")
GraphicsWindow.PenColor = "Black"
GraphicsWindow.DrawRectangle(403, 205, 140, 120)

'Background color buttons
GraphicsWindow.PenColor = "Black"
For i = 1 To 9
GraphicsWindow.BrushColor = bg[i]["c"]
GraphicsWindow.FillRectangle(bg[i]["x"], bg[i]["y"], 20, 20)
GraphicsWindow.DrawRectangle(bg[i]["x"] - 1, bg[i]["y"] - 1, 21, 21)
EndFor

'Grid color buttons
For i = 1 To 9
GraphicsWindow.BrushColor = gc[i]["c"]
GraphicsWindow.FillRectangle(gc[i]["x"], gc[i]["y"], 20, 20)
GraphicsWindow.DrawRectangle(gc[i]["x"] - 1, gc[i]["y"] - 1, 21, 21)
EndFor

'Game Timer
'Variables
'(sec, min, hour)
CountSec = 0
CountMin = 0
CountHour = 0
'Zero numbers
sZero = 0
mZero = 0
hZero = 0
'Sec positions
sSecX = 235
sSecY = 412
'Min positions
mMinX = 153
mMinY = 412
'Hour positions
hHourX = 0
hHourY = 0

'Background Timer
GraphicsWindow.FontSize = 13
GraphicsWindow.PenWidth = 4
GraphicsWindow.PenColor = "Black"
GraphicsWindow.DrawRectangle(20, 415, 264, 58)
GraphicsWindow.DrawRectangle(295, 415, 88, 58)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillRectangle(18, 390, 367, 25)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.DrawText(321,397, "Timer")
GraphicsWindow.BrushColor = "GainsBoro"
GraphicsWindow.FillRectangle(22, 417, 260, 54)
GraphicsWindow.FillRectangle(297, 417, 84, 54)
GraphicsWindow.BrushColor = "LemonChiffon"
GraphicsWindow.FillRectangle(297, 417, 42, 27)
GraphicsWindow.BrushColor = "LightSkyBlue"
GraphicsWindow.FillRectangle(339, 417, 42, 27)
GraphicsWindow.BrushColor = "Lime"
GraphicsWindow.FillRectangle(297, 444, 84, 27)
GraphicsWindow.PenColor = "black"
GraphicsWindow.DrawRectangle(297, 417, 42, 27)
GraphicsWindow.DrawRectangle(297, 444, 84, 27)
GraphicsWindow.DrawRectangle(297, 444, 84, 27)
GraphicsWindow.DrawRectangle(339, 417, 42, 27)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 13
GraphicsWindow.DrawText(325, 450, "Start")
GraphicsWindow.DrawText(311, 423, "||")
GraphicsWindow.DrawText(356, 423, ">")
EndSub

Sub GridColor
For gcLoop = 1 to 9
If (xpos > gc[gcLoop]["x"] And xpos < gc[gcLoop]["x"] + 20) Then
If (ypos > gc[gcLoop]["y"] And ypos < gc[gcLoop]["y"] + 20) Then
bc = gc[gcLoop]["c"]
GraphicsWindow.BrushColor = bc
GraphicsWindow.FillRectangle(20, 20, 360, 360)
CreateGrid()
Redraw()
endif
EndIf
endfor
Endsub

Sub Init
Not = "False=True;True=False;"

'bg (background color)
bg[1] = "c=WhiteSmoke;x=420;y=60;"
bg[2] = "c=RoyalBlue;x=465;y=60;"
bg[3] = "c=IndianRed;x=508;y=60;"
bg[4] = "c=Tomato;x=420;y=100;"
bg[5] = "c=SeaGreen;x=465;y=100;"
bg[6] = "c=Orchid;x=508;y=100;"
bg[7] = "c=Gold;x=420;y=140;"
bg[8] = "c=Gray;x=465;y=140;"
bg[9] = "c=Chocolate;x=508;y=140;"

'gc (grid color)
gc[1] = "c=White;x=420;y=215;"
gc[2] = "c=CornFlowerBlue;x=465;y=215;"
gc[3] = "c=Salmon;x=508;y=215;"
gc[4] = "c=Coral;x=420;y=255;"
gc[5] = "c=MediumSeaGreen;x=465;y=255;"
gc[6] = "c=Violet;x=508;y=255;"
gc[7] = "c=Khaki;x=420;y=295;"
gc[8] = "c=DarkGray;x=465;y=295;"
gc[9] = "c=Peru;x=508;y=295;"

' initialize array number for key check
number = ""
For i = 1 To 9
number[i] = "Numpad" + i
number[9 + i] = "D" + i
EndFor

' sudoku question
fixed = "3=4;4=9;5=1;8=8;14=3;18=9;"
fixed = fixed + "19=8;20=3;25=1;30=8;36=7;"
fixed = fixed + "38=9;39=1;40=3;44=6;45=4;"
fixed = fixed + "47=6;49=4;50=7;52=9;"
fixed = fixed + "55=9;61=5;62=7;65=7;69=1;72=6;74=1;77=5;78=9;79=3;81=8;"

' sudoku answer
'box = "3=4;4=9;5=1;8=8;14=3;18=9;19=8;20=3;25=1;30=8;36=7;38=9;39=1;40=3;44=6;45=4;47=6;49=4;50=7;52=9;55=9;61=5;62=7;65=7;69=1;72=6;74=1;77=5;78=9;79=3;81=8;1=6;2=5;6=2;7=7;9=3;10=1;11=2;12=7;13=8;15=4;16=6;17=5;21=9;22=5;23=6;24=7;26=4;27=2;28=5;29=4;31=1;32=9;33=6;34=2;35=3;37=7;41=2;42=5;43=8;46=2;48=3;51=8;53=1;54=5;56=8;57=2;58=6;59=4;60=3;63=1;64=3;66=5;67=2;68=8;70=4;71=9;73=4;75=6;76=7;"
index = Array.GetAllIndices(fixed)
n = Array.GetItemCount(fixed)
For i = 1 To n
box[index[i]] = fixed[index[i]]
EndFor
EndSub

Sub OnKeyDown
key = GraphicsWindow.LastKey
If (0 < col) And (0 < row) Then
n = (row - 1) * 9 + col
Shapes.Remove(frame)
If Array.ContainsValue(number, key) Then
len = Text.GetLength(key)
box[n] = Text.GetSubText(key, len, 1)
DrawBox()
Check()
ElseIf (key = "Delete" Or key = "Space") Then
'Delete number in box
box[n] = ""
DrawBox()
EndIf
col = -1
row = -1
EndIf
EndSub

Sub OnMouseDown
xpos = GraphicsWindow.MouseX
ypos = GraphicsWindow.MouseY
if (xpos > 420 and xpos < 528) And (ypos > 60 And ypos< 160) then
SetBackground()
endif
If (xpos > 420 And xpos < 528) And (ypos > 215 And ypos < 316) then
GridColor()
endif
If (xpos > 297 And xpos < 381) And (ypos > 417 And ypos < 471) then
StartTimer()
endif
If (22 <= xpos) And (xpos < 382) And (22 <= ypos) And (ypos < 382) then
SelectBox()
Else
Shapes.Remove(frame)
col = -1
row = -1
Endif
Endsub

Sub OnTick
CountSec = CountSec + 1
GraphicsWindow.BrushColor = "GainsBoro"
GraphicsWindow.FillRectangle(22, 417, 260, 54)
GraphicsWindow.BrushColor = "Black"

GraphicsWindow.FontSize = "50"

If (CountSec = 10) Then
sZero = ""
sSecX = 203
sSecY = 412
EndIf

If (CountMin > 9) Then
mZero = ""
mMinX = 119
mMinY = 412
EndIf

If (CountSec = 60) Then
CountMin = CountMin + 1
CountSec = 0
sZero = 0
sSecX = 235
sSecY = 412
EndIf

If (CountMin = 60) Then
CountHour = CountHour + 1
CountMin = 0
mMinX = 153
mZero = 0
EndIf

GraphicsWindow.DrawText(102, 407, ":")
GraphicsWindow.DrawText(185, 407, ":")

GraphicsWindow.DrawText(sSecX, sSecY, CountSec)
GraphicsWindow.DrawText(203, 412, sZero)

GraphicsWindow.DrawText(mMinX, mMinY, CountMin)
GraphicsWindow.DrawText(119, 412, mZero)

GraphicsWindow.DrawText(69, 412, CountHour)
GraphicsWindow.DrawText(35, 412, "0")
EndSub

Sub Redraw
index = Array.GetAllIndices(box)
_n = Array.GetItemCount(box)
For i = 1 To _n
n = index[i]
col = Math.Remainder((n - 1), 9) + 1
row = Math.Floor((n - 1) / 9) + 1
If Array.ContainsIndex(fixed, n) Then
fc = "DimGray"
Else
fc = "Black"
EndIf
DrawBox()
EndFor
fc = "Black"
EndSub

Sub SetBackground
For bgLoop = 1 to 9
If (xpos > bg[bgLoop]["x"] And xpos < bg[bgLoop]["x"] + 20) Then
If (ypos > bg[bgLoop]["y"] And ypos < bg[bgLoop]["y"] + 20) Then
GraphicsWindow.BackgroundColor = bg[bgLoop]["c"]
EndIf
EndIf
EndFor
EndSub

Sub SelectBox
' Select the box
col = Math.Floor((xpos - 22) / 40) + 1
row = Math.Floor((ypos - 22) / 40) + 1
n = (row - 1) * 9 + col
Shapes.Remove(frame)
If Array.ContainsIndex(fixed, n) Then
col = -1
row = -1
Else
x = 22 + (col - 1) * 40
y = 22 + (row - 1) * 40
GraphicsWindow.PenWidth = 2
GraphicsWindow.PenColor = "Blue"
GraphicsWindow.BrushColor = "Transparent"
frame = Shapes.AddRectangle(38, 38)
Shapes.Move(frame, x + 1, y + 1)
EndIf
EndSub

Sub StartTimer
' Start the Timer
If (xpos > 297 And xpos < (297 + 84)) Then
If (ypos > 444 And ypos < (444 + 27)) Then
Timer.Tick = OnTick
GraphicsWindow.BrushColor = "GainsBoro"
GraphicsWindow.FillRectangle(299, 446, 80, 25)
EndIf
Endif

If (xpos > 297 And xpos < (297 + 42)) Then
If (ypos > 417 And ypos < (417 + 27)) Then
Timer.Pause()
EndIf
Endif

If (xpos > 339 And xpos < (339 + 84)) Then
If (ypos > 417 And ypos < (417 + 27)) Then
Timer.Resume()
EndIf
EndIf
EndSub

Sub Dummy
OnTick = ""
EndSub