Created
June 4, 2021 22:46
-
-
Save TheRealMichaelWang/efedfa1034a4b132d9588f766fd5540e to your computer and use it in GitHub Desktop.
Conway's Game of Life, in FastCode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rem Conways Game of Life | |
rem Rules: | |
rem 1. Any live cell with fewer than two live neighbours dies, as if by underpopulation. | |
rem 2. Any live cell with two or three live neighbours lives on to the next generation. | |
rem 3. Any live cell with more than three live neighbours dies, as if by overpopulation. | |
rem 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. | |
rem sets the console title | |
system("title Conways Game of Life!") | |
const rows = 70 | |
const cols = 200 | |
const alive = 1 rem Constants cannot be applied to constants | |
const dead = 0 | |
rem cell related procedures, variables go here. Since this code-segment is "grouped", any function, variable or structure prototype declarations won't be directly accessible outside. | |
group cells | |
rem initialize cells and set them to dead | |
proc init() { | |
new_cells = array(rows) | |
for row in range(rows) { | |
new_cells[row] = array(cols) | |
for col in new_cells[row] => | |
col = dead | |
} | |
return new_cells | |
} | |
rem prints the cells | |
proc draw() { | |
for row in cells { | |
for col in row => | |
if col == alive => | |
print('*') | |
else => | |
print(' ') | |
printl() | |
} | |
} | |
rem produces a deep copy of a cell-matrix | |
proc clone(to_clone) { | |
cloned = init() | |
for row in range(rows) => | |
for col in range(cols) => | |
cloned[row][col] = to_clone[row][col] | |
return cloned | |
} | |
rem counts how many alive neigbors there are | |
proc neighboors(row, col) { | |
delta = [-1, 0, 1] | |
count = 0 | |
for dx in delta => | |
for dy in delta => | |
if !(dx == 0 and dy == 0) and | |
row + dy >= 0 and row + dy < rows and col + dx >= 0 and col + dx < cols => | |
if cells[row + dy][col + dx] == alive => | |
count++ | |
return count | |
} | |
endgroup | |
rem initialize cell-matrix | |
static cells = init@cells() | |
rem put hard-coded initial state here. | |
group fill_cells | |
for row in range(rows) => | |
for col in range(cols) => | |
if random([0,1]) => | |
cells[row][col] = alive | |
endgroup | |
rem evolves cells by one epoch | |
proc evolve() { | |
new_state = clone@cells(cells) | |
for row in range(rows) => | |
for col in range(cols) { | |
neighboors = neighboors@cells(row, col); | |
if cells[row][col] == alive { | |
if neighboors < 2 or neighboors > 3 => rem checks rule 1 & 2 | |
new_state[row][col] = dead | |
} | |
elif neighboors == 3 => rem checks rule 4 | |
new_state[row][col] = alive | |
} | |
cells = new_state | |
} | |
epoch = 0 | |
while true { | |
system("cls") | |
printl(epoch++, ':') | |
draw@cells() | |
evolve() | |
system("timeout 1") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment