def test_playable_cards(self): self.assertEqual(playable_cards('c', 'd', deal()), ['ad', 'kd', 'qd', 'jd', '0d', '9d']) self.assertEqual(playable_cards('c', 'c', deal()), ['js', 'ac', 'kc', 'qc', 'jc', '0c', '9c']) self.assertEqual(playable_cards('c', 'c', deal()), ['js', 'ac', 'kc', 'qc', 'jc', '0c', '9c']) self.assertEqual(playable_cards('c', 'c', ['ad']), ['ad'])
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 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