예제 #1
0
 def test_is_legal_move_returns_true_for_the_pie_rule2(self):
     env = MancalaEnv()
     env.perform_move(Move(Side.SOUTH, 5))
     self.assertTrue(env.is_legal(Move(Side.NORTH, 0)))
예제 #2
0
def _run_game(player: Player, state: MancalaEnv):
    our_agent_states = []
    their_agent_states = []
    both_agent_states = []
    our_side = Side.SOUTH
    while True:
        msg = protocol.read_msg()
        try:
            msg_type = protocol.get_msg_type(msg)
            if msg_type == MsgType.START:
                first = protocol.interpret_start_msg(msg)
                if first:
                    move = player.get_play(state)
                    protocol.send_msg(protocol.create_move_msg(move.index))
                else:
                    our_side = Side.NORTH
            elif msg_type == MsgType.STATE:
                move_turn = protocol.interpret_state_msg(msg)
                if move_turn.move == 0:
                    our_side = Side.opposite(our_side)

                move_to_perform = Move(state.side_to_move, move_turn.move)

                observed_state = ObservedState(state=state,
                                               action_taken=move_to_perform)
                both_agent_states.append(observed_state)
                if state.side_to_move == our_side:
                    our_agent_states.append(observed_state)
                else:
                    their_agent_states.append(observed_state)

                state.perform_move(move_to_perform)
                if not move_turn.end:
                    if move_turn.again:
                        move = player.get_play(state)
                        # pie rule; optimal move is to swap
                        if move.index == 0:
                            protocol.send_msg(protocol.create_swap_msg())
                        else:
                            protocol.send_msg(
                                protocol.create_move_msg(move.index))

            elif msg_type == MsgType.END:
                args = parser.parse_args()
                run_id = '%06d' % args.run_number
                run_category = args.category

                _our_agent_file_path = _checkpoint_file_path + "/our-agent/" + run_category + run_id
                _their_agent_file_path = _checkpoint_file_path + "/their-agent/" + run_category + run_id
                _both_agent_file_path = _checkpoint_file_path + "/both-agent/" + run_category + run_id

                np.save(file=_our_agent_file_path,
                        arr=np.array(our_agent_states))
                np.save(file=_their_agent_file_path,
                        arr=np.array(their_agent_states))
                np.save(file=_both_agent_file_path,
                        arr=np.array(both_agent_states))
                break
            else:
                print("Not sure what I got " + str(msg_type))
        except InvalidMessageException as _e:
            print(str(_e))
예제 #3
0
class TestMancalaGameState(unittest.TestCase):
    def setUp(self):
        self.game = MancalaEnv()

    def test_initial_state_is_correct(self):
        self.assertEqual(self.game.side_to_move, Side.SOUTH)
        self.assertFalse(self.game.north_moved)

        for hole in range(1, self.game.board.holes + 1):
            self.assertEqual(self.game.board.get_seeds(Side.SOUTH, hole), 7)
            self.assertEqual(self.game.board.get_seeds(Side.NORTH, hole), 7)
        self.assertEqual(self.game.board.get_seeds_in_store(Side.SOUTH), 0)
        self.assertEqual(self.game.board.get_seeds_in_store(Side.NORTH), 0)

    def test_cloning_immutability(self):
        clone = MancalaEnv.clone(self.game)
        self.game.perform_move(Move(Side.SOUTH, 3))

        self.assertEqual(clone.board.get_seeds(Side.SOUTH, 3), 7)
        self.assertEqual(clone.side_to_move, Side.SOUTH)

    def test_move_has_required_effects(self):
        self.game.perform_move(Move(Side.SOUTH, 5))
        self.assertEqual(self.game.board.get_seeds(Side.SOUTH, 5), 0)
        self.assertEqual(self.game.board.get_seeds(Side.SOUTH, 6), 8)
        self.assertEqual(self.game.board.get_seeds(Side.SOUTH, 7), 8)
        self.assertEqual(self.game.board.get_seeds_in_store(Side.SOUTH), 1)
        self.assertEqual(self.game.board.get_seeds(Side.NORTH, 1), 8)
        self.assertEqual(self.game.board.get_seeds(Side.NORTH, 2), 8)
        self.assertEqual(self.game.board.get_seeds(Side.NORTH, 3), 8)
        self.assertEqual(self.game.board.get_seeds(Side.NORTH, 4), 8)

        self.game.perform_move(Move(Side.NORTH, 4))
        self.assertEqual(self.game.board.get_seeds(Side.NORTH, 4), 0)
        self.assertEqual(self.game.board.get_seeds(Side.NORTH, 5), 8)
        self.assertEqual(self.game.board.get_seeds(Side.NORTH, 6), 8)
        self.assertEqual(self.game.board.get_seeds(Side.NORTH, 7), 8)
        self.assertEqual(self.game.board.get_seeds_in_store(Side.NORTH), 1)
        self.assertEqual(self.game.board.get_seeds(Side.SOUTH, 1), 8)
        self.assertEqual(self.game.board.get_seeds(Side.SOUTH, 2), 8)
        self.assertEqual(self.game.board.get_seeds(Side.SOUTH, 3), 8)

    def test_game_is_over_returns_false(self):
        self.assertFalse(self.game.is_game_over())

    def test_game_is_over_returns_true(self):
        board = self.game.board
        for hole in range(1, board.holes + 1):
            board.set_seeds(Side.SOUTH, hole, 0)
        board.set_seeds_in_store(Side.SOUTH, 49)
        self.assertTrue(self.game.is_game_over())

    def test_game_returns_winner_the_player_with_most_seeds(self):
        board = self.game.board
        for hole in range(1, self.game.board.holes + 1):
            board.set_seeds(Side.SOUTH, hole, 0)
            board.set_seeds(Side.NORTH, hole, 0)
        board.set_seeds_in_store(Side.SOUTH, 23)
        board.set_seeds_in_store(Side.NORTH, 21)

        self.assertEqual(self.game.get_winner(), Side.SOUTH)

    def test_game_returns_no_winner_if_players_have_equal_number_of_seeds(self):
        board = self.game.board
        for hole in range(1, self.game.board.holes + 1):
            board.set_seeds(Side.SOUTH, hole, 0)
            board.set_seeds(Side.NORTH, hole, 0)
        board.set_seeds_in_store(Side.SOUTH, 30)
        board.set_seeds_in_store(Side.NORTH, 30)

        self.assertEqual(self.game.get_winner(), None)

    def test_is_legal_move_returns_true_for_the_pie_rule(self):
        board = self.game.board
        MancalaEnv.make_move(board, Move(Side.SOUTH, 6), False)
        self.assertTrue(MancalaEnv.is_legal_action(board, Move(Side.NORTH, 0), False))

    def test_is_legal_move_returns_true_for_the_pie_rule2(self):
        env = MancalaEnv()
        env.perform_move(Move(Side.SOUTH, 5))
        self.assertTrue(env.is_legal(Move(Side.NORTH, 0)))

    def test_legal_moves_contains_all_moves(self):
        self.assertEqual(len(set(self.game.get_legal_moves())), 7)
        self.game.perform_move(Move(Side.SOUTH, 3))
        self.assertEqual(len(set(self.game.get_legal_moves())), 8)

    def test_side_to_move_doesnt_change(self):
        self.game.perform_move(Move(Side.SOUTH, 1))
        self.assertEqual(self.game.side_to_move, Side.NORTH)

    def test_alphbeta(self):
        board = self.game.board
        for hole in range(1, self.game.board.holes + 1):
            board.set_seeds(Side.SOUTH, hole, 0)
            board.set_seeds(Side.NORTH, hole, 0)
        board.set_seeds_in_store(Side.SOUTH, 0)
        board.set_seeds_in_store(Side.NORTH, 0)

        board.set_seeds(Side.SOUTH, 3, 1)
        board.set_seeds(Side.SOUTH, 2, 4)
        board.set_seeds_op(Side.SOUTH, 4, 5)
        print(self.game)
        print(search_action(self.game))