def test_do_not_rotate_piece_if_the_next_position_is_in_conflict(self): p = PlayerArea() p.current_piece = Piece(Piece.L_SHAPE) p.current_x = 7 blocks_before_rotate = [(0, 8), (1, 8), (2, 8), (2, 9)] for point in blocks_before_rotate: self.assertTrue(p.value_at(point[0], point[1]) > 0) p.handle_event(Event(KEYDOWN, { 'key': K_SPACE })) for point in blocks_before_rotate: self.assertTrue(p.value_at(point[0], point[1]) > 0)
def test_rotating_the_piece_clockwise(self): p = PlayerArea() p.current_piece = Piece(Piece.L_SHAPE) p.current_x = 3 p.current_y = 0 blocks_before_rotate = [(0, 4), (1, 4), (2, 4), (2, 5)] for point in blocks_before_rotate: self.assertTrue(p.value_at(point[0], point[1]) > 0) p.handle_event(Event(KEYDOWN, { 'key': K_SPACE })) blocks_after_rotate = [(1, 4), (1, 5), (1, 6), (2, 4)] for point in blocks_after_rotate: self.assertTrue(p.value_at(point[0], point[1]) > 0)
def test_blocks_in_full_line_are_marked_to_be_cleared(self): p = PlayerArea() p.current_piece = Piece(Piece.BLOCK_SHAPE) self.assertEqual(p.counter_to_clear_blocks, 0) # Fill some points along the bottom of the grid with an opening # in the middle that will be filled by the current piece. prefilled_points = [(18, 0), (18, 1), (18, 2), (18, 3), (18, 6), (18, 7), (18, 8), (18, 9), (19, 0), (19, 1), (19, 2), (19, 3), (19, 6), (19, 7), (19, 8), (19, 9)] for point in prefilled_points: p.grid[(point[0] * PlayerArea.GRID_COLUMNS) + point[1]] = 1 # Drop the current piece to the floor, which should fill in two # complete lines, so every block on the grid will be cleared out. p.handle_event(Event(KEYDOWN, { 'key': K_UP })) for row in (18, 19): for col in range(PlayerArea.GRID_COLUMNS): self.assertEqual(p.value_at(row, col), PlayerArea.BLOCK_TO_BE_CLEARED, "block should be marked at (%s, %s)" % (row, col)) self.assertEqual(p.counter_to_clear_blocks, PlayerArea.TICKS_TO_CLEAR_BLOCKS)
def test_clearing_full_lines_after_attaching_piece(self): p = PlayerArea() p.current_piece = Piece(Piece.BLOCK_SHAPE) # Fill some points along the bottom of the grid with an opening # in the middle that will be filled by the current piece. prefilled_points = [(18, 0), (18, 1), (18, 2), (18, 3), (18, 6), (18, 7), (18, 8), (18, 9), (19, 0), (19, 1), (19, 2), (19, 3), (19, 6), (19, 7), (19, 8), (19, 9)] for point in prefilled_points: p.grid[(point[0] * PlayerArea.GRID_COLUMNS) + point[1]] = 1 # Drop the current piece to the floor, which should fill in two # complete lines, so every block on the grid will be cleared out. p.handle_event(Event(KEYDOWN, { 'key': K_UP })) tick_til_lines_cleared(p) for row in range(PlayerArea.GRID_ROWS): for col in range(PlayerArea.GRID_COLUMNS): self.assertEqual(p.value_at(row, col), 0, "should be clear at point (%s, %s)" % (row, col)) self.assertEqual(p.lines_cleared, 2)
def test_clearing_the_top_line(self): p = PlayerArea() p.current_piece = Piece(Piece.BLOCK_SHAPE) for row in range(PlayerArea.GRID_ROWS): p.grid[row * PlayerArea.GRID_COLUMNS] = 1 prefilled_points = [(19, 0), (19, 1), (19, 2), (19, 3), (19, 6), (19, 7), (19, 8), (19, 9)] for point in prefilled_points: p.grid[(point[0] * PlayerArea.GRID_COLUMNS) + point[1]] = 1 p.handle_event(Event(KEYDOWN, { 'key': K_UP })) tick_til_lines_cleared(p) self.assertEqual(p.value_at(0, 0), 0) self.assertTrue(p.value_at(1, 0) > 0)
def test_getting_the_value_on_the_grid(self): p = PlayerArea() p.current_piece = Piece(Piece.BLOCK_SHAPE) p.current_y = 0 p.current_x = p.INITIAL_X # Assign some random values on the grid. for point in [(0, 9), (5, 8), (6, 2), (13, 0)]: index = (point[0] * p.GRID_COLUMNS) + point[1] p.grid[index] = 1 occupied_points = [(0, 9), (5, 8), (6, 2), (13, 0), (0, 4), (0, 5), (1, 4), (1, 5)] empty_points = [(0, 0), (0, 3), (0, 6), (1, 3), (1, 6), (10, 7), (7, 8), (15, 2), (19, 9)] for point in occupied_points: self.assertTrue(p.value_at(point[0], point[1]) > 0, "(%s, %s) does not contain a value" % (point[0], point[1])) for point in empty_points: self.assertEqual(p.value_at(point[0], point[1]), 0, "(%s, %s) contains a value" % (point[0], point[1]))
def test_clearing_line_should_drop_lines_above(self): """ When clearing a line, all of the lines above it should be shifted down a row. """ p = PlayerArea() p.current_piece = Piece(Piece.BLOCK_SHAPE) prefilled_points = [(19, 0), (19, 1), (19, 2), (19, 3), (19, 6), (19, 7), (19, 8), (19, 9)] for point in prefilled_points: p.grid[(point[0] * PlayerArea.GRID_COLUMNS) + point[1]] = 1 p.handle_event(Event(KEYDOWN, { 'key': K_UP })) tick_til_lines_cleared(p) self.assertEqual(p.value_at(18, 4), 0, "block should not exist at (18, 4)") self.assertEqual(p.value_at(18, 5), 0, "block should not exist at (18, 5)") self.assertTrue(p.value_at(19, 4) > 0, "block should exist at (19, 4)") self.assertTrue(p.value_at(19, 5) > 0, "block should exist at (19, 5)") self.assertEqual(p.lines_cleared, 1)
def test_dropping_a_piece_to_the_floor_should_attach_to_grid(self): p = PlayerArea() p.current_piece = Piece(Piece.BLOCK_SHAPE) p.current_y = 0 p.handle_event(Event(KEYDOWN, { 'key': K_UP })) self.assertIsNone(p.current_piece, "should have attached the piece to the grid") filled_points = [(18, 4), (18, 5), (19, 4), (19, 5)] for point in filled_points: self.assertTrue(p.value_at(point[0], point[1]) > 0, "(%s, %s) block should be filled" % (point[0], point[1]))
def test_that_pieces_stack_on_each_other(self): p = PlayerArea() # Drop two pieces consecutively. for i in range(2): p.current_piece = Piece(Piece.BLOCK_SHAPE) p.current_y = 0 p.handle_event(Event(KEYDOWN, { 'key': K_UP })) filled_points = [(18, 4), (18, 5), (19, 4), (19, 5), (16, 4), (16, 5), (17, 4), (17, 5)] for point in filled_points: self.assertTrue(p.value_at(point[0], point[1]) > 0, "(%s, %s) block should be filled" % (point[0], point[1]))