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,47 +1,52 @@
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 {
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 = ' '
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) {
update_board :: proc() {
new_state: [AREA]bool = ---
for x in 0 ..< WIDTH {
for y in 0 ..< HEIGHT {
@ -62,5 +67,3 @@ update_board :: proc(state: ^[AREA]bool) {
state[i] = new_state[i]
}
}