-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug fixes #3
Open
h0rban
wants to merge
5
commits into
activatedgeek:master
Choose a base branch
from
h0rban:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−65
Open
Bug fixes #3
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,5 @@ venv.bak/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,35 +6,34 @@ | |
|
||
class Base2048Env(gym.Env): | ||
metadata = { | ||
'render.modes': ['human'], | ||
'render.modes': ['human'], | ||
} | ||
|
||
## | ||
# NOTE: Don't modify these numbers as | ||
# they define the number of | ||
# anti-clockwise rotations before | ||
# applying the left action on a grid | ||
# | ||
# NOTE: Don't modify these numbers as they define the number of | ||
# anti-clockwise rotations before applying the left action on a grid | ||
|
||
LEFT = 0 | ||
UP = 1 | ||
RIGHT = 2 | ||
DOWN = 3 | ||
|
||
ACTION_STRING = { | ||
LEFT: 'left', | ||
UP: 'up', | ||
RIGHT: 'right', | ||
DOWN: 'down', | ||
LEFT: 'left', | ||
UP: 'up', | ||
RIGHT: 'right', | ||
DOWN: 'down', | ||
} | ||
|
||
def __init__(self, width=4, height=4): | ||
self.width = width | ||
self.height = height | ||
|
||
self.observation_space = spaces.Box(low=2, | ||
high=2**32, | ||
shape=(self.width, self.height), | ||
dtype=np.int64) | ||
self.observation_space = spaces.Box( | ||
low=2, | ||
high=2 ** (1 + self.width * self.height), | ||
shape=(self.width, self.height), | ||
dtype=np.int64 | ||
) | ||
self.action_space = spaces.Discrete(4) | ||
|
||
# Internal Variables | ||
|
@@ -77,8 +76,7 @@ def is_done(self): | |
|
||
return True | ||
|
||
|
||
def reset(self): | ||
def reset(self, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make parameters match the base method in class Env |
||
"""Place 2 tiles on empty board.""" | ||
|
||
self.board = np.zeros((self.width, self.height), dtype=np.int64) | ||
|
@@ -97,27 +95,24 @@ def _sample_tiles(self, count=1): | |
choices = [2, 4] | ||
probs = [0.9, 0.1] | ||
|
||
tiles = self.np_random.choice(choices, | ||
size=count, | ||
p=probs) | ||
tiles = self.np_random.choice(choices, size=count, p=probs) | ||
return tiles.tolist() | ||
|
||
def _sample_tile_locations(self, board, count=1): | ||
"""Sample grid locations with no tile.""" | ||
|
||
zero_locs = np.argwhere(board == 0) | ||
zero_indices = self.np_random.choice( | ||
len(zero_locs), size=count) | ||
zero_indices = self.np_random.choice(len(zero_locs), size=count) | ||
|
||
zero_pos = zero_locs[zero_indices] | ||
zero_pos = list(zip(*zero_pos)) | ||
return zero_pos | ||
return zero_pos.tolist() | ||
|
||
def _place_random_tiles(self, board, count=1): | ||
if not board.all(): | ||
tiles = self._sample_tiles(count) | ||
tile_locs = self._sample_tile_locations(board, count) | ||
board[tile_locs] = tiles | ||
for (x, y), tile in zip(tile_locs, tiles): | ||
board[x, y] = tile | ||
|
||
def _slide_left_and_merge(self, board): | ||
"""Slide tiles on a grid to the left and merge.""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
accurate maximum tile depending on the board size