-
Notifications
You must be signed in to change notification settings - Fork 0
/
arbiter.py
177 lines (162 loc) · 6.87 KB
/
arbiter.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
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
from patternpieces import PatternPieces
class Arbiter:
#adding x, y because for checking the piece must not be set
#Adding True instead of None for walls and keeping none for no piece
#TODO Factorize this fonction
def is_placement_valid(self, piece, board, x, y):
nearbyPieces = []
mismatch = False
if y <= 0:
nearbyPieces.append(True)
else:
nearbyPieces.append(board[x, y - 1])
if x >= 15:
nearbyPieces.append(True)
else:
nearbyPieces.append(board[x + 1, y])
if y >= 15:
nearbyPieces.append(True)
else:
nearbyPieces.append(board[x, y + 1])
if x <= 0:
nearbyPieces.append(True)
else:
nearbyPieces.append(board[x - 1, y])
for rotatetry in range(0, 4):
piece.nbofrightrotate = rotatetry
for i in range(0, 4):
if nearbyPieces[i] is True:
if piece.getSidePattern(i) != PatternPieces.EDGE:
mismatch = True
break
elif nearbyPieces[i] is not None:
if nearbyPieces[i].getSidePattern(i - 2) != piece.getSidePattern(i):
mismatch = True
break
else:
if piece.getSidePattern(i) == PatternPieces.EDGE:
mismatch = True
break
if mismatch is False:
return True
else:
mismatch = False
return False
def check_and_place(self, piece, board, x, y):
if self.is_placement_valid(piece, board, x, y):
board[x, y] = piece
piece.placed = True
return True
return False
def just_place(self, piece, board, x, y):
board[x, y] = piece
piece.placed = True
def nb_edge_piece(self, board, x, y):
nb_match = 0
if (board[x, y] is not None):
if y > 0:
if (board[x, y - 1] is not None):
if board[x, y].getSidePattern(0) == board[x, y - 1].getSidePattern(2):
nb_match += 1
else:
if board[x, y].getSidePattern(0) == PatternPieces.EDGE:
nb_match += 1
if y < 15:
if (board[x, y + 1] is not None):
if board[x, y].getSidePattern(2) == board[x, y + 1].getSidePattern(0):
nb_match += 1
else:
if board[x, y].getSidePattern(2) == PatternPieces.EDGE:
nb_match += 1
if x > 0:
if (board[x - 1, y] is not None):
if board[x, y].getSidePattern(3) == board[x - 1, y].getSidePattern(1):
nb_match += 1
else:
if board[x, y].getSidePattern(3) == PatternPieces.EDGE:
nb_match += 1
if x < 15:
if (board[x + 1, y] is not None):
if board[x, y].getSidePattern(1) == board[x + 1, y].getSidePattern(3):
nb_match += 1
else:
if board[x, y].getSidePattern(1) == PatternPieces.EDGE:
nb_match += 1
return nb_match
#TODO: Factorize
def nb_edge_match(self, board):
nb_match = 0
for y in range(16):
for x in range(16):
if (board[x, y] is not None):
if y > 0:
if (board[x, y - 1] is not None):
if board[x, y].getSidePattern(0) == board[x, y - 1].getSidePattern(2):
nb_match += 1
else:
if board[x, y].getSidePattern(0) == PatternPieces.EDGE:
nb_match += 1
if y < 15:
if (board[x, y + 1] is not None):
if board[x, y].getSidePattern(2) == board[x, y + 1].getSidePattern(0):
nb_match += 1
else:
if board[x, y].getSidePattern(2) == PatternPieces.EDGE:
nb_match += 1
if x > 0:
if (board[x - 1, y] is not None):
if board[x, y].getSidePattern(3) == board[x - 1, y].getSidePattern(1):
nb_match += 1
else:
if board[x, y].getSidePattern(3) == PatternPieces.EDGE:
nb_match += 1
if x < 15:
if (board[x + 1, y] is not None):
if board[x, y].getSidePattern(1) == board[x + 1, y].getSidePattern(3):
nb_match += 1
else:
if board[x, y].getSidePattern(1) == PatternPieces.EDGE:
nb_match += 1
return nb_match
def isGoalAchieved(self, board):
for y in range(16):
for x in range(16):
if (board[x, y] is None):
return False
# Checks above cell
if (y != 0):
side = board[x, y - 1].getSidePattern(2)
if (side == False or side != board[x, y].getSidePattern(0)):
return False
# Checks if top side is an edge
else:
if (board[x, y].getSidePattern(0) != PatternPieces.EDGE):
return False
# Checks next cell
if (x != 15):
side = board[x + 1, y].getSidePattern(3)
if (side == False or side != board[x, y].getSidePattern(1)):
return False
# Checks if right side is an edge
else:
if (board[x, y].getSidePattern(1) != PatternPieces.EDGE):
return False
# Checks below cell
if (y != 15):
side = board[x, y + 1].getSidePattern(0)
if (side == False or side != board[x, y].getSidePattern(2)):
return False
# Checks if bottom side is an edge
else:
if (board[x, y].getSidePattern(2) != PatternPieces.EDGE):
return False
# Checks previous cell
if (x != 0):
side = board[x - 1, y].getSidePattern(1)
if (side == False or side != board[x, y].getSidePattern(3)):
return False
# Checks if left side is an edge
else:
if (board[x, y].getSidePattern(3) != PatternPieces.EDGE):
return False
return True