odin-gol/main.odin
2024-05-02 14:10:19 +02:00

66 lines
1.4 KiB
Odin

package main
import "core:fmt"
import "core:strings"
import "core:time"
WIDTH :: 64
HEIGHT :: 64
AREA :: WIDTH * HEIGHT
main :: proc() {
board : [AREA]bool = ---
fmt.print("\e[2J") // clear screen
for {
print_board(&board)
update_board(&board)
time.sleep(time.Millisecond * 50)
}
}
print_board :: proc(state: ^[AREA]bool) {
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))
}
fmt.println()
}
update_board :: proc(state: ^[AREA]bool) {
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]
}
}