-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
executable file
·279 lines (253 loc) · 7.99 KB
/
game.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
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
# -*- coding: utf-8 -*-
import math
import random
class board:
def __init__(self, size='3x6', discs=4):
self.size = size
self.board = []
self.board_heatmap = []
self.UNUSED = '_'
self.NTOWIN = discs
self.fourinarow = None
self.freespaces = -1 # ... something impossible
return self.init()
def init(self):
(h,w) = self.size.split('x')
self.height = int(h)
self.width = int(w)
self.freespaces = self.height * self.width
if (self.height < self.NTOWIN and self.width < self.NTOWIN):
raise Exception("The number of discs-in-a-row to win, may not be greater than both board dimensions!")
for n in range(self.height):
row = []
for j in range(self.width):
row.append(self.UNUSED) # default unused cell
self.board.append(row)
# initialize heatmap
for n in range(self.height):
row = []
for col in range(self.width):
row.append(self.heat_value(col-(self.width/2)))
self.board_heatmap.append(row)
def heat_value(self, n):
return 1-(n*n)/50.0 # 1 - (x^2 / 50.0)
def __getitem__(self,pos):
return self.board[pos]
def draw(self):
rstr = ''
for n in range(0, self.width):
rstr += ' ' + str(n) + ' '
print rstr
for row in self.board:
rstr = ''
for cell in row:
rstr += "[" + cell + "] "
print rstr
def draw_gui(self,something):
# XXX: TBD!
return False
def inuse(self,row,col):
try:
if (self.board[row][col] != self.UNUSED):
return True
else:
return False
except Exception as e:
print e
return True # out-of-bounds cells are considered "in use", so we avoid handling these exceptions
def play(self, symbol, col):
if (self.freespaces == 0):
print "BOARD IS FULL!"
return False
if (self.fourinarow):
#print self.fourinarow
return False # game is over, we won't allow more moves
if (len(self.board[0]) <= col or col < 0):
raise Exception("Column out of bounds");
# Let's start at the "bottom" and work our way up
for n in range(self.height-1, -1, -1):
#print "Checking [" + str(col) + "][" + str(n) + "]"
if (not self.inuse(n, col)): # this cell is free, let's add the symbol and return True
self.board[n][col] = symbol
# we have successfully placed the symbol, let's check for 4-in-a-row!
self.checkinarow()
self.freespaces -= 1 # let's keep track of free spaces
return True
return False
def checkinarow(self):
eval_value = self.evaluate()
if (eval_value == 1.0):
self.fourinarow = 'X'
elif (eval_value == -1.0):
self.fourinarow = 'O'
def evaluate(self):
max1 = 0
last_value = 0
current_value = 0
try:
# Fara yfir dálka
for j in range(self.height):
last_value = 0
current_value = 0
for i in range(self.width):
if (self.get_value(j,i) == last_value):
current_value += self.get_value(j,i)
max1 = self.get_maxvalue(current_value, max1)
else:
current_value = self.get_value(j,i)
max1 = self.get_maxvalue(current_value, max1)
last_value = current_value
for i in range(self.width):
last_value = 0
current_value = 0
for j in range(self.height):
if self.get_value(j,i) == last_value:
current_value += self.get_value(j,i)
max1 = self.get_maxvalue(current_value, max1)
else:
current_value = self.get_value(j,i)
max1 = self.get_maxvalue(current_value, max1)
last_value = current_value
#print 'j: ' + str(j) + ' i: ' + str(i) + ' current: ' + str(current_value) + ' last: ' + str(last_value) + ' cell value: ' + str(self.get_value(j,i)) + ' max1: ' + str(max1)
# Check diagonal
for j in range(self.height):
j_t = j
i_t = 0
last_value = 0
current_value = 0
while (j_t >= 0 and i_t < self.width):
if self.get_value(j_t,i_t) == last_value:
current_value += self.get_value(j_t,i_t)
max1 = self.get_maxvalue(current_value, max1)
else:
current_value = self.get_value(j_t,i_t)
max1 = self.get_maxvalue(current_value, max1)
last_value = current_value
j_t -= 1
i_t += 1
for i in range(self.width):
j_t = self.height-1
i_t = i
last_value = 0
current_value = 0
while (j_t >= 0 and i_t < self.width):
if self.get_value(j_t,i_t) == last_value:
current_value += self.get_value(j_t,i_t)
max1 = self.get_maxvalue(current_value, max1)
else:
current_value = self.get_value(j_t,i_t)
max1 = self.get_maxvalue(current_value, max1)
last_value = current_value
j_t -= 1
i_t += 1
for j in range(self.height):
j_t = j
i_t = self.width-1
last_value = 0
current_value = 0
while (j_t >= 0 and i_t >= 0):
if self.get_value(j_t,i_t) == last_value:
current_value += self.get_value(j_t,i_t)
max1 = self.get_maxvalue(current_value, max1)
else:
current_value = self.get_value(j_t,i_t)
max1 = self.get_maxvalue(current_value, max1)
last_value = current_value
j_t -= 1
i_t -= 1
for i in range(self.width-1,-1,-1):
j_t = self.height-1
i_t = i
last_value = 0
current_value = 0
while (j_t >= 0 and i_t >= 0):
if self.get_value(j_t,i_t) == last_value:
current_value += self.get_value(j_t,i_t)
max1 = self.get_maxvalue(current_value, max1)
else:
current_value = self.get_value(j_t,i_t)
max1 = self.get_maxvalue(current_value, max1)
last_value = current_value
j_t -= 1
i_t -= 1
return float(float(max1)/float(self.NTOWIN))
except Exception as e:
print 'evaluate:'
print e
def get_value(self,row,col):
if self.board[row][col] == 'X':
return 1
elif self.board[row][col] == 'O':
return -1
else:
return 0
def get_maxvalue(self, current_value, max1):
try:
if math.fabs(float(current_value)) > math.fabs(float(max1)):
return current_value
else:
return max1
except Exception as e:
print 'get_maxvalue'
print e
class computer:
def __init__(self, board, difficulty = 3):
self.difficulty = difficulty
self.board = board
def legal_moves(self):
M = []
for i in range(self.board.width):
for j in range(self.board.height-1,-1,-1):
if not self.board.inuse(j,i):
L = [j, i]
M.append(L)
#print str(L[0])+','+str(L[1])
break
return M
def play(self, symbol):
if (self.board.freespaces == 0): # ^ ^ ^
print "BOARD IS FULL!"
return False
my_symbol = symbol
v = []
M = self.legal_moves()
for L in (M):
self.board[L[0]][L[1]] = my_symbol
if my_symbol == 'X':
player_symbol = 'O'
else:
player_symbol = 'X'
v.append(self.minimax_value(player_symbol, my_symbol, self.difficulty))
self.board[L[0]][L[1]] = self.board.UNUSED
best_move = 0
if my_symbol == 'X':
best_move = max(v)
else:
best_move = min(v)
#for i in range(len(v)):
# move = v[i]
# print "move=" + str(M[i]) + " : " + str(i) + " einkunn: " + str(move)
L = M[v.index(best_move)]
#print "Computer (" + player_symbol + ") choosing move: " + str(L) + " (" + str(best_move) + ")"
self.board.play(my_symbol, L[1])
def minimax_value(self, player_symbol, symbol, iterations):
if iterations == 0:
return self.board.evaluate()
iterations -= 1
v = []
M = self.legal_moves()
if len(M) == 0:
return self.board.evaluate()
for L in (M):
self.board[L[0]][L[1]] = symbol #
multiplier = self.board.board_heatmap[L[0]][L[1]]
#print "column: " + str(L[1]) + " multiplier: " + str(multiplier)
# Switch player symbols for the next move
mv = self.minimax_value(symbol, player_symbol, iterations)
#print "minimax value=" + str(mv) + " after multiplier=" + str(multiplier*mv)
v.append(multiplier * mv)
self.board[L[0]][L[1]] = self.board.UNUSED
if player_symbol == 'X':
return max(v)
else:
return min(v)