-
Notifications
You must be signed in to change notification settings - Fork 0
/
confour.py
194 lines (170 loc) · 6.42 KB
/
confour.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
from copy import deepcopy
import heapq
DEPTH = 5
RED = 1 # max
BLACK = 2 # min
class Board:
def __init__(self, width=7, height=6, win_len=4):
self.width = width
self.height = height
self.win_len = win_len
self.grid = [[0] * width for i in range(height)]
self.num_in_column = [0] * width
self.current_player = RED
self.last_move = -1
self.ended = 0
self.red_segments = []
self.black_segments = []
def print_board(self):
for row in reversed(self.grid):
for cell in row:
print cell,
print
print
def place_in_column(self, col):
row = self.num_in_column[col]
if row == self.height: raise IndexError
self.grid[row][col] = self.current_player
self.num_in_column[col] += 1
self.last_move = col
if self.check_win(row, col) != 0:
self.ended = self.current_player
self.current_player ^= 3
def check_win(self, row, col):
color = self.grid[row][col]
return any([self.__num_in_direction(color, row, col, i, j) >= self.win_len for (i,j) in [(1, 0), (0, 1), (1, 1), (1, -1)]])
def __num_in_direction(self, color, row, col, deltax, deltay):
def inner(dx, dy):
count = 0
x, y = row, col
for i in range(self.width + self.height):
x, y = x + dx, y + dy
try:
assert(x >= 0 and y >= 0)
if self.grid[x][y] != color: return count
except IndexError:
return count
except AssertionError:
return count
count += 1
return count
return 1 + inner(deltax, deltay) + inner(-deltax, -deltay)
def children(self):
ch = []
for i in range(self.width):
b = deepcopy(self)
try:
b.place_in_column(i)
ch.append(b)
except IndexError:
pass
return ch
def get_segments(self, row, col):
segments = []
rmin, cmin = max(0, row + 1 - self.win_len), max(0, col + 1 - self.win_len)
rmax, cmax = min(self.height - self.win_len, row), min(self.width - self.win_len, col)
# rows
c = cmin
while c <= cmax:
segments.append([(row, i) for i in range(c, c + self.win_len)])
c += 1
# columns
r = rmin
while r <=rmax:
segments.append([(i, col) for i in range(r, r + self.win_len)])
r += 1
# up-right diagonals
r, c = row, col
while r >= rmin and c >= cmin:
seg = []
i, j = r, c
while i < r + self.win_len and j < c + self.win_len:
if i in range(self.height) and j in range(self.width):
seg.append((i, j))
else:
break
i, j = i + 1, j + 1
if len(seg) == self.win_len:
segments.append(seg)
r, c = r - 1, c - 1
# up-left diagonals
r, c = row, col
while r <= rmax + self.win_len and c >= cmin:
seg = []
i, j = r, c
while i > r - self.win_len and j < c + self.win_len:
if i in range(self.height) and j in range(self.width):
seg.append((i, j))
else:
break
i, j = i - 1, j + 1
if len(seg) == self.win_len:
segments.append(seg)
r, c = r + 1, c - 1
return segments
def score(self):
s = 0
if self.ended == RED: return 1000
elif self.ended == BLACK: return -1000
for row in range(self.height):
for col in range(self.width):
segments = self.get_segments(row, col)
for seg in segments:
num_red = sum(map(lambda x: 1 if self.grid[x[0]][x[1]] == RED else 0, seg))
num_black = sum(map(lambda x: 1 if self.grid[x[0]][x[1]] == BLACK else 0, seg))
if num_red == 0 or num_black == 0:
s += num_red**2
s -= num_black**2
return s
def score2(self):
s = 0
if self.ended == RED: return 1000
elif self.ended == BLACK: return -1000
for row in range(self.height):
for col in range(self.width):
if self.grid[row][col] == RED:
t = self.__num_in_direction(RED, row, col, 1, 0)
s += t**2
t = self.__num_in_direction(RED, row, col, 1, 1)
s += t**2
t = self.__num_in_direction(RED, row, col, 0, 1)
s += t**2
t = self.__num_in_direction(RED, row, col, 1, -1)
s += t**2
elif self.grid[row][col] == BLACK:
t = self.__num_in_direction(BLACK, row, col, 1, 0)
t -= t**2
t = self.__num_in_direction(BLACK, row, col, 1, 1)
t -= t**2
t = self.__num_in_direction(BLACK, row, col, 0, 1)
t -= t**2
t = self.__num_in_direction(BLACK, row, col, 1, -1)
t -= t**2
return s
def alphabeta(board, depth, alpha, beta, player, move):
if depth == 0 or board.ended != 0:
return board.score(), move
if player == RED:
for child in reversed(board.children()):
alpha, move = max((alpha, move), (alphabeta(child, depth-1, alpha, beta, player^3, move)[0], child.last_move))
if beta <= alpha: break
return alpha, move
else:
for child in board.children():
beta, move = min((beta, move), (alphabeta(child, depth-1, alpha, beta, player^3, move)[0], child.last_move))
if beta <= alpha: break
return beta, move
def get_move(board):
return alphabeta(board, DEPTH, -99999999, 99999999, board.current_player, -1)
#moves = []
#for i in range(board.width):
#b = deepcopy(board)
#try:
#b.place_in_column(i)
#moves.append((alphabeta(b, DEPTH, float('-inf'), float('inf'), b.current_player), i))
#except IndexError, e:
#pass
#if board.current_player == RED:
#return max(moves)
#else:
#return min(moves)