-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.go
167 lines (140 loc) · 3.69 KB
/
board.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package astar
import (
"math/rand"
"github.com/hajimehoshi/ebiten/v2"
)
// Board represents the game board.
type Board struct {
width int
height int
tiles []*Tile
graph *AStarGraph
initialized bool
}
// NewBoard generates a new Board with giving a size.
func NewBoard(height, width int) (*Board, error) {
b := &Board{
height: height,
width: width,
tiles: make([]*Tile, height*width),
initialized: false,
}
for i := range b.tiles {
x, y := b.Coord(i)
b.tiles[i] = &Tile{
kind: TypeBlank,
x: x,
y: y,
}
}
b.Start().SetKind(TypeStart)
b.End().SetKind(TypeEnd)
return b, nil
}
// Index returns the position in the array for a coordinate.
// Board stores the grid as a 1-d array, so this helper computes the
// 1-d index based on the 2-d coordinates.
func (b *Board) Index(x, y int) int {
return y*b.width + x
}
// Coord returns the 2-d coordinates for an array index.
// Board stores the grid as a 1-d array.
func (b *Board) Coord(index int) (int, int) {
x := index % b.width
y := index / b.width
return x, y
}
// Start returns the top left tile in the board which is our starting point.
func (b *Board) Start() *Tile {
index := b.Index(0, 0)
return b.tiles[index]
}
// End returns the bottom right tile in thee board, which is our destination.
func (b *Board) End() *Tile {
index := b.Index(b.width-1, b.height-1)
return b.tiles[index]
}
func (b *Board) initialize() {
g := NewAStarGraph(b)
b.graph = g
b.initialized = true
}
// addRandomWalls adds numWalls in random locations on the board.
func (b *Board) addRandomWalls(numWalls int) {
start := b.Start()
end := b.End()
for i := 0; i < numWalls; i++ {
isValidTile := false
for !isValidTile {
x := rand.Intn(b.width)
y := rand.Intn(b.height)
index := b.Index(x, y)
tile := b.tiles[index]
// Start and end are not valid walls.
if tile == start || tile == end || tile.kind == TypeWall {
continue
}
isValidTile = true
tile.SetKind(TypeWall)
}
}
}
// IsOnBoard reeturns true if the coordinate is on the board.
// This is the relative x and y coordiantes of the mouse, not the cell in the grid.
func (b *Board) IsOnBoard(x, y int) bool {
xOnBoard := x >= 0 && x <= (b.width*TileSize)+(b.width+1)*TileMargin
yOnBoard := y >= 0 && y <= (b.height*TileSize)+(b.height+1)*TileMargin
return xOnBoard && yOnBoard
}
// TileAt returns the tile at the x and y position on the board.
// This is the position of the mouse, not the x and y coordinates of the grid.
func (b *Board) TileAt(x, y int) *Tile {
xCoord := x / (TileSize + TileMargin)
yCoord := y / (TileSize + TileMargin)
index := b.Index(xCoord, yCoord)
return b.tiles[index]
}
// Update updates the board state.
func (b *Board) Update(input *Input) {
if b.initialized {
b.graph.Step()
return
}
if input.EnterPressed {
b.initialize()
return
}
if input.RightMousePressed {
b.addRandomWalls(10)
return
}
if !input.LeftMousePressed {
return
}
if b.IsOnBoard(input.MouseX, input.MouseY) {
tile := b.TileAt(input.MouseX, input.MouseY)
tile.TryFlipWall()
}
}
// Size returns the board size.
func (b *Board) Size() (int, int) {
x := b.width*TileSize + (b.width+1)*TileMargin
y := b.height*TileSize + (b.height+1)*TileMargin
return x, y
}
// Draw draws the board to the given boardImage.
func (b *Board) Draw(boardImage *ebiten.Image) {
boardImage.Fill(FrameColor)
for j := 0; j < b.height; j++ {
for i := 0; i < b.width; i++ {
op := &ebiten.DrawImageOptions{}
x := i*TileSize + (i+1)*TileMargin
y := j*TileSize + (j+1)*TileMargin
op.GeoM.Translate(float64(x), float64(y))
boardImage.DrawImage(tileImage, op)
}
}
for _, tile := range b.tiles {
tile.Draw(boardImage)
}
}