-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.go
171 lines (138 loc) · 3.45 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
168
169
170
171
package main
import (
"github.com/veandco/go-sdl2/sdl"
)
type board struct {
turn int
width int
height int
imageSize int
playCount int
pieces [][]int
renderer *sdl.Renderer
player1Image *sdl.Texture
player2Image *sdl.Texture
}
const (
GRID_EMPTY = 0
PLAYER_1 = 1
PLAYER_2 = 2
MAX_PLAYS = 9
)
func newBoard(sqrSize int, renderer *sdl.Renderer, images []*sdl.Texture) *board {
return &board{
pieces: [][]int{
make([]int, 3),
make([]int, 3),
make([]int, 3),
},
renderer: renderer,
turn: PLAYER_1,
player1Image: images[0],
player2Image: images[1],
width: sqrSize,
height: sqrSize,
imageSize: sqrSize / 4,
}
}
func (b *board) getPiecePosition(row, col int) (int32, int32) {
square := b.height / 3
padding := square - b.imageSize
x := col*square + padding/2
y := row*square + padding/2
return int32(x), int32(y)
}
func (b *board) renderPiece(row int, col int, img *sdl.Texture) {
x, y := b.getPiecePosition(row, col)
b.renderer.Copy(img, nil, &sdl.Rect{x, y, int32(b.imageSize), int32(b.imageSize)})
}
func (b *board) renderPieces() {
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
switch b.pieces[i][j] {
case PLAYER_1:
b.renderPiece(i, j, b.player1Image)
case PLAYER_2:
b.renderPiece(i, j, b.player2Image)
}
}
}
}
func (b *board) drawBoard() {
b.renderer.SetDrawColor(255, 255, 255, 255)
b.renderer.Clear()
b.renderer.SetDrawColor(0, 0, 0, 255)
// vertical lines
b.renderer.DrawLine(screenWidth/3, 0, screenWidth/3, screenHeight)
b.renderer.DrawLine(screenWidth/3*2, 0, screenWidth/3*2, screenHeight)
// horizontal lines
b.renderer.DrawLine(0, screenHeight/3, screenWidth, screenHeight/3)
b.renderer.DrawLine(0, screenHeight/3*2, screenWidth, screenHeight/3*2)
b.renderPieces()
b.renderer.Present()
}
func (b *board) handleMouseButtonEvent(e *sdl.MouseButtonEvent) (bool, int) {
if e.Type != sdl.MOUSEBUTTONDOWN || e.Button != sdl.BUTTON_LEFT {
return false, 0
}
row := e.Y / (screenHeight / 3)
col := e.X / (screenWidth / 3)
if !b.validateMove(row, col) {
return false, 0
}
b.setMove(row, col)
b.playCount++
gameOver, winner := b.checkStatus()
if b.playCount == MAX_PLAYS && !gameOver {
return true, 0
}
if !gameOver {
b.changePlayer()
}
return gameOver, winner
}
func (b *board) validateMove(row, col int32) bool {
return b.pieces[row][col] == GRID_EMPTY
}
func (b *board) setMove(row, col int32) {
b.pieces[row][col] = b.turn
}
func (b *board) changePlayer() {
if b.turn == PLAYER_1 {
b.turn = PLAYER_2
return
}
b.turn = PLAYER_1
}
func (b *board) checkStatus() (bool, int) {
gameOver := b.checkRows() || b.checkColumns() || b.checkDiagonals()
if gameOver {
return gameOver, b.turn
}
return false, 0
}
func (b *board) checkRows() bool {
for i := 0; i < 3; i++ {
if b.pieces[i][0] == b.turn && b.pieces[i][0] == b.pieces[i][1] && b.pieces[i][0] == b.pieces[i][2] {
return true
}
}
return false
}
func (b *board) checkColumns() bool {
for i := 0; i < 3; i++ {
if b.pieces[0][i] == b.turn && b.pieces[0][i] == b.pieces[1][i] && b.pieces[0][i] == b.pieces[2][i] {
return true
}
}
return false
}
func (b *board) checkDiagonals() bool {
if b.pieces[0][0] == b.turn && b.pieces[0][0] == b.pieces[1][1] && b.pieces[0][0] == b.pieces[2][2] {
return true
}
if b.pieces[0][2] == b.turn && b.pieces[0][2] == b.pieces[1][1] && b.pieces[0][0] == b.pieces[2][0] {
return true
}
return false
}