diff --git a/cells.go b/cells.go new file mode 100644 index 0000000..a6e29a6 --- /dev/null +++ b/cells.go @@ -0,0 +1,90 @@ +package main + +type Cell struct { + X int + Y int + State int +} + +func (c *Cell) Display() rune { + if c.State == 1 { + return '\u2588' + } else { + return ' ' + } +} + +func (c *Cell) Update(g *Game) int{ + h, w := g.Screen.Size() + cs := g.Cells + n := 0 + if c.X == 0 { + if c.Y == 0 { + n = cs[c.X][c.Y+1].State + + cs[c.X+1][c.Y].State + + cs[c.X+1][c.Y+1].State + } else if c.Y < w-1 { + n = cs[c.X][c.Y+1].State + + cs[c.X+1][c.Y].State + + cs[c.X+1][c.Y+1].State + + cs[c.X][c.Y-1].State + + cs[c.X+1][c.Y-1].State + } else { + n = cs[c.X+1][c.Y].State + + cs[c.X][c.Y-1].State + + cs[c.X+1][c.Y-1].State + } + } else if c.X == h-1 { + if c.Y == 0 { + n = cs[c.X][c.Y+1].State + + cs[c.X-1][c.Y].State + + cs[c.X-1][c.Y+1].State + } else if c.Y < w-1 { + n = cs[c.X][c.Y+1].State + + cs[c.X-1][c.Y].State + + cs[c.X-1][c.Y+1].State + + cs[c.X][c.Y-1].State + + cs[c.X-1][c.Y-1].State + } else { + n = cs[c.X-1][c.Y].State + + cs[c.X][c.Y-1].State + + cs[c.X-1][c.Y-1].State + } + } else if c.Y == 0 { + n = cs[c.X-1][c.Y].State + + cs[c.X-1][c.Y+1].State + + cs[c.X][c.Y+1].State + + cs[c.X+1][c.Y+1].State + + cs[c.X+1][c.Y].State + } else if c.Y == w-1 { + n = cs[c.X-1][c.Y].State + + cs[c.X-1][c.Y-1].State + + cs[c.X][c.Y-1].State + + cs[c.X+1][c.Y-1].State + + cs[c.X+1][c.Y].State + } else { + n = cs[c.X-1][c.Y-1].State + + cs[c.X-1][c.Y].State + + cs[c.X-1][c.Y+1].State + + cs[c.X][c.Y-1].State + + cs[c.X][c.Y+1].State + + cs[c.X+1][c.Y-1].State + + cs[c.X+1][c.Y].State + + cs[c.X+1][c.Y+1].State + } + + if c.State == 1 { + if n >= 2 && n < 4 { + return 1 + } else { + return 0 + } + } else if c.State == 0 { + if n == 3 { + return 1 + } + return 0 + } else { + return 0 + } +} diff --git a/game.go b/game.go index 0c7669e..d7d7f71 100644 --- a/game.go +++ b/game.go @@ -1,5 +1,146 @@ package main -func GetText() string { - return "My text" +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