-
Notifications
You must be signed in to change notification settings - Fork 0
/
board_env.py
171 lines (139 loc) · 4.62 KB
/
board_env.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
import numpy as np
class Board():
def __init__(self, col_size=19, line_size=19):
self.col_size = col_size
self.line_size = line_size
self.board = np.zeros((self.line_size, self.col_size), dtype="int32")
def reset(self):
self.board = np.zeros((self.line_size, self.col_size), dtype="int32")
def show_board(self):
table = ""
for line in self.board:
output = ""
for col in line:
if col == 0:
output += " +"
else:
output += " "+str(col)
table += output + "\n"
return table
def put_stone(self, line_num, col_num, player):
self.board[line_num][col_num] = player
class Gomoku():
def __init__(self, col_size=19, line_size=19, length=5):
self.col_size = col_size
self.line_size = line_size
self.board = Board(self.col_size, self.line_size)
self.steps = self.line_size * self.col_size
self.length = length
self.winner_rate = line_size*col_size
self.count_rate = 0.2
self.record = []
def actions(self):
actions = []
for i in range(self.line_size):
for j in range(self.col_size):
actions.append([i, j])
return actions
def step(self, line, col, player, rec=True):
self.steps -= 1
done = False
retry= False
reward = 0
if self.steps < 0:
done = True
else:
retry = self.action(line, col, player)
wins, counts = self.victory_check(line, col)
reward = wins
if wins >= 1:
done = True
if retry:
self.steps += 1
else:
if rec:
self.record.append([line, col, player])
return reward, done, retry
def back_step(self):
print(self.record)
if len(self.record) > 0:
self.steps += 1
self.record.pop(-1)
self.reset()
for line, col, player in self.record:
self.step(line, col, player, False)
def action(self, line_num, col_num, player=1):
retry = False
if self.board.board[line_num][col_num] == 0:
self.board.put_stone(line_num, col_num, player)
else:
## print("cant put here!")
retry = True
return retry
def victory_check(self, line_num, col_num):
player = self.board.board[line_num][col_num]
wins = 0
counts = 0
col_num_min = col_num - (self.length - 1)
line_num_min = line_num - (self.length - 1)
## - horizontal
count= 0
for i in range(self.length*2-1):
col = col_num_min + i
line = line_num
count, win = self.judge(line, col, player, count)
wins += win
counts += count - 1
## | vertical
count= 0
for i in range(self.length*2-1):
col = col_num
line = line_num_min + i
count, win = self.judge(line, col, player, count)
wins += win
counts += count - 1
## \ stanting : upper-right to left
count= 0
for i in range(self.length*2-1):
col = col_num_min + i
line= line_num_min + i
count, win = self.judge(line, col, player, count)
wins += win
counts += count - 1
## / slanting : upper-left to right
count= 0
col_num_max = col_num + (self.length - 1)
for i in range(self.length*2-1):
col = col_num_max - i
line= line_num_min + i
count, win = self.judge(line, col, player, count)
wins += win
counts += count - 1
return wins, counts
def judge(self, line, col, player, count):
win = 0
if col<0 or self.col_size<=col or line<0 or self.line_size<=line:
## Out of the board
pass
else:
if self.board.board[line][col] == player:
count += 1
else:
count = 0
if count >= self.length:
win = 1
return count, win
def reset(self):
self.board.reset()
self.steps = self.line_size * self.col_size
# hoge = Board(3,3,3)
# hoge.action(1,1,2)
# hoge.action(1,0,1)
# hoge.action(0,0,2)
# hoge.action(2,2,1)
# hoge.action(0,1,2)
# hoge.action(2,1,1)
# hoge.action(0,2,2)
# hoge.show_board()
# hoge.reset()
# hoge.show_board()