def test_drop_lines_single(): b = Board(10, 22) b.current_tetromino = Tetromino("O", SPAWN["O"], COLORS["O"]) b.ghost_tetromino = b.get_ghost_tetromino() for i in range(10): b.board_tetrominos_squares.append(Square(Point(i, 0), colors.ASH)) b.board_tetrominos_squares.append(Square(Point(0, 1), colors.ASH)) b.board_tetrominos_squares.append(Square(Point(1, 1), colors.ASH)) b.board_tetrominos_squares.append(Square(Point(2, 1), colors.ASH)) b.update_matrices() filled_indices = b.get_filled_indices() b.clear_lines(filled_indices) b.drop_lines(filled_indices) b.update_matrices() ghost_squares = b.ghost_tetromino.squares assert ghost_squares[0].x == 4 assert ghost_squares[0].x == 4 assert ghost_squares[0].y == 0 assert ghost_squares[1].x == 5 assert ghost_squares[1].y == 0 assert ghost_squares[2].x == 5 assert ghost_squares[2].y == 1 assert ghost_squares[3].x == 4 assert ghost_squares[3].y == 1 for i in range(3): assert b.board_tetrominos_matrix[i][0] == 1 for i in range(3, 10): assert b.board_tetrominos_matrix[i][0] == 0
def test_point_setters_exception(value, exception_type): point = Point() with pytest.raises(exception_type): point.x = value with pytest.raises(exception_type): point.y = value
def test_point_setters(): point = Point() point.x = 42 point.y = 42 assert point.x == 42.0 assert point.y == 42.0
def test_point_operators(): p1 = Point() p2 = Point() p3 = Point(1, 10) assert p1 == p2 assert not p1 == p3 assert not p1 != p2 assert p1 != p3
def test_comparison_operators(): p1 = Point() p2 = Point() p3 = Point(2.0, 4.0) assert p1 == p2 assert not p1 == p3 assert p1 != p3 assert not p1 != p2
def test_drive(): place = Point(0, 2) far_place = Point(1425, 1365) place_two = Point(1, 1) car = Car(10, 0.5, 'Renault', place) car.drive(place_two) assert car.location == place_two with pytest.raises(OutOfFuel): car.drive(far_place)
def test_setters(): point = Point() assert point.x == POINT_DEFAULT assert point.y == POINT_DEFAULT point.x = POINT_NEW_VALUE point.y = POINT_NEW_VALUE assert point.x == POINT_NEW_VALUE assert point.y == POINT_NEW_VALUE
def test_refill(): place = Point(0, 2) destination = Point(0, 10) car = Car(10, 0.5, 'Renault', place) car.drive(destination) car.refill(3) assert car.amount == 9 with pytest.raises(ValueError): car.refill(8)
def test_equals(): p1 = Point(1, 2) p2 = Point(1, 2) assert p1.equals(p2) p1 = Point(1, 2) p2 = Point(2, 1) assert not p1.equals(p2)
def test_init(): for i in range(10): for j in range(22): p = Point(i, j) assert isinstance(p, Point) assert p.x == i assert p.y == j
def test_car_construction(): place = Point(0, 2) car = Car(15, 0.5, 'Bugatti', place) assert car.capacity == 15 assert car.amount == 15 assert car.consumption == 0.5 assert car.model == 'Bugatti' assert car.location == place
def test_drive_to(): place = Point(0, 2) car = Car(10, 0.5, 'Renault', place) car.drive_to(1, 5) with pytest.raises(TypeError): car.drive_to(1, 2.5)
def test_drop_lines_multiple(): b = Board(10, 22) for i in range(10): b.board_tetrominos_squares.append(Square(Point(i, 0), colors.ASH)) b.board_tetrominos_squares.append(Square(Point(i, 2), colors.ASH)) b.board_tetrominos_squares.append(Square(Point(0, 1), colors.ASH)) b.board_tetrominos_squares.append(Square(Point(1, 1), colors.ASH)) b.board_tetrominos_squares.append(Square(Point(2, 1), colors.ASH)) b.board_tetrominos_squares.append(Square(Point(0, 3), colors.ASH)) b.board_tetrominos_squares.append(Square(Point(1, 3), colors.ASH)) b.update_matrices() filled_indices = b.get_filled_indices() b.clear_lines(filled_indices) b.drop_lines(filled_indices) b.update_matrices() for i in range(3): assert b.board_tetrominos_matrix[i][0] == 1 for i in range(3, 10): assert b.board_tetrominos_matrix[i][0] == 0 for i in range(2): assert b.board_tetrominos_matrix[i][1] == 1 for i in range(2, 10): assert b.board_tetrominos_matrix[i][1] == 0
def test_clear_lines(): b = Board(10, 22) # Set up lines that should be cleared for i in range(10): b.board_tetrominos_squares.append(Square(Point(i, 3), colors.ASH)) b.board_tetrominos_squares.append(Square(Point(i, 8), colors.ASH)) b.update_matrices() filled_indices = b.get_filled_indices() b.clear_lines(filled_indices) b.update_matrices() lines_cleared = True for i in range(10): if (b.board_tetrominos_matrix[i][3] == 1 or b.board_tetrominos_matrix[i][8] == 1): lines_cleared = False assert lines_cleared
def test_subtract(): subtract_point_list = [ Point(-1, 0), Point(+1, 0), Point(0, -1), Point(0, +1), ] for i in range(10): for j in range(22): for subtract_point in subtract_point_list: point = Point(i, j) assert point.subtract(subtract_point).equals( Point(i - subtract_point.x, j - subtract_point.y))
def test_offset(): offsets = [(1, 0), (-1, 0), (0, 1), (0, -1)] ids = ["O", "I", "J", "L", "S", "Z", "T"] for id in ids: for offset in offsets: for i in range(10): for j in range(22): t = Tetromino(id, Point(i, j), COLORS[id]) t.offset(offset[0], offset[1]) assert t.origin.x == i + offset[0] assert t.origin.y == j + offset[1]
def test_add(): add_point_list = [ Point(-1, 0), Point(+1, 0), Point(0, -1), Point(0, +1), ] for i in range(10): for j in range(22): for add_point in add_point_list: point = Point(i, j) assert point.add(add_point).equals( Point(i + add_point.x, j + add_point.y))
def offset(self, x, y): """ Move the tetromino by the given horizontal and vertical values. Args: x (int): The number of horizontal units to move (pos = right, neg = left). y (int): The number of vertical units to move (pos = up, neg = down). """ self.origin = self.origin.add(Point(x, y)) for square in self.squares: square.offset(x, y)
def test_offset(): offsets = [ (1, 0), # right (-1, 0), # left (0, 1), # up (0, -1), # down ] for offset in offsets: for i in range(10): for j in range(22): s = Square(Point(i, j), colors.ASH) s.offset(offset[0], offset[1]) assert s.x == i + offset[0] assert s.y == j + offset[1]
def test_rotate_ccw(): for id, layouts in expected_new_layouts.items(): for i in range(10): for j in range(22): t = Tetromino(id, Point(i, j), COLORS[id]) old_position = t.origin for num_rotations, layout in enumerate(reversed(layouts)): assert t.origin.x == old_position.x assert t.origin.y == old_position.y squares_tuples = get_list_tuples(t.squares) layout_offset = [] for l in layout: layout_offset.append( tuple(map(operator.add, l, (i, j)))) assert sorted(squares_tuples) == sorted(layout_offset) assert t.state == State((4 - num_rotations) % 4) t.rotate_ccw()
def test_init(): ids_colors = { "O": colors.YELLOW, "I": colors.TEAL, "J": colors.BLUE, "L": colors.ORANGE, "S": colors.GREEN, "Z": colors.RED, "T": colors.PURPLE, } for id, color in ids_colors.items(): for i in range(10): for j in range(22): t = Tetromino(id, Point(i, j), COLORS[id]) assert t.id == id assert t.origin.x == i assert t.origin.y == j assert t.state == State.ZERO assert t.color == color
def test_populate_squares(): expected_layouts = { "O": [(0, 0), (1, 0), (1, 1), (0, 1)], "I": [(0, 0), (1, 0), (2, 0), (3, 0)], "J": [(0, 0), (0, 1), (1, 0), (2, 0)], "L": [(0, 0), (1, 0), (2, 0), (2, 1)], "S": [(0, 0), (1, 0), (1, 1), (2, 1)], "Z": [(0, 1), (1, 1), (1, 0), (2, 0)], "T": [(0, 0), (1, 0), (2, 0), (1, 1)] } for id, layout in expected_layouts.items(): for i in range(10): for j in range(22): t = Tetromino(id, Point(i, j), COLORS[id]) squares_tuples = get_list_tuples(t.squares) layout_offset = [] for l in layout: layout_offset.append(tuple(map(operator.add, l, (i, j)))) assert sorted(squares_tuples) == sorted(layout_offset)
def rotate_ccw(self): """Rotate the tetromino by 90 degrees, counterclockwise.""" # the point of rotation, relative to the board origin abs_rotation_pt = self.origin.add(ROTATION_POINTS[self.id]) for i in range(len(self.squares)): # the square's position relative to the point of rotation current_square = Point( self.squares[i].x, self.squares[i].y).subtract(abs_rotation_pt) # the square's bottom right point, which will be the new # square origin after the rotation top_left = current_square.add(Point(0, 1)) # x -> y and y -> -x for rotation about the origin new_point = Point(-top_left.y, top_left.x).add(abs_rotation_pt) # replace the old square with the new square point = Point(int(new_point.x), int(new_point.y)) self.squares[i] = Square(point, self.color) self.state = self.state.prev()
def drive_to(self, x: float, y: float) -> None: x = check_coordinate(x) y = check_coordinate(y) destination = Point(x, y) self.drive(destination)
def point(self, coords): self._point = Point(coords[0], coords[1])
def test_point_constructor(x, y): point = Point(x, y) assert point.x == x assert point.y == y
def test_car_to_string(): place = Point(0, 2) car = Car(10, 0.5, 'Renault', place) assert str( car) == 'Renault : (10.00 l/10.0l, point of location: (0.0, 2.0)'
def test_point_to_string(): point = Point() assert str(point) == '(0.0, 0.0)'
def test_point_distance_exception(): with pytest.raises(TypeError): p1 = Point() p1.distance(dir)
def test_point_distance(): p1 = Point() p2 = Point(2, 4) assert p1.distance(p2) == 4.47213595499958