odin-gol/main.odin

69 lines
1.4 KiB
Odin

package main
import "core:fmt"
import "core:math/rand"
import "core:strings"
import "core:time"
WIDTH :: 64
HEIGHT :: 64
AREA :: WIDTH * HEIGHT
state: [AREA]bool
main :: proc() {
fmt.print("\e[2J") // clear screen
for i in 0 ..< AREA do state[i] = bool(rand.uint32() & 1)
for {
print_board()
update_board()
time.sleep(time.Millisecond * 50)
}
}
print_board :: proc() {
fmt.print("\e[u") // reset cursor
for y := 0; y < HEIGHT; y += 2 {
line := strings.builder_make()
defer strings.builder_destroy(&line)
for x in 0 ..< WIDTH {
top := state[x + y * WIDTH]
bot := state[x + (y + 1) * WIDTH]
c: rune
switch {
case top && bot:
c = '█'
case top && !bot:
c = '▀'
case !top && bot:
c = '▄'
case:
c = ' '
}
strings.write_rune(&line, c)
}
fmt.println(strings.to_string(line))
}
}
update_board :: proc() {
new_state: [AREA]bool = ---
for x in 0 ..< WIDTH {
for y in 0 ..< HEIGHT {
count := 0
for dx in -1 ..= 1 {
px := (x + dx + WIDTH) % WIDTH
for dy in -1 ..= 1 {
py := (y + dy + HEIGHT) % HEIGHT
if state[px + py * WIDTH] do count += 1
}
}
cell := state[x + y * WIDTH]
new_cell := (count == 3 && !cell) || (cell && count < 5 && count > 2)
new_state[x + y * WIDTH] = new_cell
}
}
for i in 0 ..< AREA {
state[i] = new_state[i]
}
}