diff --git a/code/bot/__pycache__/game.cpython-39.pyc b/code/bot/__pycache__/game.cpython-39.pyc index adc74c7..6550fe9 100644 Binary files a/code/bot/__pycache__/game.cpython-39.pyc and b/code/bot/__pycache__/game.cpython-39.pyc differ diff --git a/code/bot/game.py b/code/bot/game.py index 1f5bfb6..1558837 100644 --- a/code/bot/game.py +++ b/code/bot/game.py @@ -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) @@ -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 = "" diff --git a/code/bot/tests.py b/code/bot/tests.py index cb65c66..94ead07 100644 --- a/code/bot/tests.py +++ b/code/bot/tests.py @@ -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()