Ejemplo n.º 1
0
    def test_should_add_object_when_object_within_bounds(self):  # noqa: D102
        grid = TetrisGrid(10)
        # Single element at (5, 5) should fit within wall bounds
        layout = Layout([Position(0, 0)])
        object = TetrisPiece(layout, Position(5, 5))

        grid.add_object(object)
Ejemplo n.º 2
0
    def test_should_should_raise_conflict_exception_when_attempting_to_add_overlapping_object(
            self):  # noqa: D102
        grid = TetrisGrid(10)
        layout = Layout([Position(0, 0)])
        object = TetrisPiece(layout, Position(5, 5))

        grid.add_object(object)
        with self.assertRaises(ElementConflictException):
            grid.add_object(object)
Ejemplo n.º 3
0
    def test_should_should_raise_out_of_bounds_exception_when_attempting_to_add_object_overlapping_wall(
            self):  # noqa: D102
        grid = TetrisGrid(10)
        # Single element at (0, 0) overlaps with floor
        layout = Layout([Position(0, 0)])
        object = TetrisPiece(layout, Position(0, 0))

        with self.assertRaises(ElementOutOfBoundsException):
            grid.add_object(object)
Ejemplo n.º 4
0
    def play(self):
        """Play a game of Tetris."""
        game_over = False
        grid = TetrisGrid(GRID_SIZE)
        piece_factory = TetrisPieceFactory()
        self.user_interface.render_hello_message()

        try:
            active_piece = self._add_new_piece_to_grid(grid, piece_factory)
            self.user_interface.render_grid(grid)

            while not game_over:
                grid.remove_object(active_piece)

                # Check if piece can be be moved by player in-place
                # Get player move and add piece in new position if valid move
                moved = False
                if grid.object_has_valid_move(active_piece):
                    movement_type = self.user_interface.get_player_move()
                    if movement_type is not MovementType.NONE:
                        moved_piece = active_piece.moved(movement_type)

                        if grid.can_add_object(moved_piece):
                            active_piece = moved_piece
                            moved = True

                # Check if piece can be moved down
                # Check if piece can still be added after moving downwards after move
                # TODO: need to check fractions of downward move when MOVE_UNITS > 1
                moved_piece = active_piece.moved_down()
                if grid.can_add_object(moved_piece):
                    active_piece = moved_piece
                    moved = True

                grid.add_object(active_piece)

                if not moved:
                    active_piece = self._add_new_piece_to_grid(
                        grid, piece_factory)

                self.user_interface.render_grid(grid)

        except GameOverException as e:
            self.user_interface.render_game_over_message(grid)