add game of life
This commit is contained in:
commit
4fe6463fc4
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
gameoflige-go
|
||||
gameoflige-go
|
12
go.mod
Normal file
12
go.mod
Normal file
|
@ -0,0 +1,12 @@
|
|||
module gitnet.fr/deblan/gameoflife-go
|
||||
|
||||
go 1.21.0
|
||||
|
||||
require github.com/tfriedel6/canvas v0.12.1
|
||||
|
||||
require (
|
||||
github.com/go-gl/gl v0.0.0-20181026044259-55b76b7df9d2 // indirect
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/veandco/go-sdl2 v0.4.0 // indirect
|
||||
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 // indirect
|
||||
)
|
15
go.sum
Normal file
15
go.sum
Normal file
|
@ -0,0 +1,15 @@
|
|||
github.com/go-gl/gl v0.0.0-20181026044259-55b76b7df9d2 h1:78Hza2KHn2PX1jdydQnffaU2A/xM0g3Nx1xmMdep9Gk=
|
||||
github.com/go-gl/gl v0.0.0-20181026044259-55b76b7df9d2/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||
github.com/tfriedel6/canvas v0.12.1 h1:Oc4gww+cOtix69IaYo8TmRbwpbTl6D1jza2mBM4ZOPo=
|
||||
github.com/tfriedel6/canvas v0.12.1/go.mod h1:WIe1YgsQiKA1awmU6tSs8e5DkceDHC5MHgV5vQQZr/0=
|
||||
github.com/veandco/go-sdl2 v0.4.0 h1:l9q6K+Dvpd/VlZdw2ufApKnWhAQqx9UL8Zrvbjtm3Lw=
|
||||
github.com/veandco/go-sdl2 v0.4.0/go.mod h1:FB+kTpX9YTE+urhYiClnRzpOXbiWgaU3+5F2AB78DPg=
|
||||
golang.org/x/exp v0.0.0-20181106170214-d68db9428509/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 h1:5h3ngYt7+vXCDZCup/HkCQgW5XwmSvR/nA2JmJ0RErg=
|
||||
golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/mobile v0.0.0-20181026062114-a27dd33d354d/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
|
||||
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
109
main.go
Normal file
109
main.go
Normal file
|
@ -0,0 +1,109 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
||||
"github.com/tfriedel6/canvas/sdlcanvas"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
squareSize int
|
||||
sizeX int
|
||||
sizeY int
|
||||
gameWidth int
|
||||
gameHeight int
|
||||
)
|
||||
|
||||
func initTable(sizeX, sizeY int) map[int]map[int]int {
|
||||
table := make(map[int]map[int]int)
|
||||
|
||||
for x := 0; x < sizeX; x++ {
|
||||
table[x] = make(map[int]int)
|
||||
|
||||
for y := 0; y < sizeY; y++ {
|
||||
table[x][y] = rand.Intn(2)
|
||||
}
|
||||
}
|
||||
|
||||
return table
|
||||
}
|
||||
|
||||
func updateTable(table map[int]map[int]int) map[int]map[int]int {
|
||||
sum := 0
|
||||
newTable := make(map[int]map[int]int)
|
||||
|
||||
for x := 0; x < sizeX; x++ {
|
||||
newTable[x] = make(map[int]int)
|
||||
|
||||
for y := 0; y < sizeY; y++ {
|
||||
sum = getSum(table, x, y)
|
||||
|
||||
if sum == 3 {
|
||||
newTable[x][y] = 1
|
||||
} else if sum > 3 || sum < 2 {
|
||||
newTable[x][y] = 0
|
||||
} else {
|
||||
newTable[x][y] = table[x][y]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newTable
|
||||
}
|
||||
|
||||
func getSum(table map[int]map[int]int, x, y int) int {
|
||||
sum := 0
|
||||
|
||||
sum += table[(sizeX+x-1)%sizeX][(sizeY+y-1)%sizeY]
|
||||
sum += table[x][(sizeY+y-1)%sizeY]
|
||||
sum += table[(sizeX+x+1)%sizeX][(sizeY+y-1)%sizeY]
|
||||
sum += table[(sizeX+x-1)%sizeX][y]
|
||||
sum += table[(sizeX+x+1)%sizeX][y]
|
||||
sum += table[(sizeX+x-1)%sizeX][(sizeY+y+1)%sizeY]
|
||||
sum += table[x][(sizeY+y+1)%sizeY]
|
||||
sum += table[(sizeX+x+1)%sizeX][(sizeY+y+1)%sizeY]
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
func main() {
|
||||
squareSize, _ = strconv.Atoi(os.Args[1])
|
||||
sizeX, _ = strconv.Atoi(os.Args[2])
|
||||
sizeY, _ = strconv.Atoi(os.Args[3])
|
||||
gameWidth = squareSize * sizeX
|
||||
gameHeight = squareSize * sizeY
|
||||
|
||||
table := initTable(sizeX, sizeY)
|
||||
|
||||
wnd, cv, err := sdlcanvas.CreateWindow(gameWidth, gameHeight, "Game of life")
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer wnd.Destroy()
|
||||
w, h := float64(cv.Width()), float64(cv.Height())
|
||||
|
||||
wnd.MainLoop(func() {
|
||||
cv.SetFillStyle("#3f4b7e")
|
||||
cv.FillRect(0, 0, w, h)
|
||||
|
||||
for x := 0; x < sizeX; x++ {
|
||||
for y := 0; y < sizeY; y++ {
|
||||
if table[x][y] == 1 {
|
||||
cv.SetFillStyle(255, 255, 255)
|
||||
cv.BeginPath()
|
||||
// cv.Rect(float64(x*squareSize), float64(y*squareSize), float64(squareSize), float64(squareSize))
|
||||
cv.Arc(float64(x*squareSize), float64(y*squareSize), float64(squareSize)/2, 0, math.Pi*2, false)
|
||||
cv.ClosePath()
|
||||
cv.Fill()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
table = updateTable(table)
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue