Skip to content

Commit 859c747

Browse files
author
aafent
committed
GameOfLife example
1 parent 70f3450 commit 859c747

File tree

2 files changed

+246
-0
lines changed

2 files changed

+246
-0
lines changed
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
REM Conway's Game of Life using 1-D SDATA "array"
2+
REM Grid: N x N, stored row-major in GameScreen
3+
4+
print "Simulating Conway's game of life using standard rules"
5+
REM The line below sets the grid size. One can also use INPUT to set it.
6+
let N = 16
7+
8+
print "How many generations of Game of life should I run? ";
9+
Input M
10+
11+
'SEED is a random 5 digit number or more
12+
let SEED=rnd()*34214*M
13+
14+
15+
REM =========================
16+
REM 1. INITIAL RANDOM GRID
17+
REM =========================
18+
19+
let i = 0
20+
21+
For i = 1 To N * N
22+
let gameChar = "."
23+
SEED = mod(SEED * 16807, 2147483647)
24+
let rng = mod(SEED * 11, 100)
25+
if rng < 27 Then
26+
gameChar = "@"
27+
EndIf
28+
sdata GameScreen gameChar
29+
Next i
30+
31+
REM Create NextScreen with same size (so SSET works on it)
32+
reset GameScreen
33+
foreach GameScreen
34+
sdata NextScreen [GameScreen.Item]
35+
endforeach GameScreen
36+
37+
REM =========================
38+
REM 2. LOOP OVER GENERATIONS
39+
REM =========================
40+
let gen = 0
41+
42+
For gen = 0 To M - 1
43+
44+
REM ---- Print current generation from GameScreen ----
45+
print "Generation " + str(gen)
46+
47+
reset GameScreen
48+
let GameOutput = ""
49+
let cnt = 0
50+
51+
foreach GameScreen
52+
GameOutput = GameOutput + [GameScreen.Item] + " "
53+
cnt = cnt + 1
54+
if mod(cnt, N) = 0 Then
55+
GameOutput = GameOutput + "\n"
56+
EndIf
57+
endforeach GameScreen
58+
59+
print GameOutput
60+
print "" ' blank line between generations
61+
62+
REM If this was the last generation, do not compute a next one
63+
if gen = M - 1 Then
64+
goto DoneAll
65+
EndIf
66+
67+
REM =========================
68+
REM 3. COMPUTE NEXT GENERATION (into NextScreen)
69+
REM =========================
70+
71+
REM Variables used by neighbor lookups
72+
let row = 0
73+
let col = 0
74+
let qRow = 0
75+
let qCol = 0
76+
let targetIndex = 0
77+
let idx = 0
78+
let cellChar = "."
79+
let liveNeighbors = 0
80+
let newChar = "."
81+
let curCell = "."
82+
let cellIndex = 0
83+
84+
for row = 1 to N
85+
for col = 1 to N
86+
87+
REM Get current cell state at (row, col)
88+
qRow = row
89+
qCol = col
90+
gosub GetCell
91+
curCell = cellChar
92+
93+
REM Count live neighbors
94+
liveNeighbors = 0
95+
96+
REM (row-1, col-1)
97+
qRow = row - 1
98+
qCol = col - 1
99+
gosub GetCell
100+
if cellChar = "@" Then
101+
liveNeighbors = liveNeighbors + 1
102+
EndIf
103+
104+
REM (row-1, col)
105+
qRow = row - 1
106+
qCol = col
107+
gosub GetCell
108+
if cellChar = "@" Then
109+
liveNeighbors = liveNeighbors + 1
110+
EndIf
111+
112+
REM (row-1, col+1)
113+
qRow = row - 1
114+
qCol = col + 1
115+
gosub GetCell
116+
if cellChar = "@" Then
117+
liveNeighbors = liveNeighbors + 1
118+
EndIf
119+
120+
REM (row, col-1)
121+
qRow = row
122+
qCol = col - 1
123+
gosub GetCell
124+
if cellChar = "@" Then
125+
liveNeighbors = liveNeighbors + 1
126+
EndIf
127+
128+
REM (row, col+1)
129+
qRow = row
130+
qCol = col + 1
131+
gosub GetCell
132+
if cellChar = "@" Then
133+
liveNeighbors = liveNeighbors + 1
134+
EndIf
135+
136+
REM (row+1, col-1)
137+
qRow = row + 1
138+
qCol = col - 1
139+
gosub GetCell
140+
if cellChar = "@" Then
141+
liveNeighbors = liveNeighbors + 1
142+
EndIf
143+
144+
REM (row+1, col)
145+
qRow = row + 1
146+
qCol = col
147+
gosub GetCell
148+
if cellChar = "@" Then
149+
liveNeighbors = liveNeighbors + 1
150+
EndIf
151+
152+
REM (row+1, col+1)
153+
qRow = row + 1
154+
qCol = col + 1
155+
gosub GetCell
156+
if cellChar = "@" Then
157+
liveNeighbors = liveNeighbors + 1
158+
EndIf
159+
160+
REM Apply Game of Life rules WITHOUT ELSE
161+
162+
REM default: cell will be dead
163+
newChar = "."
164+
165+
REM live cell survives only with 2 or 3 neighbors
166+
if curCell = "@" Then
167+
if liveNeighbors = 2 Then
168+
newChar = "@"
169+
EndIf
170+
if liveNeighbors = 3 Then
171+
newChar = "@"
172+
EndIf
173+
EndIf
174+
175+
REM dead cell becomes live only with exactly 3 neighbors
176+
if curCell <> "@" Then
177+
if liveNeighbors = 3 Then
178+
newChar = "@"
179+
EndIf
180+
EndIf
181+
182+
cellIndex = (row - 1) * N + col
183+
SSET NextScreen cellIndex newChar
184+
185+
next col
186+
next row
187+
188+
REM =========================
189+
REM 4. COPY NextScreen BACK INTO GameScreen
190+
REM =========================
191+
idx = 0
192+
reset NextScreen
193+
foreach NextScreen
194+
idx = idx + 1
195+
if idx <= N * N Then
196+
SSET GameScreen idx [NextScreen.Item]
197+
EndIf
198+
endforeach NextScreen
199+
200+
Next gen
201+
202+
DoneAll:
203+
HALT REM Program ends, subroutine is below
204+
205+
206+
REM ======================================================
207+
REM SUBROUTINE: GetCell(qRow, qCol) -> cellChar ("@" or ".")
208+
REM Uses GameScreen as the current generation.
209+
REM ======================================================
210+
GetCell:
211+
212+
REM Out-of-bounds = dead cell
213+
if qRow < 1 Then
214+
cellChar = "."
215+
return
216+
EndIf
217+
if qRow > N Then
218+
cellChar = "."
219+
return
220+
EndIf
221+
if qCol < 1 Then
222+
cellChar = "."
223+
return
224+
EndIf
225+
if qCol > N Then
226+
cellChar = "."
227+
return
228+
EndIf
229+
230+
REM Convert 2D → 1D index
231+
targetIndex = (qRow - 1) * N + qCol
232+
233+
idx = 0
234+
reset GameScreen
235+
foreach GameScreen
236+
idx = idx + 1
237+
if idx = targetIndex Then
238+
cellChar = [GameScreen.Item]
239+
return
240+
EndIf
241+
endforeach GameScreen
242+
243+
REM fallback (should never hit)
244+
cellChar = "."
245+
return

FAST.FBasicInterpreter/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ For **Business and Technical** documentation read the [Wiki page](https://github
99

1010
| When | Description |
1111
|------------|--------------------------------------------------------|
12+
| 2025-11-18 | DeepSeek as AI Provider |
1213
| 2025-11-17 | New Package version for Interpreter and Toolkit |
1314
| 2025-11-17 | AI Chat Library added |
1415
| 2025-11-16 | Now Arrays supporting the Lazy Initialization Pattern |

0 commit comments

Comments
 (0)