implement game of life
This commit is contained in:
commit
e706ba4767
2 changed files with 86 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.exe
|
85
main.odin
Normal file
85
main.odin
Normal file
|
@ -0,0 +1,85 @@
|
|||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "core:strings"
|
||||
|
||||
WIDTH :: 32
|
||||
HEIGHT :: 32
|
||||
AREA :: WIDTH * HEIGHT
|
||||
|
||||
main :: proc() {
|
||||
fmt.println("hellope")
|
||||
board : [AREA]bool = ---
|
||||
|
||||
// board[0] = true
|
||||
// board[1] = true
|
||||
// board[WIDTH] = true
|
||||
// for x in 4..<14 {
|
||||
// board[x + WIDTH*8] = true
|
||||
// }
|
||||
for {
|
||||
print_board(&board)
|
||||
bytes := 0
|
||||
err : os.Errno
|
||||
buf : [3]byte
|
||||
for {
|
||||
bytes, err = os.read(os.stdin, buf[:])
|
||||
if bytes > 0 do break
|
||||
}
|
||||
update_board(&board)
|
||||
}
|
||||
}
|
||||
|
||||
print_board :: proc(state: ^[AREA]bool){
|
||||
fmt.println(strings.repeat("-", WIDTH))
|
||||
for y := 0; y < HEIGHT; y += 2{
|
||||
for x in 0..<WIDTH{
|
||||
top := state[x + y * WIDTH]
|
||||
bot := state[x + (y+1) * WIDTH ]
|
||||
if top {
|
||||
if bot{
|
||||
fmt.print("█")
|
||||
}else{
|
||||
fmt.print("▀")
|
||||
}
|
||||
}else{
|
||||
if bot {
|
||||
fmt.print("▄")
|
||||
}else{
|
||||
fmt.print(" ")
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.println()
|
||||
}
|
||||
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]{
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
cell := state[x + y * WIDTH];
|
||||
new_cell := (count == 3 && !cell) || (cell && count < 5 && count > 2)
|
||||
// fmt.println("count %v, state %v", count, new_cell)
|
||||
new_state[x + y * WIDTH] = new_cell
|
||||
}
|
||||
}
|
||||
for i in 0..<AREA {
|
||||
state[i] = new_state[i]
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in a new issue