示例#1
0
def test_step_rotate_r() -> None:
    # arrange
    tetris: TetrisBase = TetrisBase(seed=0)
    board = Board(seed=0)
    board.rotate = MagicMock()
    tetris.board = board

    # act
    tetris.step(constants.ROTATE_R)

    # assert
    board.rotate.assert_called_once_with(3)
示例#2
0
def test_step_left() -> None:
    # arrange
    tetris: TetrisBase = TetrisBase(seed=0)
    board = Board(seed=0)
    board.move_left = MagicMock()
    tetris.board = board

    # act
    tetris.step(constants.LEFT)

    # assert
    board.move_left.assert_called_once()
示例#3
0
def test_step_down() -> None:
    # arrange
    tetris: TetrisBase = TetrisBase(seed=0)
    board = Board(seed=0)
    board.move_down = MagicMock()
    tetris.board = board

    # act
    tetris.step(constants.DOWN)

    # assert
    board.move_down.assert_called_once()
示例#4
0
def test_reset() -> None:
    # arrange
    tetris: TetrisBase = TetrisBase(seed=0)
    tetris.glob_info['Score'] = 128
    tetris.glob_info['Lines'] = 6

    board = Board(seed=0)
    board.get_board = MagicMock()
    board.get_board.return_value = "TEST"
    tetris.board = board

    # act
    result = tetris.reset()

    # assert
    assert result == "TEST"
    assert tetris.glob_info['Score'] == 0
    assert tetris.glob_info['Lines'] == 0
示例#5
0
def test_reset() -> None:
    # arrange
    board: Board = Board()
    board.board = np.ones((board.height, board.width))

    # act
    board.reset()

    # assert
    assert np.array_equal(board.board, np.zeros((board.height, board.width)))
示例#6
0
def test_render() -> None:
    # arrange
    tetris: TetrisBase = TetrisBase(seed=0)

    board = Board(seed=0)
    board.get_board = MagicMock()
    board.get_board.return_value = "TEST"
    tetris.board = board

    gui = Gui(20, 10)
    gui.render = MagicMock()

    tetris.gui = gui

    # act
    tetris.render()

    # assert
    gui.render.assert_called_once()
示例#7
0
def test_step_none() -> None:
    # arrange
    tetris: TetrisBase = TetrisBase(seed=0)
    board = Board(seed=0)
    board.move_down = MagicMock()
    tetris.board = board

    # act
    tetris.step(constants.NONE)
    assert tetris.last_down == 1
    tetris.step(constants.NONE)
    assert tetris.last_down == 2
    tetris.step(constants.NONE)
    assert tetris.last_down == 3
    tetris.step(constants.NONE)
    assert tetris.last_down == 4

    # assert
    board.move_down.assert_not_called()
示例#8
0
    def __init__(self, *, seed: int = 0) -> None:
        """
        Create instance of TetrisBase
        :param seed: seed to create object

        """
        self.board: Board = Board(constants.HEIGHT, constants.WIDTH, seed=seed)
        self.gui: Any = None
        self.last_down: int = 0

        self.glob_info: Dict[str, int] = {'Score': 0, 'Lines': 0}
示例#9
0
def test_move_down_placed() -> None:
    # arrange
    board: Board = Board(seed=0)

    board.next = 0
    board._next_block()
    board.position = np.array([18, 4])

    # act
    board.move_down()

    # assert
    assert np.array_equal(board.board.sum(), 4)
示例#10
0
def test_step_get_info() -> None:
    # arrange
    tetris: TetrisBase = TetrisBase(seed=0)
    board = Board(seed=0)
    board.get_board = MagicMock()
    board.move_down = MagicMock()

    board.get_board.return_value = "TEST"
    board.move_down.return_value = {"lines": 1, "reward": 50, "done": True}

    tetris.board = board

    # act
    result = tetris.step(constants.DOWN)

    # assert
    assert result == ("TEST", 50, True, {
        'Score': 50,
        'Lines': 1,
        'Next': 0,
        'NewLine': True
    })
示例#11
0
def test_move_down() -> None:
    # arrange
    board: Board = Board(seed=0)

    board.next = 0
    board._next_block()
    board.position = np.array([4, 4])
    expected = np.array([5, 4])

    # act
    board.move_down()

    # assert
    assert np.array_equal(board.position, expected)
示例#12
0
def test_rotate() -> None:
    # arrange
    board: Board = Board(seed=0)

    board.next = 2
    board._next_block()
    board.position = np.array([4, 4])
    shape = board.curr.shape

    # act
    board.rotate(1)

    # assert
    assert board.curr.shape == (shape[1], shape[0])
示例#13
0
def test_rotate_R() -> None:
    # arrange
    board: Board = Board(seed=0)

    board.next = 1
    board._next_block()
    board.position = np.array([4, 4])
    shape = board.curr.shape
    expected = expected = np.array([[1, 1, 1], [1, 0, 0]])

    # act
    board.rotate(3)

    # assert
    assert board.curr.shape == (shape[1], shape[0])
    assert np.array_equal(board.curr, expected)
示例#14
0
def test_move_down_line() -> None:
    # arrange
    board: Board = Board(seed=0)
    board.board[19:] = 1
    board.board[19][0] = 0
    board.board[19][1] = 0
    board.next = 0
    board._next_block()
    board.position = np.array([18, 0])
    expected = np.array([0, 4])

    # act
    info = board.move_down()

    # assert
    assert info['reward'] == 20
    assert info['lines'] == 1
    assert np.array_equal(board.position, expected)
示例#15
0
def test_move_fast_down_toutch() -> None:
    # arrange
    board: Board = Board(seed=0)
    board.board[19][4] = 1

    board.next = 0
    board._next_block()
    board.position = np.array([0, 4])
    board.next = 0

    expected = np.array([
        [0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
        [0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    ]).astype(float)

    # act
    board.move_fast_down()
    result = board.get_board()

    # assert
    assert expected.shape == board.get_board().shape
    assert np.array_equal(result, expected)
示例#16
0
def test_get_board() -> None:
    # arrange
    board: Board = Board(seed=0)
    board.board[19:] = 1

    board.next = 0
    board._next_block()
    board.position = np.array([5, 4])

    expected = np.array([
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
        [0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ]).astype(float)

    # act
    result = board.get_board()

    # assert
    assert expected.shape == board.get_board().shape
    assert np.array_equal(result, expected)