Repository navigation
Expand file tree
/
Copy pathboardpy.py
More file actions
86 lines (71 loc) · 2.99 KB
/
Copy pathboardpy.py
File metadata and controls
86 lines (71 loc) · 2.99 KB
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
import numpy as np
class Board(object):
def __init__(self, width = 6, grid = None):
self.width = width
if (grid == None):
self.grid = np.zeros((self.width, self.width), dtype=int)
self.grid[width // 2 - 1][width // 2 - 1] = 1
self.grid[width // 2][width // 2] = 1
self.grid[width // 2 - 1][width // 2] = -1
self.grid[width // 2][width // 2 - 1] = -1
else:
self.grid = grid[:]
def countWinner(self):
white = 0
black = 0
for i in range(self.width):
for j in range(self.width):
if (self.grid[i][j] == 1):
white += 1
elif (self.grid[i][j] == -1):
black += 1
if (white > black):
return 1
elif (white < black):
return -1
else:
return 0
def inSideBoard(self, i):
return i < self.width and i >= 0
def canPositionOn(self, i, j, player):
if (self.grid[i][j] != 0):
return False
directions = [[1,1],[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1],[0,1]]
for direction in directions:
tmpi, tmpj = [i + direction[0], j + direction[1]]
while (self.inSideBoard(tmpi) and self.inSideBoard(tmpj) and self.grid[tmpi][tmpj] == -player):
tmpi += direction[0]
tmpj += direction[1]
if (self.inSideBoard(tmpi) and self.inSideBoard(tmpj) and self.grid[tmpi][tmpj] == player and [i + direction[0], j + direction[1]] != [tmpi, tmpj]):
return True
return False
def positioning(self, i, j, player):
# if (not self.canPositionOn(i, j, player)):
# return
self.grid[i][j] = player
directions = [[1,1],[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1],[0,1]]
for direction in directions:
tmpi, tmpj = [i + direction[0], j + direction[1]]
steps = []
while (self.inSideBoard(tmpi) and self.inSideBoard(tmpj) and self.grid[tmpi][tmpj] == -player):
steps.append([tmpi, tmpj])
tmpi += direction[0]
tmpj += direction[1]
if (self.inSideBoard(tmpi) and self.inSideBoard(tmpj) and self.grid[tmpi][tmpj] == player and [i + direction[0], j + direction[1]] != [tmpi, tmpj]):
for step in steps:
self.grid[step[0]][step[1]] = player
return
def oneSideNeedPass(self, player):
for i in range(self.width):
for j in range(self.width):
if (self.canPositionOn(i, j, player)):
return False # not need pass
return True
def isTerminated(self):
for i in range(self.width):
for j in range(self.width):
if (self.canPositionOn(i, j, 1) or self.canPositionOn(i, j, -1)):
return -2 # not end
return self.countWinner()
def GetGrid(self):
return self.grid