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_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_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_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 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_suit(self): self.assertEqual(suit('s', 'jc'), 's') self.assertEqual(suit('c', 'js'), 'c') self.assertEqual(suit('d', 'jh'), 'd') self.assertEqual(suit('h', 'jd'), 'h')