-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomoku.py
65 lines (49 loc) · 2.08 KB
/
gomoku.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
# Gomoku logic design based on xerwin's tutorial
# http://www.cnblogs.com/erwin/p/7828956.html
from boardstate import *
class Gomoku(object):
def __init__(self):
# create a N * N map
self.__chessMap = [[BoardState.EMPTY for j in range(N)]
for i in range(N)]
self.__currentI = -1
self.__currentJ = -1
self.__currentState = BoardState.EMPTY
def get_chessMap(self):
return self.__chessMap
def get_chessboard_state(self, i, j):
return self.__chessMap[i][j]
def set_chessboard_state(self, i, j, state):
self.__chessMap[i][j] = state
self.__currentI = i
self.__currentJ = j
self.__currentState = state
def get_chess_result(self):
if self.connected_five(self.__currentI, self.__currentJ, self.__currentState):
return self.__currentState
else:
return BoardState.EMPTY
def direction_count(self, i, j, xdirection, ydirection, player):
count = 0
for step in range(1, 5): # look four more steps on a certain direction
if xdirection != 0 and (j + xdirection * step < 0 or j + xdirection * step >= N):
break
if ydirection != 0 and (i + ydirection * step < 0 or i + ydirection * step >= N):
break
if self.__chessMap[i + ydirection * step][j + xdirection * step] == player:
count += 1
else:
break
return count
def connected_five(self, i, j, player):
# four directions: horizontal, vertical, two diagonals
directions = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, 1),
(1, -1)], [(-1, -1), (1, 1)]]
for axis in directions:
axis_count = 1
for (xdirection, ydirection) in axis:
axis_count += self.direction_count(i, j, xdirection,
ydirection, player)
if axis_count >= 5:
return True
return False