This commit is contained in:
Xaloc 2024-05-24 23:34:56 +02:00
parent 4c305006a8
commit b7e9bd1d58
3 changed files with 293 additions and 38 deletions

90
cells.go Normal file
View File

@ -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
}
}

145
game.go
View File

@ -1,5 +1,146 @@
package main package main
func GetText() string { import (
return "My text" "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
} }

96
main.go
View File

@ -1,44 +1,68 @@
package main package main
import ( import (
"math/rand/v2"
"os"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
) )
func main() { func randNum() int {
app := tview.NewApplication() n := rand.IntN(100)
if n < 85 {
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { return 0
if event.Rune() == 'q' { } else {
app.Stop() return 1
} }
return event }
})
func main() {
shortcuts := tview.NewList(). // create new screen
AddItem("Run", "Press to run", 'r', nil). s, err := tcell.NewScreen()
AddItem("New", "Press for new init", 'n', nil). if err != nil {
AddItem("Quit", "Press to exit", 'q', func() { panic(err)
app.Stop() }
}).
SetSelectedFocusOnly(true) // initialise screen
if err := s.Init(); err != nil {
main := tview.NewTextView(). panic(err)
SetTextAlign(tview.AlignCenter). }
SetText(GetText())
// polling to be able to quit
grid := tview.NewGrid().
SetRows(0). style := tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorPurple)
SetColumns(0, 30). s.SetStyle(style)
AddItem(main, 0, 0, 1, 1, 0, 0, false). h, w := s.Size()
AddItem(shortcuts, 0, 1, 1, 1, 0, 0, true)
var cells [][]Cell
grid.Box = tview.NewBox(). for r := 0; r<h; r++ {
SetTitle("Game of Life"). var row []Cell
SetBorder(true). for c := 0; c<w; c++ {
SetBorderColor(tcell.ColorPurple) newCell := Cell{r, c, randNum()}
row = append(row, newCell)
if err := app.SetRoot(grid, true).EnableMouse(true).Run(); err != nil { }
panic(err) 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
} }
} }