global state, actual prandom and formatting

This commit is contained in:
Crispy 2024-05-03 11:06:27 +02:00
parent a784f7bb98
commit 38f56a1099

View file

@ -1,54 +1,59 @@
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() {
board : [AREA]bool = ---
fmt.print("\e[2J") // clear screen
for i in 0 ..< AREA do state[i] = bool(rand.uint32() & 1)
for {
print_board(&board)
update_board(&board)
print_board()
update_board()
time.sleep(time.Millisecond * 50)
}
}
print_board :: proc(state: ^[AREA]bool) {
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 {
for x in 0 ..< WIDTH {
top := state[x + y * WIDTH]
bot := state[x + (y+1) * WIDTH]
c : rune
bot := state[x + (y + 1) * WIDTH]
c: rune
switch {
case top && bot: c = '█'
case top && !bot: c = '▀'
case !top && bot: c = '▄'
case: c = ' '
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 {
update_board :: proc() {
new_state: [AREA]bool = ---
for x in 0 ..< WIDTH {
for y in 0 ..< HEIGHT {
count := 0
for dx in -1..=1 {
for dx in -1 ..= 1 {
px := (x + dx + WIDTH) % WIDTH
for dy in -1..=1 {
for dy in -1 ..= 1 {
py := (y + dy + HEIGHT) % HEIGHT
if state[px + py * WIDTH] do count += 1
}
@ -58,9 +63,7 @@ update_board :: proc(state: ^[AREA]bool) {
new_state[x + y * WIDTH] = new_cell
}
}
for i in 0..<AREA {
for i in 0 ..< AREA {
state[i] = new_state[i]
}
}