def test_simulation_getNextStoneId_with_removed(): curl = game.CurlingGame() curl.sim.addShooterAsInvalid() curl.sim.addShooterAsInvalid() curl.sim.addShooterAsInvalid() curl.sim.addShooterAsInvalid() r = utils.STONE_RADIUS i = simulation.getNextStoneId(curl.sim.getBoard()) assert i == 2 # for red curl.sim.addStone(c.P1_COLOR, 0, utils.HOG_LINE) i = simulation.getNextStoneId(curl.sim.getBoard()) assert i == 2 # for blue curl.sim.addStone(c.P2_COLOR, 2 * r, utils.HOG_LINE + 2 * r) i = simulation.getNextStoneId(curl.sim.getBoard()) assert i == 3 # for red curl.sim.addStone(c.P1_COLOR, 4 * r, utils.HOG_LINE + 4 * r) i = simulation.getNextStoneId(curl.sim.getBoard()) assert i == 3 # for blue
def test_gameEnded_GameNotStarted(): curl = game.CurlingGame() board = curl.getInitBoard() ended = curl.getGameEnded(board, 1) assert ended == 0
def test_5_rock_rule(): curl = game.CurlingGame() board = curl.getInitBoard() # Set up a guard next_board, next_player = curl.getNextState( board, c.P1, c.ACTION_LIST.index((-1, '3', 5))) assert next_player == c.P2 shooter = curl.sim.getStones()[0] shooter.updateGuardValue() # We normally only do this during addStone() assert shooter.is_guard # Take it out with mock.patch.object(curl.sim, 'addShooterAsInvalid', wraps=curl.sim.addShooterAsInvalid) as spy: next_board, next_player = curl.getNextState( next_board, next_player, c.ACTION_LIST.index((-1, 'control', 0))) assert spy.call_count == 1 assert next_player == c.P1 p1_stones = len(list(board_utils.get_xy_team1(next_board))) assert p1_stones == 1, "Player 1 should keep their stone." p2_stones = len(list(board_utils.get_xy_team2(next_board))) assert p2_stones == 0, "Player 2 should have had their stone removed." first_stone = curl.sim.getStones()[0] assert first_stone.is_guard
def test_getNextState_is_cached(): curl = game.CurlingGame() board = curl.getInitBoard() curl.getNextState(board, c.P1, c.ACTION_LIST.index((1, '3', 5))) curl.sim.setupBoard = mock.Mock(side_effect=UnitTestException) curl.getNextState(board, c.P1, c.ACTION_LIST.index((1, '3', 5)))
def test_gameEnded_NoStonesInPlay(): curl = game.CurlingGame() board = curl.getInitBoard() board_utils.scenario_all_out_of_play(board) ended = curl.getGameEnded(board, 1) assert ended == 0.00001 # Draw
def test_get_valid_moves(): curl = game.CurlingGame() board = curl.getInitBoard() valid = curl.getValidMoves(board, 1) assert sum(valid) < len(c.ACTION_LIST) assert sum(valid) == 2
def test_CanonicalBoard_unchanged(): curl = game.CurlingGame() original = curl.getInitBoard() original[1][1] = 1 canonical = curl.getCanonicalForm(original, 1) np.testing.assert_array_equal(canonical, original)
def test_getNextState_has_extra_data(): curl = game.CurlingGame() board = curl.getInitBoard() next_board, _ = curl.getNextState(board, c.P1, utils.getAction(1, '5', 0)) np.testing.assert_almost_equal(next_board[:, 0], [70.3, 1450, 1, 1, 81.2, 1], decimal=-1)
def test_gameEnded_HammerWinsBy2(): curl = game.CurlingGame() board = curl.getInitBoard() board_utils.configure_hammer_2_scenario(board) ended = curl.getGameEnded(board, c.P2) assert ended == 2 # Win by 2 is good
def test_guard(): curl = game.CurlingGame() board = curl.getInitBoard() curl.getNextState(board, c.P1, c.ACTION_LIST.index((-1, '3', 5))) shooter = curl.sim.getStones()[0] shooter.updateGuardValue() # We normally only do this during addStone() assert shooter.is_guard
def test_CanonicalBoard_unchanged_symmetric(): curl = game.CurlingGame() original = curl.getInitBoard() original[1][1] = 1 canonical_once = curl.getCanonicalForm(original, -1) canonical_twice = curl.getCanonicalForm(canonical_once, -1) np.testing.assert_array_equal(canonical_twice, original)
def test_it_curls_right(): curl = game.CurlingGame() board = curl.getInitBoard() curl.getNextState(board, 1, utils.getAction(1, '5', 1)) stone = curl.sim.getStones()[0] right_house = utils.dist(feet=5) assert stone.body.position.x > right_house # positive handle, positive broom, should cross over the center
def test_gameEnded_NotEndOfGame(): curl = game.CurlingGame() board = curl.getInitBoard() board_utils.scenario_all_out_of_play(board) board_utils.set_stone(board, c.P2, 7, 0, 0, c.NOT_THROWN, c.IN_PLAY) ended = curl.getGameEnded(board, 1) assert ended == 0
def test_getSymmetries_flip(): curl = game.CurlingGame() board = curl.getInitBoard() board_utils.configure_hammer_2_scenario(board) sym = curl.getSymmetries(board, 0) back = curl.getSymmetries(sym[-1][0], 0) np.testing.assert_array_equal(board, back[-1][0])
def test_getSymmetries_count(): curl = game.CurlingGame() board = curl.getInitBoard() sym = curl.getSymmetries(board, 0) assert len(sym) == 2 # Regular and flip board_utils.configure_hammer_2_scenario(board) sym = curl.getSymmetries(board, 0) # 2 stones yield (14 - 1) permutations. assert len(sym) == 28 # (13 permutations + original) * 2 for flip
def test_getNextPlayer_1(): curl = game.CurlingGame() board = curl.getInitBoard() next_board, next_player = curl.getNextState( board, c.P1, c.ACTION_LIST.index((-1, '3', 5))) total_stones = len(curl.sim.getStones()) assert total_stones == 1 assert next_player == c.P2
def test_control(): curl = game.CurlingGame() board = curl.getInitBoard() next_board, next_player = curl.getNextState( board, c.P1, c.ACTION_LIST.index((-1, 'control', 6))) p1_stones = len(list(board_utils.get_xy_team1(next_board))) assert p1_stones == 0 assert next_player == c.P2
def test_get_valid_moves_too_late(): curl = game.CurlingGame() board = curl.getInitBoard() board_utils.scenario_all_out_of_play(board) try: curl.getValidMoves(board, 1) except game.GameException: pass else: raise Exception('getValidMoves should prohibit 16 stones.')
def test_string_repr_is_symmetric(): curl = game.CurlingGame() board = curl.getInitBoard() curl.getNextState(board, c.P1, c.ACTION_LIST.index((1, '3', 5))) board_setup = curl.sim.getBoard() curl.boardFromString(curl.stringRepresentation(board)) board_check = curl.sim.getBoard() np.testing.assert_array_equal(board_setup, board_check)
def test_simulation_setupBoard_1(): curl = game.CurlingGame() board = curl.getInitBoard() sim = curl.sim sim.setupBoard(board) sim.addStone(c.P1_COLOR, 0, utils.TEE_LINE) sim.addStone(c.P2_COLOR, 0, utils.TEE_LINE) stones = list(sim.getStones()) assert len(stones) == 2
def test_getNextPlayer_4_canonical(): curl = game.CurlingGame() board = curl.getInitBoard() board[-1][0] = c.EMPTY board[-1][8] = c.EMPTY board[-1][1] = c.EMPTY board[-1][9] = c.EMPTY canon = curl.getCanonicalForm(board, c.P2) player = utils.getNextPlayer(canon, c.P2) assert player == -1
def test_gameEnded_SlightlyOffCenter_x_2(): curl = game.CurlingGame() board = curl.getInitBoard() board_utils.scenario_all_out_of_play(board) x, y = curling.constants.BUTTON_POSITION # Team 2 is winning by 1 board_utils.set_stone(board, c.P2, 7, x + 1 * inch, y) ended = curl.getGameEnded(board, 1) assert ended == -1
def test_simulation_getBoard_is_symmetric(): """Create a board then convert it to simulation and back to board.""" curl = game.CurlingGame() expected = curl.sim.getBoard() curl.sim.addStone(c.P1_COLOR, 0, utils.TEE_LINE) curl.sim.setupBoard(expected) actual = curl.sim.getBoard() actual_position = list(board_utils.get_xy_team1(actual)) expected_position = list(board_utils.get_xy_team1(expected)) np.testing.assert_array_equal(actual_position, expected_position)
def test_simulation_getBoard_has_extra_data(): curl = game.CurlingGame() expected = curl.sim.getBoard() board_utils.configure_hammer_2_scenario(expected) curl.sim.setupBoard(expected) actual = curl.sim.getBoard() # Not called during setup() because useless board_utils.update_distance_and_score(expected) np.testing.assert_array_equal(actual, expected)
def test_gameEnded_y_HammerCloser(): curl = game.CurlingGame() board = curl.getInitBoard() board_utils.scenario_all_out_of_play(board) x, y = curling.constants.BUTTON_POSITION board_utils.set_stone(board, c.P1, 7, x, y - 10 * inch) board_utils.set_stone(board, c.P2, 7, x, y + 1 * inch) curl.sim.setupBoard(board) board = curl.sim.getBoard() ended = curl.getGameEnded(board, c.P2) assert ended == 1
def test_gameEnded_edgeCase(): # Ensure the board is setup/reset before counting the stones # This edge case is when a game goes in "reverse" because of MCTS curl = game.CurlingGame() board = curl.getInitBoard() board_utils.scenario_all_out_of_play(board) board_utils.set_stone(board, c.P2, 7, 0, 0, c.NOT_THROWN, c.IN_PLAY) assert curl.getGameEnded(board, 1) == 0 board_utils.set_stone(board, c.P2, 7, 0, 0, c.THROWN, c.OUT_OF_PLAY) assert curl.getGameEnded(board, 1) != 0
def test_draw(): curl = game.CurlingGame() board = curl.getInitBoard() next_board, next_player = curl.getNextState( board, c.P1, c.ACTION_LIST.index((-1, '7', 6))) p1_stones = len(list(board_utils.get_xy_team1(next_board))) assert p1_stones == 1 assert next_player == c.P2 shooter = curl.sim.getStones()[0] shooter.updateGuardValue() # We normally only do this during addStone() assert not shooter.is_guard
def test_getNextState_cache_canonical(): curl = game.CurlingGame() board = curl.getInitBoard() p1_board, p1_next_player = curl.getNextState(board, c.P1, c.ACTION_LIST.index((1, '3', 5))) curl.sim.setupBoard = mock.Mock(side_effect=UnitTestException) p2_board, p2_next_player = curl.getNextState(board, c.P2, c.ACTION_LIST.index((1, '3', 5))) assert p1_next_player == c.P2 assert p2_next_player == c.P1 with np.testing.assert_raises(AssertionError): np.testing.assert_array_equal(p1_board, p2_board) p1_board_canon = curl.getCanonicalForm(p1_board, c.P2) np.testing.assert_array_equal(p1_board_canon[2], p2_board[2])
def test_simulation_getNextStoneId(): curl = game.CurlingGame() r = utils.STONE_RADIUS i = simulation.getNextStoneId(curl.sim.getBoard()) assert i == 0 # for red curl.sim.addStone(c.P1_COLOR, 0, utils.HOG_LINE) i = simulation.getNextStoneId(curl.sim.getBoard()) assert i == 0 # for blue curl.sim.addStone(c.P2_COLOR, 2 * r, utils.HOG_LINE + 2 * r) i = simulation.getNextStoneId(curl.sim.getBoard()) assert i == 1 # for red curl.sim.addStone(c.P1_COLOR, 4 * r, utils.HOG_LINE + 4 * r) i = simulation.getNextStoneId(curl.sim.getBoard()) assert i == 1 # for blue
def SKIP_test_simulation_getBoard_button(): sim = game.CurlingGame().sim board = sim.getBoard() board[-1][0] = c.EMPTY button_x, button_y = curling.constants.BUTTON_POSITION board_x, board_y = utils.realToBoard(button_x, button_y) board[board_x][board_y] = c.P1 sim.setupBoard(board) stones = sim.getStones() assert len(stones) == 1 stone_x, stone_y = stones[0].body.position assert (button_x, button_y) == (stone_x, stone_y) recalculated_board = sim.getBoard() expected = np.argwhere(board == c.P1) actual = np.argwhere(recalculated_board == c.P1) np.testing.assert_array_equal(expected, actual)