-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
68 lines (57 loc) · 1.19 KB
/
queue.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
package main
import (
"strings"
)
type MovementQueue struct {
data []*Move
head *Move
}
func MakeMovementQueue() *MovementQueue {
return &MovementQueue{
[]*Move{},
nil,
}
}
func (queue *MovementQueue) String() string {
var str strings.Builder
for i := 0; i < len(queue.data); i++ {
str.WriteString(queue.data[i].String() + "\n")
}
return str.String()
}
// Queue is FIFO
func (queue *MovementQueue) Enqueue(move *Move) error {
queue.data = append(queue.data, move)
queue.head = move
return nil
}
type BoardQueue struct {
data []*Grid
head *Grid
}
func MakeBoardQueue() *BoardQueue {
return &BoardQueue{
[]*Grid{},
nil,
}
}
func (queue *BoardQueue) Enqueue(board *Grid) error {
// Make a snapshot of the grid to store as history
snapshot := board.Clone()
queue.data = append(queue.data, &snapshot)
queue.head = &snapshot
return nil
}
func (queue *BoardQueue) IsKo(board *Grid) bool {
// Ko is when move n == n-2
currentBoard := *board
previousBoard := *queue.data[len(queue.data)-2]
for i := 0; i < len(currentBoard); i++ {
for y := 0; y < len(currentBoard); y++ {
if currentBoard[i][y].piece != previousBoard[i][y].piece {
return false
}
}
}
return true
}