-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.py
128 lines (102 loc) · 4.45 KB
/
Board.py
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
from Node import *
from Constants import *
from collections import deque
class Board:
def __init__(self):
self.playing = "b"
self.matrix = [[Node(x, y) for x in range(SIZE)] for y in range(SIZE)]
self.koNode = None
def changePlayer(self):
if self.playing == "b":
self.playing = "w"
else:
self.playing = "b"
def getOtherPlayer(self):
if self.playing == "b":
return "w"
else:
return "b"
def show(self):
for x in range(SIZE):
for y in range(SIZE):
self.matrix[y][x].show()
def inPosBounds(self, x, y):
return x>MARGIN and x<XMAX and y>MARGIN and y<YMAX
def inBoardBounds(self, x, y):
return x>-1 and x<SIZE and y>-1 and y<SIZE
def getNodes(self, x, y):
return ((x - MARGIN) // CELLDIM, (y - MARGIN) // CELLDIM)
def checkHover(self, x, y):
if self.inPosBounds(x, y):
xNode, yNode = self.getNodes(x, y)
self.matrix[yNode][xNode].showTransparent(self.playing)
def evalLiberties(self, x, y, player):
if self.inBoardBounds(x, y):
node = self.matrix[y][x]
queue = deque([node])
searched = []
while len(queue) > 0:
node = queue.pop()
if not node.searched:
if node.isEmpty():
for capNode in searched:
capNode.searched = False
return
if node.piece != player:
node.searched = True
searched.append(node)
if self.inBoardBounds(node.x, node.y+1):
queue.appendleft(self.matrix[node.y+1][node.x])
if self.inBoardBounds(node.x, node.y-1):
queue.appendleft(self.matrix[node.y-1][node.x])
if self.inBoardBounds(node.x+1, node.y):
queue.appendleft(self.matrix[node.y][node.x+1])
if self.inBoardBounds(node.x-1, node.y):
queue.appendleft(self.matrix[node.y][node.x-1])
for capNode in searched:
capNode.searched = False
capNode.setEmpty()
def zeroLiberties(self, x, y, player):
node = self.matrix[y][x]
queue = deque([node])
searched = []
while len(queue) > 0:
node = queue.pop()
if not node.searched:
if node.isEmpty():
for capNode in searched:
capNode.searched = False
return False
if node.piece != player:
node.searched = True
searched.append(node)
if self.inBoardBounds(node.x, node.y+1):
queue.appendleft(self.matrix[node.y+1][node.x])
if self.inBoardBounds(node.x, node.y-1):
queue.appendleft(self.matrix[node.y-1][node.x])
if self.inBoardBounds(node.x+1, node.y):
queue.appendleft(self.matrix[node.y][node.x+1])
if self.inBoardBounds(node.x-1, node.y):
queue.appendleft(self.matrix[node.y][node.x-1])
for capNode in searched:
capNode.searched = False
return True
def checkCaptures(self, x, y):
self.evalLiberties(x-1, y, self.playing)
self.evalLiberties(x+1, y, self.playing)
self.evalLiberties(x, y-1, self.playing)
self.evalLiberties(x, y+1, self.playing)
def checkClick(self, x, y):
if self.inPosBounds(x, y):
xNode, yNode = self.getNodes(x, y)
node = self.matrix[yNode][xNode]
if node.isEmpty():
if self.playing == "b":
node.setBlack()
elif self.playing == "w":
node.setWhite()
self.checkCaptures(xNode, yNode)
if self.zeroLiberties(xNode, yNode, self.getOtherPlayer()):
node.setEmpty()
else:
self.changePlayer()