Skip to content

Commit 1e38280

Browse files
committed
add max steps and max illegal steps
1 parent b152ee3 commit 1e38280

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

gym_2048.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def stack(flat, layers=16):
3737

3838
class Game2048Env(gym.Env): # directions 0, 1, 2, 3 are up, right, down, left
3939
metadata = {'render.modes': ['human', 'ansi']}
40+
max_steps = 10000
4041

4142
def __init__(self):
4243
# Definitions for game. Board must be square.
@@ -56,6 +57,9 @@ def __init__(self):
5657
self.set_illegal_move_reward(0.)
5758
self.set_max_tile(None)
5859

60+
self.max_illegal = 50 # max number of illegal actions
61+
self.num_illegal = 0
62+
5963
# Initialise seed
6064
self.seed()
6165

@@ -111,8 +115,14 @@ def step(self, action):
111115
except IllegalMove as e:
112116
logging.debug("Illegal move")
113117
info['illegal_move'] = True
114-
done = False
118+
if self.steps > self.max_steps:
119+
done = True
120+
else:
121+
done = False
115122
reward = self.illegal_move_reward
123+
self.num_illegal += 1
124+
if self.num_illegal >= self.max_illegal: # exceed the maximum number of illegal actions
125+
done = True
116126

117127
info = self._get_info(info)
118128

@@ -123,6 +133,7 @@ def reset(self):
123133
self.Matrix = np.zeros((self.h, self.w), np.int)
124134
self.score = 0
125135
self.steps = 0
136+
self.num_illegal = 0
126137

127138
logging.debug("Adding tiles")
128139
self.add_tile()
@@ -272,6 +283,9 @@ def isend(self):
272283

273284
if self.max_tile is not None and self.highest() == self.max_tile:
274285
return True
286+
287+
if self.steps >= self.max_steps:
288+
return True
275289

276290
for direction in range(4):
277291
try:

0 commit comments

Comments
 (0)