示例#1
0
 def test_apply_move_sets_a_winner_after_4_plays(self):
     state = EuchreGame.initial_state()
     state = EuchreGame.determine(state)
     for x in range(4):
         lead_suit = suit(state.trump, state.lead_card)
         move = playable_cards(state.trump,
                               lead_suit,
                               state.hands[state.current_player], )[0]
         state = EuchreGame.apply_move(state, move)
     self.assertEqual(sum(state.tricks_won_by_team), 1)
示例#2
0
def main():
    state = EuchreGame.initial_state()
    hands = EuchreGame.determine(state).hands
    table = [None] * 4
    while True:
        winner = EuchreGame.get_winner(state)
        if winner is not None:
            dump_state(state, hands)
            break
        state_hands = [player == state.current_player and hand[:] or []
                       for player, hand in enumerate(hands)]
        state = state._replace(hands=state_hands)
        actual_options = playable_cards(state.trump,
                                        suit(state.trump,
                                             state.lead_card),
                                        hands[state.current_player])
        legal_moves = EuchreGame.get_moves(state)[1]
        result = (
            MCTS(EuchreGame, state)
            .get_simulation_result(1000, actual_options))
        move = result.move
        dump_state(state, hands, result.root.children, move, table)
        if state.current_player == 0:
            while True:
                try:
                    move = input('')
                    assert move in legal_moves
                    hands[0].remove(move)
                    table[state.current_player] = move
                    state = EuchreGame.apply_move(state, move)
                    break
                except (AssertionError, ValueError):
                    print(dumps({'error': 'That is not a legal move'}))
        else:
            hands[state.current_player].remove(move)
            table[state.current_player] = move
            state = EuchreGame.apply_move(state, move)
        if len(filter(None, table)) == 4:
            dump_state(state, hands, result.root.children, move, table)
            table = [None] * 4
            sleep(4)  # wait for the player to see the table before clearing it
示例#3
0
def main():
    state = EuchreGame.initial_state()
    hands = EuchreGame.determine(state).hands
    table = [None] * 4
    while True:
        winner = EuchreGame.get_winner(state)
        if winner is not None:
            dump_state(state, hands)
            break
        state_hands = [
            player == state.current_player and hand[:] or []
            for player, hand in enumerate(hands)
        ]
        state = state._replace(hands=state_hands)
        actual_options = playable_cards(state.trump,
                                        suit(state.trump, state.lead_card),
                                        hands[state.current_player])
        legal_moves = EuchreGame.get_moves(state)[1]
        result = (MCTS(EuchreGame,
                       state).get_simulation_result(1000, actual_options))
        move = result.move
        dump_state(state, hands, result.root.children, move, table)
        if state.current_player == 0:
            while True:
                try:
                    move = input('')
                    assert move in legal_moves
                    hands[0].remove(move)
                    table[state.current_player] = move
                    state = EuchreGame.apply_move(state, move)
                    break
                except (AssertionError, ValueError):
                    print(dumps({'error': 'That is not a legal move'}))
        else:
            hands[state.current_player].remove(move)
            table[state.current_player] = move
            state = EuchreGame.apply_move(state, move)
        if len(filter(None, table)) == 4:
            dump_state(state, hands, result.root.children, move, table)
            table = [None] * 4
            sleep(4)  # wait for the player to see the table before clearing it
示例#4
0
 def test_apply_move_sets_a_winner_after_4_plays(self):
     state = EuchreGame.initial_state()
     state = EuchreGame.determine(state)
     for x in range(4):
         lead_suit = suit(state.trump, state.lead_card)
         move = playable_cards(
             state.trump,
             lead_suit,
             state.hands[state.current_player],
         )[0]
         state = EuchreGame.apply_move(state, move)
     self.assertEqual(sum(state.tricks_won_by_team), 1)
示例#5
0
 def test_a_whole_hand(self):
     state = EuchreGame.initial_state(['jd', 'jh', '9c', '9h', 'as'], 'kd')
     state = state._replace(hands=[['jd', 'jh', '9c', '9h', 'as'],
                                   ['qd', 'ah', '0h', 'kc', 'js'],
                                   ['9d', 'jc', 'kh', 'qc', '9s'],
                                   ['0d', '0c', 'qh', 'ac', '0s']])
     state = EuchreGame.apply_move(state, 'jd')
     state = EuchreGame.apply_move(state, 'qd')
     state = EuchreGame.apply_move(state, '9d')
     state = EuchreGame.apply_move(state, '0d')
     self.assertEqual(state.tricks_won_by_team, [1, 0])
     self.assertEqual(state.current_player, 0)
     state = EuchreGame.apply_move(state, 'jh')
     state = EuchreGame.apply_move(state, 'ah')
     state = EuchreGame.apply_move(state, 'jc')
     state = EuchreGame.apply_move(state, '0c')
     self.assertEqual(state.tricks_won_by_team, [2, 0])
     self.assertEqual(state.current_player, 0)
     state = EuchreGame.apply_move(state, '9h')
     state = EuchreGame.apply_move(state, '0h')
     state = EuchreGame.apply_move(state, 'kh')
     state = EuchreGame.apply_move(state, 'qh')
     self.assertEqual(state.tricks_won_by_team, [3, 0])
     self.assertEqual(state.current_player, 2)
     state = EuchreGame.apply_move(state, 'qc')
     state = EuchreGame.apply_move(state, 'ac')
     state = EuchreGame.apply_move(state, '9c')
     state = EuchreGame.apply_move(state, 'kc')
     self.assertEqual(state.tricks_won_by_team, [3, 1])
     self.assertEqual(state.current_player, 3)
     self.assertIsNone(state.winning_team)
     state = EuchreGame.apply_move(state, '0s')
     try:
         EuchreGame.apply_move(state, 'ad')
         self.fail('Did not follow suit earlier')
     except ValueError:
         pass
     state = EuchreGame.apply_move(state, 'as')
     state = EuchreGame.apply_move(state, 'js')
     state = EuchreGame.apply_move(state, '9s')
     self.assertEqual(state.tricks_won_by_team, [4, 1])
     self.assertEqual(state.winning_team, 0)
     self.assertEqual(EuchreGame.get_winner(state), 0)
     self.assertEqual(EuchreGame.current_player(state), 0)
示例#6
0
 def test_a_whole_hand(self):
     state = EuchreGame.initial_state(['jd', 'jh', '9c', '9h', 'as'], 'kd')
     state = state._replace(hands=[['jd', 'jh', '9c', '9h', 'as'],
                                   ['qd', 'ah', '0h', 'kc', 'js'],
                                   ['9d', 'jc', 'kh', 'qc', '9s'],
                                   ['0d', '0c', 'qh', 'ac', '0s']])
     state = EuchreGame.apply_move(state, 'jd')
     state = EuchreGame.apply_move(state, 'qd')
     state = EuchreGame.apply_move(state, '9d')
     state = EuchreGame.apply_move(state, '0d')
     self.assertEqual(state.tricks_won_by_team, [1, 0])
     self.assertEqual(state.current_player, 0)
     state = EuchreGame.apply_move(state, 'jh')
     state = EuchreGame.apply_move(state, 'ah')
     state = EuchreGame.apply_move(state, 'jc')
     state = EuchreGame.apply_move(state, '0c')
     self.assertEqual(state.tricks_won_by_team, [2, 0])
     self.assertEqual(state.current_player, 0)
     state = EuchreGame.apply_move(state, '9h')
     state = EuchreGame.apply_move(state, '0h')
     state = EuchreGame.apply_move(state, 'kh')
     state = EuchreGame.apply_move(state, 'qh')
     self.assertEqual(state.tricks_won_by_team, [3, 0])
     self.assertEqual(state.current_player, 2)
     state = EuchreGame.apply_move(state, 'qc')
     state = EuchreGame.apply_move(state, 'ac')
     state = EuchreGame.apply_move(state, '9c')
     state = EuchreGame.apply_move(state, 'kc')
     self.assertEqual(state.tricks_won_by_team, [3, 1])
     self.assertEqual(state.current_player, 3)
     self.assertIsNone(state.winning_team)
     state = EuchreGame.apply_move(state, '0s')
     try:
         EuchreGame.apply_move(state, 'ad')
         self.fail('Did not follow suit earlier')
     except ValueError:
         pass
     state = EuchreGame.apply_move(state, 'as')
     state = EuchreGame.apply_move(state, 'js')
     state = EuchreGame.apply_move(state, '9s')
     self.assertEqual(state.tricks_won_by_team, [4, 1])
     self.assertEqual(state.winning_team, 0)
     self.assertEqual(EuchreGame.get_winner(state), 0)
     self.assertEqual(EuchreGame.current_player(state), 0)