-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.go
489 lines (449 loc) · 10.4 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Package board gives functions that allows to interact with the structure Board
package main
import (
"container/list"
"fmt"
"strconv"
)
const BoardSize int = 8
// Struct board describes a board of othello and the current player
type Board struct {
Board [BoardSize + 2][BoardSize + 2]rune // board representation in matrix form
maxPlayer bool // True if the player is white
}
var localBoard *Board
// create a new board
func NewBoard() *Board {
return new(Board).Init()
}
// Initialize the structure board
func (b *Board) Init() *Board {
for i := 0; i < (BoardSize + 2); i++ {
for j := 0; j < (BoardSize + 2); j++ {
b.Board[i][j] = 'E'
}
}
b.Board[BoardSize/2][BoardSize/2] = 'W'
b.Board[BoardSize/2+1][BoardSize/2+1] = 'W'
b.Board[BoardSize/2][BoardSize/2+1] = 'B'
b.Board[BoardSize/2+1][BoardSize/2] = 'B'
b.maxPlayer = true
return b
}
// Initialize the board accordind to the string input
func (b *Board) OthelloPosition(s string) *Board {
if len(s) != 65 {
b.Init()
} else {
if s[0] == 'W' {
b.maxPlayer = true
} else {
b.maxPlayer = false
}
for i := 1; i <= 64; i++ {
var c rune
if s[i] == 'E' {
c = 'E'
} else if s[i] == 'O' {
c = 'W'
} else {
c = 'B'
}
column := ((i - 1) % 8) + 1
row := (i-1)/8 + 1
b.Board[row][column] = c
}
}
return b
}
// return which player turn it is
// true is white and false black
func (b *Board) GetMaxPlayer() bool {
return b.maxPlayer
}
// function ChangeMaxPlayer changes the player of the current structure Board
func (b *Board) ChangeMaxPlayer() {
b.maxPlayer = !b.maxPlayer
}
// take the x,y position of a cell
// return a boolean corresponding to the presence of neighbours
func hasNeighbor(row int, column int) bool {
if !isFree(row-1, column) {
return true
}
if !isFree(row-1, column+1) {
return true
}
if !isFree(row, column+1) {
return true
}
if !isFree(row+1, column+1) {
return true
}
if !isFree(row+1, column) {
return true
}
if !isFree(row+1, column-1) {
return true
}
if !isFree(row, column-1) {
return true
}
if !isFree(row-1, column-1) {
return true
}
return false
}
// return if the cell is free
func isFree(row int, col int) bool {
if localBoard.Board[row][col] == 'E' {
return true
}
return false
}
// return if a piece can be placed on this cell
func isCandidate(row int, col int) bool {
if isFree(row, col) && hasNeighbor(row, col) {
return true
}
return false
}
// return if the cell is own by the opponent
func isOpponentSquare(row int, column int) bool {
if localBoard.maxPlayer && (localBoard.Board[row][column] == 'B') {
return true
}
if !localBoard.maxPlayer && (localBoard.Board[row][column] == 'W') {
return true
}
return false
}
//return if the cell is own by the current player
func isOwnSquare(row, column int) bool {
if !localBoard.maxPlayer && (localBoard.Board[row][column] == 'B') {
return true
}
if localBoard.maxPlayer && (localBoard.Board[row][column] == 'W') {
return true
}
return false
}
// Check if it is possible to do a move to the north from this position
func checkNorth(row, column int) bool {
if !isOpponentSquare(row-1, column) {
return false
}
for i := (row - 2); i > 0; i-- {
if isFree(i, column) {
return false
}
if isOwnSquare(i, column) {
return true
}
}
return false
}
// Check if it is possible to do a move to the east from this position
func checkEast(row, col int) bool {
if !isOpponentSquare(row, col+1) {
return false
}
for i := col + 2; i <= BoardSize; i++ {
if isFree(row, i) {
return false
}
if isOwnSquare(row, i) {
return true
}
}
return false
}
// Check if it is possible to do a move to the south from this position
func checkSouth(row, col int) bool {
if !isOpponentSquare(row+1, col) {
return false
}
for i := row + 2; i <= BoardSize; i++ {
if isFree(i, col) {
return false
}
if isOwnSquare(i, col) {
return true
}
}
return false
}
// Check if it is possible to do a move to the west from this position
func checkWest(row, col int) bool {
if !isOpponentSquare(row, col-1) {
return false
}
for i := col - 2; i > 0; i-- {
if isFree(row, i) {
return false
}
if isOwnSquare(row, i) {
return true
}
}
return false
}
// Check if it is possible to do a move to the north east from this position
func checkNorthEast(row, col int) bool {
if !isOpponentSquare(row-1, col+1) {
return false
}
for i := 2; (row-i) > 0 && (col+i) <= BoardSize; i++ {
if isFree(row-i, col+i) {
return false
}
if isOwnSquare(row-i, col+i) {
return true
}
}
return false
}
// Check if it is possible to do a move to the south east from this position
func checkSouthEast(row, col int) bool {
if !isOpponentSquare(row+1, col+1) {
return false
}
for i := 2; row+1 <= BoardSize && col+i <= BoardSize; i++ {
if isFree(row+i, col+i) {
return false
}
if isOwnSquare(row+i, col+i) {
return true
}
}
return false
}
// Check if it is possible to do a move to the south west from this position
func checkSouthWest(row, col int) bool {
if !isOpponentSquare(row+1, col-1) {
return false
}
for i := 2; row+i <= BoardSize && col-i > 0; i++ {
if isFree(row+i, col-i) {
return false
}
if isOwnSquare(row+i, col-i) {
return true
}
}
return false
}
// Check if it is possible to do a move to the north west from this position
func checkNorthWest(row, col int) bool {
if !isOpponentSquare(row-1, col-1) {
return false
}
for i := 2; row-i > 0 && col-i > 0; i++ {
if isFree(row-i, col-i) {
return false
}
if isOwnSquare(row-i, col-i) {
return true
}
}
return false
}
// Check if it is possible to do a move from this position
func isMove(row int, col int) bool {
if checkNorth(row, col) {
return true
}
if checkNorthEast(row, col) {
return true
}
if checkEast(row, col) {
return true
}
if checkSouthEast(row, col) {
return true
}
if checkSouth(row, col) {
return true
}
if checkSouthWest(row, col) {
return true
}
if checkWest(row, col) {
return true
}
if checkNorthWest(row, col) {
return true
}
return false
}
// get all the possible moves starting from a board
// return a linked list of the moves
func (b *Board) GetMoves() *list.List {
localBoard = b
moves := list.New()
var candidates [BoardSize + 2][BoardSize + 2]bool
for i := 0; i < BoardSize; i++ {
for j := 0; j < BoardSize; j++ {
candidates[i][j] = isCandidate(i+1, j+1)
}
}
for i := 0; i < BoardSize; i++ {
for j := 0; j < BoardSize; j++ {
if candidates[i][j] {
if isMove(i+1, j+1) {
moves.PushBack(NewAction(i+1, j+1))
}
}
}
}
return moves
}
func printHorizontalBorder() {
fmt.Print("---")
for i := 1; i <= BoardSize; i++ {
fmt.Print("|---")
}
fmt.Println("|---")
}
// Draws an ASCII representation of the position. White squares are marked by
// '0' while black squares are marked by 'X'.
func (b *Board) Print() {
fmt.Print(" ")
for i := 1; i <= BoardSize; i++ {
fmt.Print("| " + strconv.Itoa(i) + " ")
}
fmt.Println("|")
printHorizontalBorder()
for i := 1; i <= BoardSize; i++ {
fmt.Print(" " + strconv.Itoa(i) + " ")
for j := 1; j <= BoardSize; j++ {
if b.Board[i][j] == 'W' {
fmt.Print("| 0 ")
} else if b.Board[i][j] == 'B' {
fmt.Print("| X ")
} else {
fmt.Print("| ")
}
}
fmt.Println("| " + strconv.Itoa(i) + " ")
printHorizontalBorder()
}
fmt.Print(" ")
for i := 1; i <= BoardSize; i++ {
fmt.Print("| " + strconv.Itoa(i) + " ")
}
fmt.Println("|")
fmt.Println()
}
// put the board in a string format
func (b *Board) ToString() string {
s := ""
var c, d rune
if b.maxPlayer {
s += "W"
} else {
s += "B"
}
for i := 1; i <= 8; i++ {
for j := 1; j <= 8; j++ {
d = b.Board[i][j]
if d == 'W' {
c = 'O'
} else if d == 'B' {
c = 'X'
} else {
c = 'E'
}
s += string(c)
}
}
return s
}
// copy the board structure
func (b *Board) Clone() *Board {
var newPosition *Board = NewBoard()
newPosition.maxPlayer = b.maxPlayer
for i := 0; i < BoardSize+2; i++ {
for j := 0; j < BoardSize+2; j++ {
newPosition.Board[i][j] = b.Board[i][j]
}
}
return newPosition
}
// change the color of an own cell
func (b *Board) flipSquare(row, col int) {
if b.maxPlayer {
b.Board[row][col] = 'W'
} else {
b.Board[row][col] = 'B'
}
}
// play an Action on the board
// return the new Board with the played action
func (b *Board) MakeMove(action *Action) *Board {
localBoard = b.Clone()
rowAction := action.GetRow()
colAction := action.GetColumn()
if action.IsPassMove() {
localBoard.maxPlayer = !localBoard.maxPlayer
return localBoard
} else {
// put the piece on the Board
if localBoard.maxPlayer {
localBoard.Board[rowAction][colAction] = 'W'
} else {
localBoard.Board[rowAction][colAction] = 'B'
}
// flip all the coins between the new coin and the existing one
if checkNorth(rowAction, colAction) {
for rowIndex := rowAction - 1; isOpponentSquare(rowIndex, colAction); rowIndex-- {
localBoard.flipSquare(rowIndex, colAction)
}
}
if checkEast(rowAction, colAction) {
for colIndex := colAction + 1; isOpponentSquare(rowAction, colIndex); colIndex++ {
localBoard.flipSquare(rowAction, colIndex)
}
}
if checkSouth(rowAction, colAction) {
for rowIndex := rowAction + 1; isOpponentSquare(rowIndex, colAction); rowIndex++ {
localBoard.flipSquare(rowIndex, colAction)
}
}
if checkWest(rowAction, colAction) {
for colIndex := colAction - 1; isOpponentSquare(rowAction, colIndex); colIndex-- {
localBoard.flipSquare(rowAction, colIndex)
}
}
if checkNorthEast(rowAction, colAction) {
rowIndex := rowAction - 1
for colIndex := colAction + 1; isOpponentSquare(rowIndex, colIndex); colIndex++ {
localBoard.flipSquare(rowIndex, colIndex)
rowIndex--
}
}
if checkNorthWest(rowAction, colAction) {
rowIndex := rowAction - 1
for colIndex := colAction - 1; isOpponentSquare(rowIndex, colIndex); colIndex-- {
localBoard.flipSquare(rowIndex, colIndex)
rowIndex--
}
}
if checkSouthWest(rowAction, colAction) {
rowIndex := rowAction + 1
for colIndex := colAction - 1; isOpponentSquare(rowIndex, colIndex); colIndex-- {
localBoard.flipSquare(rowIndex, colIndex)
rowIndex++
}
}
if checkSouthEast(rowAction, colAction) {
rowIndex := rowAction + 1
for colIndex := colAction + 1; isOpponentSquare(rowIndex, colIndex); colIndex++ {
localBoard.flipSquare(rowIndex, colIndex)
rowIndex++
}
}
localBoard.ChangeMaxPlayer()
return localBoard
}
}