it works
This commit is contained in:
parent
4c305006a8
commit
b7e9bd1d58
90
cells.go
Normal file
90
cells.go
Normal 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
145
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<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
96
main.go
|
@ -1,44 +1,68 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math/rand/v2"
|
||||
"os"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := tview.NewApplication()
|
||||
|
||||
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||
if event.Rune() == 'q' {
|
||||
app.Stop()
|
||||
}
|
||||
return event
|
||||
})
|
||||
|
||||
shortcuts := tview.NewList().
|
||||
AddItem("Run", "Press to run", 'r', nil).
|
||||
AddItem("New", "Press for new init", 'n', nil).
|
||||
AddItem("Quit", "Press to exit", 'q', func() {
|
||||
app.Stop()
|
||||
}).
|
||||
SetSelectedFocusOnly(true)
|
||||
|
||||
main := tview.NewTextView().
|
||||
SetTextAlign(tview.AlignCenter).
|
||||
SetText(GetText())
|
||||
|
||||
grid := tview.NewGrid().
|
||||
SetRows(0).
|
||||
SetColumns(0, 30).
|
||||
AddItem(main, 0, 0, 1, 1, 0, 0, false).
|
||||
AddItem(shortcuts, 0, 1, 1, 1, 0, 0, true)
|
||||
|
||||
grid.Box = tview.NewBox().
|
||||
SetTitle("Game of Life").
|
||||
SetBorder(true).
|
||||
SetBorderColor(tcell.ColorPurple)
|
||||
|
||||
if err := app.SetRoot(grid, true).EnableMouse(true).Run(); err != nil {
|
||||
panic(err)
|
||||
func randNum() int {
|
||||
n := rand.IntN(100)
|
||||
if n < 85 {
|
||||
return 0
|
||||
} else {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 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
|
||||
|
||||
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)
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user