def test_initial_state_bad_cards(self): try: EuchreGame.initial_state( ['notacard', 'notacard2', 'notacard4', 'notacard5']) self.fail('Should not allow invalid starting hands') except ValueError: pass
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)
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)
def test_with_mcts(self): state = EuchreGame.initial_state(['0d', '0h', 'as', 'ac', 'ah'], 'jd') result = MCTS(EuchreGame, state).get_simulation_result(100) self.assertEqual(result.max_depth, 20) # there is no longer option now so all players playing will always # result in 20 cards being played self.assertEqual(result.avg_depth, 20)
def test_determine(self): state = EuchreGame.initial_state() state = state._replace(trump_card='jd', trump='d', cards_played=['jd', 'ad', 'kd', 'qd', 'qc'], tricks_won_by_team=[1, 0], hands=[['jc', 'kc', 'ah', 'js'], [], [], []], voids_by_player=[ set(), set(['d', 'h', 'c']), set(), set(['s', 'c', 'd']) ]) state = EuchreGame.determine(state) self.assertTrue( all([suit('d', card) == 's' for card in state.hands[1]])) self.assertTrue( all([suit('d', card) == 'h' for card in state.hands[3]]))
def test_determine_in_the_middle_of_a_trick(self): state = EuchreGame.initial_state() state = state._replace(trump_card='jd', trump='d', tricks_won_by_team=[1, 0], cards_played_by_player=['jh', None, None, None], hands=[[], [], [], []], voids_by_player=[ set(), set(['d', 'h', 'c']), set(), set(['s', 'c', 'd']) ]) state = EuchreGame.determine(state) self.assertEqual(len(state.hands[0]), 3) self.assertEqual(len(state.hands[1]), 4) self.assertEqual(len(state.hands[2]), 4) self.assertEqual(len(state.hands[3]), 4)
def test_determine(self): state = EuchreGame.initial_state() state = state._replace(trump_card='jd', trump='d', cards_played=['jd', 'ad', 'kd', 'qd', 'qc'], tricks_won_by_team=[1, 0], hands=[['jc', 'kc', 'ah', 'js'], [], [], []], voids_by_player=[set(), set(['d', 'h', 'c']), set(), set(['s', 'c', 'd'])]) state = EuchreGame.determine(state) self.assertTrue(all([suit('d', card) == 's' for card in state.hands[1]])) self.assertTrue(all([suit('d', card) == 'h' for card in state.hands[3]]))
def test_determine_in_the_middle_of_a_trick(self): state = EuchreGame.initial_state() state = state._replace(trump_card='jd', trump='d', tricks_won_by_team=[1, 0], cards_played_by_player=['jh', None, None, None], hands=[[], [], [], []], voids_by_player=[set(), set(['d', 'h', 'c']), set(), set(['s', 'c', 'd'])]) state = EuchreGame.determine(state) self.assertEqual(len(state.hands[0]), 3) self.assertEqual(len(state.hands[1]), 4) self.assertEqual(len(state.hands[2]), 4) self.assertEqual(len(state.hands[3]), 4)
def test_this_hand_should_win_every_time(self): state = EuchreGame.State( cards_played_by_player=[None, None, None, None], current_player=0, lead_card=None, trump_card='0d', trump='d', winning_team=None, hands=[['jd', 'jh', 'ad', 'kd', 'qd'], [], [], []], tricks_won_by_team=[0, 0], cards_played=['0d'], voids_by_player=[set([]), set([]), set([]), set([])]) result = (MCTS(EuchreGame, state).get_simulation_result(100, get_leaf_nodes=True)) for node in result.leaf_nodes: self.assertGreaterEqual(node.state.tricks_won_by_team[0], 5)
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
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
def test_initial_state_trump_determined_by_kitty(self): state = EuchreGame.initial_state() state = EuchreGame.determine(state) self.assertEqual(len(state.cards_played), 1) self.assertEqual(suit(state.trump, state.cards_played[0]), state.trump) self.assertIn(state.trump_card, state.cards_played)
def test_initial_state_invalid_trump(self): try: EuchreGame.initial_state(trump_card='xp') self.fail('initial_state should require valid trump') except ValueError: pass
def test_initial_state_set_trump_and_determine_deals(self): state = EuchreGame.initial_state(['ad', 'kd', 'qd', 'jd', '0d']) state = EuchreGame.determine(state) self.assertEqual(len(list(chain(*state.hands))), 20) state = EuchreGame.initial_state(trump_card='jc') self.assertEqual(state.trump, 'c')
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)
def test_determine_deals_5_cards_to_each_player(self): state = EuchreGame.initial_state() state = EuchreGame.determine(state) self.assertTrue(all(len(hand) == 5 for hand in state.hands))
def main(): state = EuchreGame.initial_state(['ad', '0d', 'kd', '0s', '9s'], trump='d') result = (MCTS(EuchreGame, state) .get_simulation_result(1000, get_leaf_nodes=True)) flamegraph(result)