-
Notifications
You must be signed in to change notification settings - Fork 0
/
triple_game.py
307 lines (261 loc) · 9.36 KB
/
triple_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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import math
import gym
from gym import spaces, logger
from gym.utils import seeding
import numpy as np
import random
ACT_UP = 0
ACT_DOWN = 1
ACT_LEFT = 2
ACT_RIGHT = 3
class TripleEnv(gym.Env):
def __init__(self):
self.rowMax = 7
self.columnMax = 7
self.colourMax = 3
self.colours = [[0 for i in range(self.rowMax)] for j in range(self.columnMax)]
self.reset()
self.score = 0
def step(self, row, column, act):
done = False
if (self.CheckActionValid(row, column, act)):
reward = 1
colour = self.GetColour(row, column)
targetRow = -1
targetColumn = -1
if act == ACT_UP:
targetRow = row - 1
targetColumn = column
elif act == ACT_DOWN:
targetRow = row + 1
targetColumn = column
elif act == ACT_LEFT:
targetRow = row
targetColumn = column - 1
elif act == ACT_RIGHT:
targetRow = row
targetColumn = column + 1
else:
return False
tarGetColour = self.GetColour(targetRow, targetColumn)
self.SetColour(row, column, tarGetColour)
self.SetColour(targetRow, targetColumn, colour)
self.EnsureValid()
self.score += 1
if self.score >= 50:
done = True
self.score = 50
else:
reward = -1
return self.colours, reward, done, {}
def reset(self):
self.ColourReset()
self.EnsureValid()
return self.colours
def render(self):
print(" ", end='')
for k in range(0, self.columnMax):
print(k, end='')
print(" ", end='')
for i in range(0, self.rowMax):
print("\n")
print(i, end='')
print(" ", end='')
for j in range(0, self.columnMax):
colour = self.GetColour(i, j)
if colour == 1:
s = "☆"
elif colour == 2:
s = "○"
elif colour == 3:
s = "□"
elif colour == 4:
s = "△"
elif colour == 5:
s = "●"
else:
s = "X"
print(s, end='')
print("\n")
def GetColour(self, row, column):
if row < 0 or column <0 or row >= self.rowMax or column >= self.columnMax:
return -1
else:
return self.colours[row][column]
def SetColour(self, row, column, colour):
if row < 0 or column <0 or row >= self.rowMax or column >= self.columnMax:
return -1
else:
self.colours[row][column] = colour
return colour
def CheckActionValid(self, row, column, act):
colour = self.GetColour(row, column)
if colour == -1:
return True
if (act == ACT_UP):
# OOA
# BBO
row1 = row - 1
column1 = column - 2
column2 = column - 1
if (self.GetColour(row1, column1) == colour and self.GetColour(row1, column2) == colour):
return True
# OAO
# BOB
row1 = row - 1
column1 = column - 1
column2 = column + 1
if (self.GetColour(row1, column1) == colour and self.GetColour(row1, column2) == colour):
return True
# AOO
# OBB
row1 = row - 1
column1 = column + 1
column2 = column + 2
if (self.GetColour(row1, column1) == colour and self.GetColour(row1, column2) == colour):
return True
# AOB
# BOA
# ABB
# BOB
row1 = row - 3
row2 = row - 2
column1 = column
if (self.GetColour(row1, column1) == colour and self.GetColour(row2, column1) == colour):
return True
elif (act == ACT_DOWN):
# BBO
# OOA
row1 = row + 1
column1 = column - 1
column2 = column - 2
if (self.GetColour(row1, column1) == colour and self.GetColour(row1, column2) == colour):
return True
# BOB
# OAO
row1 = row + 1
column1 = column - 1
column2 = column + 1
if (self.GetColour(row1, column1) == colour and self.GetColour(row1, column2) == colour):
return True
# OBB
# AOO
row1 = row + 1
column1 = column + 2
column2 = column + 1
if (self.GetColour(row1, column1) == colour and self.GetColour(row1, column2) == colour):
return True
# BOA
# ABB
# AOB
# BOA
row1 = row + 2
row2 = row + 3
column1 = column
if (self.GetColour(row1, column1) == colour and self.GetColour(row2, column1) == colour):
return True
elif (act == ACT_LEFT):
# OB
# OB
# AO
row1 = row - 1
row2 = row - 2
column1 = column - 1
if (self.GetColour(row1, column1) == colour and self.GetColour(row2, column1) == colour):
return True
# OB
# AO
# OB
row1 = row - 1
row2 = row + 1
column1 = column - 1
if (self.GetColour(row1, column1) == colour and self.GetColour(row2, column1) == colour):
return True
# AO
# OB
# OB
row1 = row + 1
row2 = row + 2
column1 = column - 1
if (self.GetColour(row1, column1) == colour and self.GetColour(row2, column1) == colour):
return True
# OOBO
row1 = row
column1 = column - 3
column2 = column - 2
if (self.GetColour(row1, column1) == colour and self.GetColour(row1, column2) == colour):
return True
elif (act == ACT_RIGHT):
# BO
# BO
# OA
row1 = row - 1
row2 = row - 2
column1 = column + 1
if (self.GetColour(row1, column1) == colour and self.GetColour(row2, column1) == colour):
return True
# BO
# OA
# BO
row1 = row + 1
row2 = row - 1
column1 = column + 1
if (self.GetColour(row1, column1) == colour and self.GetColour(row2, column1) == colour):
return True
# OA
# BO
# BO
row1 = row + 1
row2 = row + 2
column1 = column + 1
if (self.GetColour(row1, column1) == colour and self.GetColour(row2, column1) == colour):
return True
# OBOO
row1 = row
column1 = column + 2
column2 = column + 3
if (self.GetColour(row1, column1) == colour and self.GetColour(row1, column2) == colour):
return True
else:
return False
def GetRandomColour(self):
return random.randint(1, self.colourMax)
def ColourReset(self):
for i in range(0, self.rowMax):
for j in range(0, self.columnMax):
self.SetColour(i, j, self.GetRandomColour())
# 直接暴力循环处理已经形成三连的元素(直接以每个元素为中心,判断四周即可)
def ProcessTriple(self):
# 保证不存在已经三连的元素
find = True
while find:
find = False
for i in range(0, self.rowMax):
for j in range(0, self.columnMax):
colour = self.GetColour(i, j)
if self.GetColour(i - 1, j) == colour and self.GetColour(i + 1, j) == colour:
self.SetColour(i, j, self.GetRandomColour())
self.SetColour(i - 1, j, self.GetRandomColour())
self.SetColour(i + 1, j, self.GetRandomColour())
find = True
elif self.GetColour(i, j - 1) == colour and self.GetColour(i, j + 1) == colour:
self.SetColour(i, j, self.GetRandomColour())
self.SetColour(i, j - 1, self.GetRandomColour())
self.SetColour(i, j + 1, self.GetRandomColour())
find = True
# 直接暴力循环检查是否有通过一步即可形成三连的地方
def CheckTripleValid(self):
for i in range(0, self.rowMax):
for j in range(0, self.columnMax):
for act in range(ACT_UP, ACT_RIGHT):
if self.CheckActionValid(i, j, act):
return True
return False
# 形成既没有已经三连的情况,又有单步可消除的环境
def EnsureValid(self):
while True:
self.ProcessTriple()
if not self.CheckTripleValid():
self.ColourReset()
else:
break