-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
198 lines (191 loc) · 6.13 KB
/
util.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Constants
depth = 3 # how many moves ahead to look
won = 10000
lost = -10000
size = 8 # size of board
const = 3 # number for heuristic that you multiply the importance of quantity of coins by
# Functions
def move(token1, token2):
# flip all pieces in row to colour corresponding to end 2 tokens
# step one determine whether diag, row or column
# step 2 iterate through appropriate one calling flip on each token
x = token1.x
y = token1.y
x1 = token2.x
y2 = token2.y
if x == x1:
if y < y1:
for i in xrange(y,y1):
token(x,i).flip()
else:
for i in xrange(y1,y):
token(x,i).flip()
elif y == y1:
if x < x1:
for j in xrange(x,x1):
token(j,y).flip()
else:
for j in xrange(x1,x):
token(j,y).flip()
else:
if x < x1 and y < y1:
for i in xrange(x,x1):
for j in xrange(y,y1):
token(i,j).flip()
elif x > x1 and y < y1:
for i in xrange(x1,x):
for j in xrange(y,y1):
token(i,j).flip()
elif x < x1 and y > y1:
for i in xrange(x,x1):
for j in xrange(y1,y):
token(i,j).flip()
else:
for i in xrange(x1,x):
for j in xrange(y1,y):
token(i,j).flip()
return 0
# SURELY THERE IS A NEATER WAY TO DO THIS... TO DO LATER
def valid_moves(board, colour):
if colour == black:
other = white
else:
other = black
# returns a list of valid moves
moves = []
# for each other_colour piece on the board:
# if it has an adjacent blank piece next to it remember and check if it has one of your own next to it...
# or in the row/column/diagnonal next to it
pieces = []
for token in board.tokens:
if token.colour == other:
pieces.append(token)
search = []
for token in pieces:
mysearch = neighbours(token,blank)
search.append(mysearch)
# check if each move is valid and evaluate new heuristic
for tokens in search:
for possible in tokens:
if valid_row(possible, piece[tokens], board):
value = evaluate(row)
moves.append(token, value) # token is the piece you are placing
return moves
def valid_row(newToken, oldToken, board):
valid = False
found = False
token = NULL
# it continues along row/column/diagonal to see if the end of that row/column diagonal has your piece in it,
# returns true(eventually hits new colour piece) or false (eventually hits a blank)
x = newtoken.x
y = newtoken.y
x1 = oldtoken.x
y2 = oldtoken.y
if x == x1:
i = y
if y < y1:
while i < size and not found:
if not token(x,i) or token(x,i).colour == newToken.colour:
found = true
if token(x,i).colour == newToken.colour:
valid = true
token = token(x,i)
i = i + 1
else:
while i < 0 and not found:
if not token(x,i) or token(x,i).colour == newToken.colour:
found = true
if token(x,i).colour == newToken.colour:
valid = true
token = token(x,i)
i = i - 1
elif y == y1:
j = x
if x < x1:
while j < size and not found:
if not token(x,j) or token(x,j).colour == newToken.colour:
found = true
if token(x,j).colour == newToken.colour:
valid = true
token = token(j,y)
i = i + 1
else:
while j > 0 and not found:
if not token(x,j) or token(x,j).colour == newToken.colour:
found = true
if token(x,j).colour == newToken.colour:
valid = true
token = token(j,y)
i = i - 1
else:
if x < x1 and y < y1:
for i in xrange(x,x1):
for j in xrange(y,y1):
while j < size and i < size and not found:
if not token(i,j) or token(i,j).colour == newToken.colour:
found = true
if token(i,j).colour == newToken.colour:
valid = true
token = token(i,j)
i = i + 1
j = j + 1
elif x > x1 and y < y1:
for i in xrange(x1,x):
for j in xrange(y,y1):
while j < size and i > 0 and not found:
if not token(i,j) or token(i,j).colour == newToken.colour:
found = true
if token(i,j).colour == newToken.colour:
valid = true
token = token(i,j)
i = i - 1
j = j + 1
elif x < x1 and y > y1:
for i in xrange(x,x1):
for j in xrange(y1,y):
while j > 0 and i < size and not found:
if not token(i,j) or token(i,j).colour == newToken.colour:
found = true
if token(i,j).colour == newToken.colour:
valid = true
token = token(i,j)
i = i + 1
j = j - 1
else:
for i in xrange(x1,x):
for j in xrange(y1,y):
while j > 0 and i > 0 and not found:
if not token(i,j) or token(i,j).colour == newToken.colour:
found = true
if token(i,j).colour == newToken.colour:
valid = true
token = token(i,j)
i = i - 1
j = j - 1
return token
# THERE MUST BE A NICER WAY OF DOING THIS TO
def evaluate(newToken, oldToken):
# TO DO
# returns two integers the first saying how much the playing players heuristic goes up, the second how much the others goes down
current = 0
opponent = 0
search = neighbours(newToken)
# from oponnent remove number of coins * const
if newToken == (token(0,0) or token(size,size) or token(0,size) or token(size,0)):
newToken.stabalise(10)
# if stablise value not 5 then make value one less than highest neighbour value
# do this for every flipped token
# add number of extra coins*const + token.stablise of each token to current
return (current,opponent)
def neighbours(token,goal):
# takes arguement type: blank/black/white; and a token
# returns list of all such neighbours
search = [(i,j) for i in [-1,0,1] for j in [-1,0,1] if token(i,j) and token(i,j).colour == goal]
return search
def eval1(board,player):
toks = board.get_alltoks()
mytoks = []
for tok in toks:
if tok.colour == player.colour:
mytoks.append(tok)
return size(mytoks)