forked from yangrc1234/Gomoku-Zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
82 lines (74 loc) · 2.36 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
import numpy as np
from enum import Enum
from collections import namedtuple
#game_state.board stores the position
#-1 means there is white, 1 is black, 0 means nothing.
class game_state:
def __init__(self,width, playSide = -1):
self.width = width
self.board = np.zeros((width,width))
self.playerSide = playSide
self.finished = False
self.winner = 0
def play(self,x,y):
assert(not self.finished)
if self.board[x][y] != 0:
return False
self.board[x][y] = self.playerSide
self._check_renju(x,y)
self.playerSide *= -1
return True
def _check_renju(self,x,y):
assert(self.board[x][y] != 0)
color = self.board[x][y]
direction = (
(0,1),
(1,0),
(1,1),
(-1,1)
)
def _checkBound(x,y):
return x >= 0 and y >= 0 and x < self.width and y < self.width
for dir in direction:
counter = 1
for reverse in (1,-1):
ite = 0
while True:
ite+=1
tp = (x + ite * dir[0] * reverse, y + ite * dir[1] * reverse)
if _checkBound(tp[0],tp[1]) and self.board[tp[0]][tp[1]] == color:
counter+= 1
else:
break
if counter >= 5:
self.finished=True
self.winner = color
break
def copy(self):
copy = game_state(self.width)
copy.board = np.copy(self.board)
copy.playerSide = self.playerSide
return copy
def print_beautiful(self):
result = ''
for x in range(width):
for y in range(width):
if self.board[x][y] == 1 :
result += '●'
if self.board[x][y] == -1:
result += '○'
if self.board[x][y] == 0:
result += '╋'
result += '\n'
return result
def test():
import configs.mini as mconfig
testSub = game_state(mconfig.MainConfig().common.game_board_size)
assert(testSub.playerSide == -1)
for i in range(4):
testSub.play(i,0)
testSub.play(i,5)
testSub.play(4,0)
print(testSub.print_beautiful())
if __name__ == '__main__' :
test()