def test_ai_get_best_doubles(self): prev_play = Play(2, [Card('8', 'c'), Card('8', 'd')], 0, play_type=DOUBLES) self.setup_game_state([prev_play]) best_play = self.test_ai_player_lv2.get_best_doubles(self.game_state) assert best_play[0].name == '9' _check_double(best_play.cards)
def setup_method(self): hand = Hand(Card.strs_to_cards(TestAIPlayer.card_strs_lv2)) self.test_ai_player_lv2 = AIPlayer(hand, 0, "") hand = Hand(Card.strs_to_cards(TestAIPlayer.card_strs_lv3)) self.test_ai_player_lv3 = AIPlayer(hand, 0, "") self.game_state = GameState(17, 17) self.game_state_is_setup = False
def test_ai_get_best_triples_alone(self): prev_play = Play( 2, [Card('J', 'h'), Card('J', 'd'), Card('J', 'c')], 0, play_type=TRIPLES) self.setup_game_state([prev_play]) best_play = self.test_ai_player_lv3.get_best_triples(self.game_state) assert best_play[0].name == 'Q' _check_triple(best_play.cards)
def get_new_ordered_deck(): """Returns an ordered list of all the cards""" deck = [] for v in VALUES: for s in SUITS: if v == "Z": deck.append(Card(v, 0)) deck.append(Card(v, 1)) break else: deck.append(Card(v, s)) return deck
def setup_method(self): hand_lv1 = Hand(Card.strs_to_cards(TestMC.card_strs_lv1)) self.test_player_lv1 = Player(hand_lv1, 0, "") hand_lv2 = Hand(Card.strs_to_cards(TestMC.card_strs_lv2)) self.test_player_lv2 = Player(hand_lv2, 0, "") hand_lv3 = Hand(Card.strs_to_cards(TestMC.card_strs_lv3)) self.test_player_lv3 = Player(hand_lv3, 0, "") hand_lv4 = Hand(Card.strs_to_cards(TestMC.card_strs_lv4)) self.test_player_lv4 = Player(hand_lv4, 0, "") self.game_state = GameState(17, 17)
def setup_game(card_strs): hand = Hand(Card.strs_to_cards(card_strs)) print("Starting hand:", hand) aiplayer = AIPlayer(hand, 0, "") player = Player(hand, 0, "") game_state = GameState(17, 17) return aiplayer, player, game_state
def get_play_from_input(user_input): """returns card play based on user's input""" if not user_input: return Play.get_pass_play() player_card_strs = user_input.split() played_cards = Card.strs_to_cards(player_card_strs) return Play.get_play_from_cards(played_cards)
def test_prob_double_simple_hand_too_small(self): """ 100% chance of a person having a double since one hand is too small """ card_strs = ['4d', '4h', '5h', '5c', '6h', '8d', '0c', '0h', '2c', '2h'] cards = Card.strs_to_cards(card_strs) n_cards1 = 8 TestProbability._run_test_prob_of_occurance(cards, 2, n_cards1)
def test_prob_double_simple_three_exists(self): """ 100% chance of a person having a double since there is a triple """ card_strs = ['3s', '4d', '4h', '4s', '5s', '5d', '6h', '6d', '7c'] cards = Card.strs_to_cards(card_strs) n_cards1 = 5 TestProbability._run_test_prob_of_occurance(cards, 2, n_cards1)
def get_cards_from_file(filename): """returns a list of cards from file""" card_strs = [] with open(filename, "r") as f: for line in f.readlines(): card_str = line.strip() card_strs.append(card_str) return Card.strs_to_cards(card_strs)
def test_ai_get_best_wild(self): card_strs = [ '5c', '5d', '6h', '6s', '7h', '7d', '7c', '7s', '8d', '8c', 'As', 'Ad', 'Ac', 'Ah', 'Z0', 'Z1' ] player = TestAIPlayer.generate_ai_from_card_strs(card_strs) prev_play1 = Play(0, [Card('2', 'd')], 0, play_type=SINGLES) prev_play2 = Play( 2, [Card('4', 's'), Card('4', 'c'), Card('4', 'd'), Card('4', 'h')], 0, play_type=QUADRUPLES) self.setup_game_state([prev_play1, prev_play2]) best_play = player.get_best_wild(self.game_state) assert best_play.get_base_card().name == 'A' _check_quadruples(best_play.cards)
def test_best_play_single_and_triple(self): """tests best play function""" computer_card_strs = ['3d', '3s', '3c', '4c', 'Ah'] unrevealed_card_strs = ['Qd', 'Qs', 'Qc', '7c', 'Kd', 'Ks', 'Kc', '8d'] game_state, computer = TestMC.generate_game_state( computer_card_strs, unrevealed_card_strs, 1) play1 = computer.get_best_play(game_state) game_state.prev_play = Play(2, [Card('K', 'h')], 0) play2 = computer.get_best_play(game_state) assert get_best_play(iter([play1, play2]), computer, game_state) == play2
def test_ai_get_best_single_straight(self): card_strs = [ '3c', '3d', '4s', '6h', '6d', '6c', '7h', '7d', '9c', '0s', 'Jd', 'Qc', 'Qs', 'Kd', 'Ac', 'Ah', 'Z0' ] player = TestAIPlayer.generate_ai_from_card_strs(card_strs) prev_play = Play(2, [ Card('3', 'h'), Card('4', 'd'), Card('5', 'c'), Card('6', 's'), Card('7', 's') ], 0, play_type=STRAIGHTS) self.setup_game_state([prev_play]) best_play = player.get_best_straights(self.game_state) assert best_play[0].name == '9' _check_straight(best_play.cards, 1)
def add_cards(self, cards=None, card_strs=None): """adds a list of cards or list of string of cards to hand""" did_add = cards or card_strs if cards: for c in cards: self._add(c) if card_strs: for card_str in card_strs: self._add(Card.str_to_card(card_str)) if did_add: self._organize()
def test_best_play_two_singles(self): """tests best play function""" computer_card_strs = ['0d', '2s'] unrevealed_card_strs = ['Qd', 'kd'] game_state, computer = TestMC.generate_game_state( computer_card_strs, unrevealed_card_strs, 1) play1 = computer.get_best_play(game_state) game_state.prev_play = Play(2, [Card('K', 'h')], 0) play2 = computer.get_best_play(game_state) assert get_best_play(iter([play1, play2]), computer, game_state) == play2
def test_prob_double_with_base(self): """tests prob of double greater than some number""" card_strs = ['3c', '3s', '3h', '4d', '5c', '6h', '8c', '9s', 'Jc', 'Jh', 'Ks', 'Ah', '2h'] cards = Card.strs_to_cards(card_strs) n_cards1 = 6 base_val = 5 expected = TestProbability.prob_of_occurance_brute(cards, 2, n_cards1, base_val=base_val) actual = _prob_of_doubles(cards, n_cards1, base_val=base_val) assert actual == expected not_expected = TestProbability.prob_of_occurance_brute(cards, 2, n_cards1) assert actual != not_expected
def test_ai_get_best_triples_double(self): card_strs = [ '3c', '3d', '6h', '6s', '6d', '6c', '7h', '7d', '7c', '8s', '9d', '0c', 'Qs', 'Kd', 'Ac', '2h', 'Z0' ] player = TestAIPlayer.generate_ai_from_card_strs(card_strs) prev_play = Play(2, [ Card('5', 'h'), Card('5', 'd'), Card('5', 'c'), Card('4', 'c'), Card('4', 's') ], 2, play_type=TRIPLES) self.setup_game_state([prev_play]) best_play = player.get_best_triples(self.game_state) assert best_play[0].name == '7' assert best_play[3].name == '3' _check_triple(best_play.cards)
def test_brute_is_slower(self): """tests that brute force is indeed slower""" card_strs = ['3s', '4d', '4h', '5s', '5d', '6h', '6d', '7c', '8s', '9c', '0h', '0c', 'Jd', 'qs', 'Kd', 'kc', 'ah', '2c'] cards = Card.strs_to_cards(card_strs) n_cards1 = 8 time1 = time.time() expected = TestProbability.prob_of_occurance_brute(cards, 2, n_cards1) time2 = time.time() actual = _prob_of_doubles(cards, n_cards1) time3 = time.time() assert expected == actual assert time3 - time2 < time2 - time1 print("manual time: {}\tbrute force time: {}".format(time3 - time2, time2 - time1))
def test_multiple_best_play(self): """tests multiple best play function""" computer_card_strs = ['3d', '3s', '3c', '4c', 'Ah'] unrevealed_card_strs = ['Qd', 'Qs', 'Qc', '7c', 'Kd', 'Ks', 'Kc', '8d'] game_state, computer = TestMC.generate_game_state( computer_card_strs, unrevealed_card_strs, 1) play1 = computer.get_best_play(game_state) game_state.prev_play = Play(2, [Card('K', 'h')], 0) play2 = computer.get_best_play(game_state) ordered_best_plays = get_best_play(iter([play1, play2]), computer, game_state, num_best=2) assert ordered_best_plays[0] == play2 assert ordered_best_plays[1] == play1
def generate_game_state(computer_card_strs, unrevealed_card_strs, n_cards1): game_state = GameState(20, 17) game_state.unused_cards = Card.strs_to_cards(computer_card_strs + unrevealed_card_strs) game_state.used_cards = game_tools.remove_from_deck( get_new_ordered_deck(), game_state.unused_cards) game_state.player_cards = [ len(computer_card_strs), n_cards1, len(unrevealed_card_strs) - n_cards1 ] computer_hand = Hand([]) computer_hand.add_cards(card_strs=computer_card_strs) computer = Player(computer_hand, 0, "") return game_state, computer
def test_prob_double_compare_two_bases(self): """tests prob of doubles multiple times""" card_strs = ['3c', '3s', '3h', '4d', '5c', '6h', '8c', '9s', 'Jc', 'Jh', 'Ks', 'Ah', '2h'] cards = Card.strs_to_cards(card_strs) n_total_cards = 13 n_cards1 = 6 base_val_low = 5 base_val_high = 10 expected_low = TestProbability.prob_of_occurance_brute(cards, 2, n_cards1, base_val=base_val_low) actual_low = _prob_of_doubles(cards, n_cards1, base_val=base_val_low) assert actual_low == expected_low expected_high = TestProbability.prob_of_occurance_brute(cards, 2, n_cards1, base_val=base_val_high) actual_high = _prob_of_doubles(cards, n_cards1, base_val=base_val_high) assert actual_high == expected_high assert actual_low >= actual_high
def test_ai_get_best_double_straight(self): card_strs = [ '6h', '6c', '6s', '7s', '7d', '7c', '8d', '8h', '9H', '9D', '0H', '0S', 'Js', 'Qd', 'KD', 'KC', '2C' ] player = TestAIPlayer.generate_ai_from_card_strs(card_strs) prev_play = Play(2, [ Card('3', 'h'), Card('3', 'd'), Card('4', 'c'), Card('4', 's'), Card('5', 's'), Card('5', 'c') ], 0, play_type=DOUBLE_STRAIGHTS) self.setup_game_state([prev_play]) best_play = player.get_best_double_straights(self.game_state) assert best_play[0].name == '8' _check_straight(best_play.cards, 2)
def test_ai_get_best_singles(self): prev_play = Play(2, [Card('3', 'h')], 0, play_type=SINGLES) self.setup_game_state([prev_play]) best_play = self.test_ai_player_lv3.get_best_singles(self.game_state) assert best_play[0].name == 'A' _check_single(best_play.cards)
def generate_player_from_card_strs(card_strs): hand = Hand(Card.strs_to_cards(card_strs)) return Player(hand, 0, "")
def test_ai_get_best_adj_triple_alone(self): # should not take 777888 because need two sevens for 556677 card_strs = [ '5c', '5d', '6h', '6s', '7h', '7d', '7c', '8s', '8d', '8c', '9s', '9d', '9c', '0h' ] player = TestAIPlayer.generate_ai_from_card_strs(card_strs) prev_play1 = Play( 0, [Card('2', 'd'), Card('2', 's'), Card('2', 'c')], 0, play_type=TRIPLES) prev_play2 = Play(2, [ Card('3', 'h'), Card('3', 'd'), Card('3', 'c'), Card('4', 'd'), Card('4', 's'), Card('4', 'c'), Card('5', 'h'), Card('6', 'd') ], 2, play_type=ADJ_TRIPLES) self.setup_game_state([prev_play1, prev_play2]) best_play = player.get_best_adj_triples(self.game_state) assert best_play[0].name == '8' assert best_play[3].name == '9' assert best_play[6].name == '7' assert best_play[7].name == '0' _check_adj_triple(best_play.cards, 2)
def test_prob_triple_hand_too_small(self): """tests probability of triples with outcome of 1""" card_strs = ['3s', '3s', '3s', '4s', '4s','4s', '5s', '5s', '5s', '6s', '7s', '7s'] cards = Card.strs_to_cards(card_strs) n_cards1 = 2 TestProbability._run_test_prob_of_occurance(cards, 3, n_cards1)
def test_prob_triple_no_three_exists(self): """tests probability of triples with outcome of 0""" card_strs = ['3s', '3s', '4s', '4s', '5s', '5s', '6s', '7s', '7s'] cards = Card.strs_to_cards(card_strs) n_cards1 = 5 TestProbability._run_test_prob_of_occurance(cards, 3, n_cards1)
def test_prob_triple_simple(self): """tests probability of triples with simple inputs""" card_strs = ['3s', '3s', '3s', '4s', '4s','4s', '5s', '5s', '5s', '6s', '7s', '7s'] cards = Card.strs_to_cards(card_strs) n_cards1 = 6 TestProbability._run_test_prob_of_occurance(cards, 3, n_cards1)
def test_ai_get_best_quad_double(self): # should take 77775566 (follow up with AAAA then 88) card_strs = [ '5c', '5d', '6h', '6s', '7h', '7d', '7c', '7s', '8d', '8c', 'As', 'Ad', 'Ac', 'Ah' ] player = TestAIPlayer.generate_ai_from_card_strs(card_strs) prev_play1 = Play( 0, [Card('2', 'd'), Card('2', 's'), Card('2', 'c')], 0, play_type=TRIPLES) prev_play2 = Play(1, [ Card('9', 'd'), Card('0', 'd'), Card('J', 's'), Card('Q', 'c'), Card('K', 'c') ], 0, play_type=STRAIGHTS) prev_play3 = Play(2, [ Card('4', 's'), Card('4', 'c'), Card('4', 'd'), Card('4', 'h'), Card('3', 'h'), Card('3', 's'), Card('5', 'h'), Card('5', 's') ], 4, play_type=QUADRUPLES) self.setup_game_state([prev_play1, prev_play2, prev_play3]) best_play = player.get_best_quad(self.game_state) assert best_play[0].name == '7' _check_quadruples(best_play.cards)
def test_prob_double_simple(self): """tests prob of double with simple inputs""" card_strs = ['3h', '4d', '4h', '5s', '5d', '6h', '6d', '7c', '9d', '9s'] cards = Card.strs_to_cards(card_strs) n_cards1 = 6 TestProbability._run_test_prob_of_occurance(cards, 2, n_cards1)