-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (106 loc) · 2.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"image"
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2"
)
const (
width = 700
height = 400
w = 3
)
var (
columns = width / w
rows = height / w
board [][]int
next [][]int
)
func init() {
// Initialize the board with zeros
board = make([][]int, columns)
for i := range board {
board[i] = make([]int, rows)
}
next = make([][]int, columns)
for i := range next {
next[i] = make([]int, rows)
}
initGrid()
}
func initGrid() {
board[columns/4][0] = 1
board[columns/2][0] = 1
board[(columns*6)/8][0] = 1
for j := 0; j < rows; j++ {
for i := 0; i < columns; i++ {
if i != 0 && j != 0 && i != columns-1 {
XORCompare(i, j)
}
}
for i2 := columns - 1; i2 > 0; i2-- {
if i2 != 0 && j != 0 && i2 != columns-1 {
XORCompare(i2, j)
}
}
}
}
func XORCompare(i, j int) {
UtL := (board[i-1][j-1] == 1)
UuU := (board[i][j-1] == 1)
UtR := (board[i+1][j-1] == 1)
if (UtL && !(UuU || UtR)) || (!UtL && (UuU || UtR)) {
board[i][j] = 1
} else {
board[i][j] = 0
}
}
func update(screen *ebiten.Image) error {
screen.Fill(color.White)
// Draw the grid cells
for i := 0; i < columns; i++ {
for j := 0; j < rows; j++ {
var c color.Color
if board[i][j] == 1 {
c = color.Black
} else {
c = color.White
}
// Creating the rectangle for cell
rectImage := image.NewRGBA(image.Rect(0, 0, w-1, w-1))
for yi := 0; yi < w-1; yi++ {
for xi := 0; xi < w-1; xi++ {
rectImage.Set(xi, yi, c)
}
}
// Draw the rectangle on the screen at the correct position
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(i*w), float64(j*w)) // Translate to the correct position
screen.DrawImage(ebiten.NewImageFromImage(rectImage), op)
}
}
return nil
}
type Game struct{}
// Update is called every frame to update the game state.
func (g *Game) Update() error {
// No updates to game state needed, this is where you would add logic.
return nil
}
// Draw is called every frame to render the game state.
func (g *Game) Draw(screen *ebiten.Image) {
update(screen)
}
// Layout is required by ebiten.Game and specifies the screen size.
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return width, height
}
func main() {
// Create a new Ebiten game window
ebiten.SetWindowSize(width, height)
ebiten.SetWindowTitle("Go Grid Simulation")
// Start the game loop
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}