GameOfLife/game.go

147 lines
2.6 KiB
Go
Raw Permalink Normal View History

2024-05-22 12:24:01 +01:00
package main
2024-05-24 22:34:56 +01:00
import (
"time"
"github.com/gdamore/tcell/v2"
)
type Game struct {
Screen tcell.Screen
Cells [][]Cell
}
func (g *Game) Run() Game{
s := g.Screen
style := tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorPurple)
s.SetStyle(style)
state := g.Cells
var newState [][]Cell
for r, h := range state {
var row []Cell
for c, _ := range h {
cellState := state[r][c].Update(g)
cellX := r
cellY := c
newCell := Cell{
X: cellX,
Y: cellY,
State: cellState,
}
row = append(row, newCell)
s.SetContent(state[r][c].X, state[r][c].Y, state[r][c].Display(), nil, style)
}
newState = append(newState, row)
}
newGame := Game {
Screen: s,
Cells: newState,
}
s.Show()
time.Sleep(1*time.Millisecond)
s.Clear()
return newGame
}
func (g *Game) Glider() Game{
s := g.Screen
h, w := s.Size()
var states [][]int
for r := 0; r<h; r++ {
var row []int
for c := 0; c<w; c++ {
n := 0
row = append(row, n)
}
states = append(states, row)
}
states[111][26] = 1
states[112][27] = 1
states[113][27] = 1
states[113][26] = 1
states[113][25] = 1
var cells [][]Cell
for r := 0; r<h; r++ {
var row []Cell
for c := 0; c<w; c++ {
newCell := Cell{r, c, states[r][c]}
row = append(row, newCell)
}
cells = append(cells, row)
}
game := Game{
Screen: s,
Cells: cells,
}
return game
}
func (g *Game) GliderGun() Game{
s := g.Screen
h, w := s.Size()
var states [][]int
for r := 0; r<h; r++ {
var row []int
for c := 0; c<w; c++ {
n := 0
row = append(row, n)
}
states = append(states, row)
}
states[111][26] = 1
states[112][27] = 1
states[113][26] = 1
states[113][27] = 1
states[122][26] = 1
states[122][27] = 1
states[122][28] = 1
states[123][25] = 1
states[123][29] = 1
states[124][24] = 1
states[124][30] = 1
states[125][24] = 1
states[125][30] = 1
states[126][27] = 1
states[127][25] = 1
states[127][29] = 1
states[128][26] = 1
states[128][27] = 1
states[128][28] = 1
states[129][27] = 1
states[131][24] = 1
states[131][25] = 1
states[131][26] = 1
states[132][24] = 1
states[132][25] = 1
states[132][26] = 1
states[133][23] = 1
states[133][27] = 1
states[135][22] = 1
states[135][23] = 1
states[135][27] = 1
states[135][28] = 1
states[145][24] = 1
states[145][25] = 1
states[146][24] = 1
states[146][25] = 1
var cells [][]Cell
for r := 0; r<h; r++ {
var row []Cell
for c := 0; c<w; c++ {
newCell := Cell{r, c, states[r][c]}
row = append(row, newCell)
}
cells = append(cells, row)
}
game := Game{
Screen: s,
Cells: cells,
}
return game
2024-05-22 12:24:01 +01:00
}