Skip to content

Commit

Permalink
hard drop
Browse files Browse the repository at this point in the history
  • Loading branch information
aeblyve committed Dec 3, 2021
1 parent fcd52c1 commit 9742beb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Binary file modified code/bot/__pycache__/game.cpython-39.pyc
Binary file not shown.
14 changes: 12 additions & 2 deletions code/bot/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ def clear_rows(grid):
x_dimension = len(grid[0])
blank_row = tuple([BLANK_LABEL] * x_dimension)
filtered = list(filter(lambda x: not row_is_full(x), grid))
# prepend rows until match y_dimension

for _ in range(y_dimension - len(filtered)):
filtered.insert(0, blank_row)
return tuple(filtered)
Expand Down Expand Up @@ -182,6 +180,18 @@ def lock_piece(self, randomizer):
else:
return CheeseState(self.spawn, self.anchor, self.piece, self.grid)

def hard_drop(self, randomizer):
y_delta = float("inf")

for x, y, label in self.piece:
for y_i in range(y, len(self.grid)):
if self.grid[y_i][x] != BLANK_LABEL and not y < 0:
y_delta = min(y_delta, y_i - y)
break

new_state = self.move_anchor(0, y_delta - 1)
return new_state.lock_piece(randomizer)

def __repr__(self):
rep = []
st = ""
Expand Down
27 changes: 27 additions & 0 deletions code/bot/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ def test_lock(self):
state = state.lock_piece(simple_randomizer)
print(state)

def test_drop(self):
simple_grid = (
(BLANK_LABEL, BLANK_LABEL),
(BLANK_LABEL, BLANK_LABEL),
(BLANK_LABEL, "b"),
(BLANK_LABEL, "b"),
("b", BLANK_LABEL),
)

simple_piece = (
(0, 0, "b"),
(0, -1, "b"),
)

new_piece = (
# it's reD!
(0, 0, "r"),
(0, -1, "r"),
)

simple_randomizer = game.SimpleRandomizer([new_piece])

state = game.CheeseState((0, 0), (0, 0), simple_piece, simple_grid)

state = state.hard_drop(simple_randomizer)
print(state)


if __name__ == "__main__":
unittest.main()

0 comments on commit 9742beb

Please sign in to comment.