Microsoft Small Basic

Program Listing: CVS703
GraphicsWindow.Title = "Conway's Game of Life"
GraphicsWindow.Top = 0
GraphicsWindow.Left = 0
GraphicsWindow.BackgroundColor = "SteelBlue"
GraphicsWindow.BrushColor = "White"
GraphicsWindow.Height = 250
GraphicsWindow.Width = 250
GraphicsWindow.CanResize = "True"

maxI = 20
maxJ = 20
size = 10

one = "one"
started = "False"

For i = 1 To maxI
For j = 1 To maxJ
x = (i - 1) * size
y = (j - 1) * size
GraphicsWindow.FillRectangle(x, y, size, size)
cell[one][i][j] = "White"
EndFor
EndFor

GraphicsWindow.MouseDown = OnMouseDown

Sub OnMouseDown
If (started = "True") then
Program.End()
EndIf

x = GraphicsWindow.MouseX
y = GraphicsWindow.MouseY

ix = Math.Ceiling(x / size)
iy = Math.Ceiling(y / size)

If (ix > maxI) Then
Goto start
EndIf

If (iy > maxJ) Then
Goto start
EndIf

If (cell[one][ix][iy] = "White") Then
cell[one][ix][iy] = "Black"
GraphicsWindow.BrushColor = "Black"
Else
cell[one][ix][iy] = "White"
GraphicsWindow.BrushColor = "White"
EndIf

GraphicsWindow.FillRectangle((ix - 1) * size, (iy - 1) * size, size, size)
Goto skip

start:
started = "True"
StartLife()

skip:
EndSub

Sub StartLife
label:
If (one = "one") Then
one = "two"
now = "one"
next = "two"
Else
one = "one"
now = "two"
next = "one"
EndIf

For i = 1 To maxI
For j = 1 To maxJ
live = 0

x0 = i - 1
x1 = i + 1
y0 = j - 1
y1 = j + 1

cell[next][i][j] = "White"

If (x0 = 0) Then
x0 = maxI
EndIf

If (x1 > maxI) Then
x1 = 1
EndIf

If (y0 = 0) Then
y0 = maxJ
EndIf

If (y1 > maxJ) Then
y1 = 1
EndIf

If (cell[now][i][y0] = "Black") Then
live = live + 1
EndIf

If (cell[now][x1][y0] = "Black") Then
live = live + 1
EndIf

If (cell[now][x1][j] = "Black") Then
live = live + 1
EndIf

If (cell[now][x1][y1] = "Black") Then
live = live + 1
EndIf

If (cell[now][i][y1] = "Black") Then
live = live + 1
EndIf

If (cell[now][x0][y1] = "Black") Then
live = live + 1
EndIf

If (cell[now][x0][j] = "Black") Then
live = live + 1
EndIf

If (cell[now][x0][y0] = "Black") Then
live = live + 1
EndIf

If (cell[now][i][j] = "Black") Then
If (live = 2) Then
cell[next][i][j] = "Black"
EndIf

If (live = 3) Then
cell[next][i][j] = "Black"
EndIf
Else
If (live = 3) Then
cell[next][i][j] = "Black"
EndIf
EndIf

If (cell[now][i][j] = cell[next][i][j]) Then
Else
GraphicsWindow.BrushColor = cell[next][i][j]
GraphicsWindow.FillRectangle((i - 1) * size, (j - 1) * size, size, size)
EndIf

EndFor
EndFor
Goto label
EndSub