コード例 #1
0
ファイル: exact.py プロジェクト: zakizadeh/mancala
    def _move(self, game):
        '''Return move which ends in score hole'''
        self._idx = self._idx + 1
        random.seed(self._seed + self._idx)
        game_clone, rot_flag = game.clone_turn()

        move_options = Agent.valid_indices(game_clone)

        distance = list(
            map(
                lambda move_slot: AgentExact._hole_to_score(
                    move_slot, game_clone.clone()), move_options))

        available_scores = list(
            map(
                lambda move_slot: AgentExact._score_of_move(
                    move_slot, game_clone.clone()), move_options))

        final_options = [
            move for exact, move in zip(distance, move_options) if exact == 1
        ]

        if not final_options:
            score_max = max(available_scores)
            final_options = [
                move for score, move in zip(available_scores, move_options)
                if score == score_max
            ]
            final_move = Game.rotate_board(rot_flag,
                                           random.choice(final_options))
        else:
            final_move = Game.rotate_board(rot_flag, max(final_options))

        return final_move
コード例 #2
0
ファイル: test_agent_exact.py プロジェクト: zakizadeh/mancala
 def test_move_p2(self):
     '''Test move correctly picks 2nd player move'''
     game = Game()
     game.move(5)
     self.assertEqual(game.turn_player(), 2)
     move = AgentExact().move(game)
     self.assertEqual(move, 8)
コード例 #3
0
 def test_player1(self):
     for i in range(0, 6):
         self.assertTrue(Game.idx_player_1(i))
     for i in range(-50, 0):
         self.assertFalse(Game.idx_player_1(i))
     for i in range(6, 200):
         self.assertFalse(Game.idx_player_1(i))
コード例 #4
0
 def test_move_p2(self):
     """Test move picks a max move for player 2"""
     game = Game()
     game.move(4)
     self.assertEqual(game.turn_player(), 2)
     move = AgentMax().move(game)
     self.assertEqual(move, 10)
コード例 #5
0
 def test_player2(self):
     for i in range(-20, 7):
         self.assertFalse(Game.idx_player_2(i))
     for i in range(7, 13):
         self.assertTrue(Game.idx_player_2(i))
     for i in range(13, 200):
         self.assertFalse(Game.idx_player_2(i))
コード例 #6
0
    def test_random_move_02(self):
        """Test multiple moves"""
        game = Game()
        agent = AgentRandom(454)
        move = agent.move(game)
        self.assertEqual(move, 5)
        game.move(move)

        move = agent.move(game)
        self.assertEqual(move, 12)
コード例 #7
0
ファイル: test_game.py プロジェクト: Leonie-/mancala-python
    def test_player_one_takes_a_turn(self):
        current_player = 1
        mock_mancala_board = mock.Mock()
        mock_player_one = mock.Mock()
        mock_player_two = mock.Mock()
        mock_mancala_board.game_over.return_value = True
        mock_player_one.play.return_value = False

        game = Game(mock_mancala_board, mock_player_one, mock_player_two)
        game.play(current_player)
        assert mock_player_one.play.call_count == 1
コード例 #8
0
ファイル: test_game.py プロジェクト: Leonie-/mancala-python
    def test_player_one_takes_an_extra_turn(self):
        current_player = 1
        mock_mancala_board = mock.Mock()
        mock_player_one = mock.Mock()
        mock_player_two = mock.Mock()
        mock_mancala_board.game_over.side_effect = [False, True]
        mock_player_one.play.side_effect = [True, False]

        game = Game(mock_mancala_board, mock_player_one, mock_player_two)
        game.play(current_player)

        assert mock_player_one.play.call_count == 2
コード例 #9
0
 def test_move_11(self):
     g = Game()
     g.move(0)
     g.move(11)
     self.assertEqual(g._board, [1, 6, 5, 5, 5, 4, 0, 4, 4, 4, 4, 0, 5, 1])
     self.assertEqual(g.score(), (0, 1))
     self.assertEqual(g.turn_player(), 1)
     self.assertEqual(g.history(),
                      [[4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0],
                       [0, 5, 5, 5, 5, 4, 0, 4, 4, 4, 4, 4, 4, 0]])
コード例 #10
0
ファイル: arena.py プロジェクト: feliciaannette/mancala
 def _handle_combo(combo, seed, games_to_play):
     wins = 0
     for idx in range(0, games_to_play):
         game = Game()
         agent_one = (combo[0][1])(seed + idx)
         agent_two = (combo[1][1])(seed + idx)
         while not game.over():
             if game.turn_player() == 1:
                 game.move(agent_one.move(game))
             else:
                 game.move(agent_two.move(game))
         if game.score()[0] > game.score()[1]:
             wins = wins + 1
     return (combo[0][0], combo[1][0], wins)
コード例 #11
0
ファイル: test_game.py プロジェクト: Leonie-/mancala-python
    def test_does_not_switch_players_when_game_over_occurs_on_a_players_extra_turn(
            self):
        expected_game_log = "[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [23, 19]]"
        current_player = 1
        mock_mancala_board = mock.Mock()
        mock_player_one = mock.Mock()
        mock_player_two = mock.Mock()
        mock_mancala_board.game_log = expected_game_log
        mock_mancala_board.game_over.side_effect = [False, True]
        mock_player_one.play.side_effect = [True, False]

        game = Game(mock_mancala_board, mock_player_one, mock_player_two)
        game.play(current_player)
        assert not mock_player_two.play.called
コード例 #12
0
 def test_Illegal_empty(self):
     g = Game()
     g.move(0)
     g.move(7)
     g.move(0)
     self.assertEqual(g._board, [0, 5, 5, 5, 5, 4, 0, 0, 5, 5, 5, 5, 4, 0])
     self.assertEqual(g.score(), (0, 0))
     self.assertEqual(g.turn_player(), 1)
コード例 #13
0
 def test_capture_p2_not_okay_opp_side(self):
     g = Game()
     g._board = [1, 0, 0, 1, 0, 0, 0, 2, 3, 0, 0, 1, 0, 0]
     g._player_one = False
     g.move(11)
     self.assertEqual(g._board, [0, 0, 0, 1, 0, 0, 0, 2, 3, 0, 0, 0, 0, 2])
     self.assertEqual(g.score(), (0, 2))
     self.assertEqual(g.turn_player(), 1)
コード例 #14
0
 def test_capture_p2_not_okay_empty_opp(self):
     g = Game()
     g._board = [1, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 1, 2]
     g._player_one = False
     g.move(7)
     self.assertEqual(g._board, [1, 0, 0, 0, 0, 1, 2, 0, 1, 0, 0, 0, 1, 2])
     self.assertEqual(g.score(), (2, 2))
     self.assertEqual(g.turn_player(), 1)
コード例 #15
0
 def test_capture_p2_not_okay_grand_slam(self):
     g = Game()
     g._board = [1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1]
     g._player_one = False
     g.move(11)
     self.assertEqual(g._board, [1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1])
     self.assertEqual(g.score(), (1, 1))
     self.assertEqual(g.turn_player(), 1)
コード例 #16
0
 def test_p2_Win_exact(self):
     g = Game()
     g._board = [1, 0, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0, 1, 24]
     g._player_one = False
     g.move(12)
     self.assertEqual(g._board, [1, 0, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0, 0, 25])
     self.assertEqual(g.score(), (5, 25))
     self.assertTrue(g.over())
コード例 #17
0
 def test_p2_Win_over(self):
     g = Game()
     g._board = [1, 0, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0, 15, 24]
     g._player_one = False
     g.move(12)
     self.assertEqual(g._board, [3, 1, 1, 1, 1, 2, 5, 2, 1, 1, 1, 1, 1, 26])
     self.assertEqual(g.score(), (5, 26))
     self.assertTrue(g.over())
コード例 #18
0
def game(client, session):
    game = Game(player_1="Tester", player_2="Checker")
    session.add(game)
    session.commit()
    yield game
    session.delete(game)
    session.commit()
コード例 #19
0
ファイル: a3c.py プロジェクト: zakizadeh/mancala
    def _move(self, game):
        '''Return move which ends in score hole'''
        assert not game.over()
        self._idx += 1
        game_clone, rot_flag = game.clone_turn()
        move_options = Agent.valid_indices(game_clone)

        state = self.env.force(game_clone)
        state = torch.from_numpy(state).type(self._dtype)
        cx = Variable(torch.zeros(1, 400).type(self._dtype), volatile=True)
        hx = Variable(torch.zeros(1, 400).type(self._dtype), volatile=True)

        _, logit, (hx, cx) = self._model((Variable(state.unsqueeze(0),
                                                   volatile=True), (hx, cx)))
        prob = F.softmax(logit)
        scores = [(action, score)
                  for action, score in enumerate(prob[0].data.tolist())
                  if action in move_options]

        valid_actions = [action for action, _ in scores]
        valid_scores = np.array([score for _, score in scores])

        final_move = self.np_random.choice(valid_actions,
                                           1,
                                           p=valid_scores /
                                           valid_scores.sum())[0]

        return Game.rotate_board(rot_flag, final_move)
コード例 #20
0
ファイル: test_game.py プロジェクト: Leonie-/mancala-python
    def test_returns_the_game_logs_on_game_over(self):
        expected_game_board_log = "[[4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4], [0, 0]]"
        expected_game_state_log = "game state log"
        current_player = 1
        mock_mancala_board = mock.Mock()
        mock_player_one = mock.Mock()
        mock_player_two = mock.Mock()
        mock_mancala_board.game_over.return_value = True
        mock_mancala_board.game_board_log = expected_game_board_log
        mock_mancala_board.game_state_log = expected_game_state_log
        mock_player_one.play.return_value = False

        game = Game(mock_mancala_board, mock_player_one, mock_player_two)
        assert game.play(current_player) == [
            expected_game_board_log, expected_game_state_log
        ]
コード例 #21
0
    def _move(self, game):
        """Return best value from Min_Max"""
        self._idx = self._idx + 1
        random.seed(self._seed + self._idx)
        game_clone, rot_flag = game.clone_turn()

        move_options = Agent.valid_indices(game_clone)

        available_scores = list(
            map(lambda move_slot:
                AgentMinMax._min_max(
                    self._depth,
                    game_clone,
                    move_slot,
                    -sys.maxsize,
                    sys.maxsize
                ),
                move_options))

        score_max = max(available_scores)
        final_options = [move for score, move in
                         zip(available_scores, move_options)
                         if score == score_max]

        final_move = Game.rotate_board(rot_flag, random.choice(final_options))
        return final_move
コード例 #22
0
 def test_side_02_empty(self):
     g = Game()
     g._board = [1, 0, 0, 0, 0, 7, 20, 0, 0, 0, 0, 0, 3, 10]
     g._player_one = False
     g.move(12)
     self.assertEqual(g._board,
                      [0, 0, 0, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 11])
     self.assertEqual(g.score(), (30, 11))
     self.assertTrue(g.over())
コード例 #23
0
ファイル: test_game.py プロジェクト: Leonie-/mancala-python
def test_creates_a_new_game():
    mock_mancala_board = mock.Mock()
    mock_player_one = mock.Mock()
    mock_player_two = mock.Mock()

    game = Game(mock_mancala_board, mock_player_one, mock_player_two)
    assert game.game == mock_mancala_board
    assert game.players == [mock_player_one, mock_player_two]
コード例 #24
0
 def test_capture_p1_okay(self):
     g = Game()
     g._board = [1, 0, 0, 4, 0, 0, 0, 2, 3, 0, 0, 1, 0, 0]
     g.move(0)
     self.assertEqual(g._board, [0, 0, 0, 4, 0, 0, 2, 2, 3, 0, 0, 0, 0, 0])
     self.assertEqual(g.score(), (2, 0))
     self.assertEqual(g.turn_player(), 2)
コード例 #25
0
 def test_capture_p1_not_okay_empty_opp(self):
     g = Game()
     g._board = [1, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 1, 2]
     g.move(0)
     self.assertEqual(g._board, [0, 1, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 1, 2])
     self.assertEqual(g.score(), (2, 2))
     self.assertEqual(g.turn_player(), 2)
コード例 #26
0
 def test_capture_p1_not_okay_grand_slam(self):
     g = Game()
     g._board = [1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1]
     g.move(0)
     self.assertEqual(g._board, [0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1])
     self.assertEqual(g.score(), (1, 1))
     self.assertEqual(g.turn_player(), 2)
コード例 #27
0
 def test_capture_score_not_okay(self):
     g = Game()
     g._board = [1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 5]
     g.move(5)
     self.assertEqual(g._board, [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 5])
     self.assertEqual(g.score(), (1, 5))
     self.assertEqual(g.turn_player(), 1)
コード例 #28
0
 def test_p1_Win_exact(self):
     g = Game()
     g._board = [1, 0, 0, 0, 0, 1, 24, 0, 0, 0, 0, 0, 3, 5]
     g.move(5)
     self.assertEqual(g._board, [1, 0, 0, 0, 0, 0, 25, 0, 0, 0, 0, 0, 3, 5])
     self.assertEqual(g.score(), (25, 5))
     self.assertTrue(g.over())
コード例 #29
0
 def test_p1_Win_over(self):
     g = Game()
     g._board = [1, 0, 0, 0, 15, 1, 24, 0, 0, 0, 0, 0, 3, 5]
     g.move(4)
     self.assertEqual(g._board, [2, 1, 1, 1, 1, 3, 26, 1, 1, 1, 1, 1, 4, 5])
     self.assertEqual(g.score(), (26, 5))
     self.assertTrue(g.over())
コード例 #30
0
 def test_capture_p1_not_okay_opp_side(self):
     g = Game()
     g._board = [1, 0, 0, 4, 1, 3, 0, 2, 0, 0, 0, 1, 0, 0]
     g.move(5)
     self.assertEqual(g._board, [1, 0, 0, 4, 1, 0, 1, 3, 1, 0, 0, 1, 0, 0])
     self.assertEqual(g.score(), (1, 0))
     self.assertEqual(g.turn_player(), 2)