GameOfLife/main.go

69 lines
1.0 KiB
Go
Raw Normal View History

2024-05-22 12:24:01 +01:00
package main
import (
2024-05-24 22:34:56 +01:00
"math/rand/v2"
"os"
2024-05-22 12:24:01 +01:00
"github.com/gdamore/tcell/v2"
)
2024-05-24 22:34:56 +01:00
func randNum() int {
n := rand.IntN(100)
if n < 85 {
return 0
} else {
return 1
}
}
2024-05-22 12:24:01 +01:00
func main() {
2024-05-24 22:34:56 +01:00
// create new screen
s, err := tcell.NewScreen()
if err != nil {
panic(err)
}
// initialise screen
if err := s.Init(); err != nil {
panic(err)
}
// polling to be able to quit
2024-05-22 12:24:01 +01:00
2024-05-24 22:34:56 +01:00
style := tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorPurple)
s.SetStyle(style)
h, w := s.Size()
var cells [][]Cell
for r := 0; r<h; r++ {
var row []Cell
for c := 0; c<w; c++ {
newCell := Cell{r, c, randNum()}
row = append(row, newCell)
2024-05-22 12:24:01 +01:00
}
2024-05-24 22:34:56 +01:00
cells = append(cells, row)
}
game := Game{
Screen: s,
Cells: cells,
}
println("about to start")
// game = game.Glider()
for {
newGame := game.Run()
switch event := s.PollEvent().(type) {
case *tcell.EventResize:
s.Sync()
case *tcell.EventKey:
if event.Rune() == 'q' || event.Key() == tcell.KeyCtrlC {
s.Fini()
os.Exit(0)
}
}
game = newGame
2024-05-22 12:24:01 +01:00
}
}