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