def test_draw(self): player_1_hand, player_2_hand = parse_hands( "9S TS JS QS KS 9H TH JH QH KH") player_1_result, player_2_result = play(player_1_hand, player_2_hand) self.assertEqual(0, player_1_result) self.assertEqual(0, player_2_result)
def test_player_2_wins_pair_of_eights(self): player_1_hand, player_2_hand = parse_hands( "5H 5C 6S 7S KD 2C 3S 8S 8D TD") player_1_result, player_2_result = play(player_1_hand, player_2_hand) self.assertEqual(0, player_1_result) self.assertEqual(1, player_2_result)
def test_player_1_wins_highest_card_nine(self): player_1_hand, player_2_hand = parse_hands( "4D 6S 9H QH QC 3D 6D 7H QD QS") player_1_result, player_2_result = play(player_1_hand, player_2_hand) self.assertEqual(1, player_1_result) self.assertEqual(0, player_2_result)
def test_player_1_wins_full_house(self): player_1_hand, player_2_hand = parse_hands( "2H 2D 4C 4D 4S 3C 3D 3S 9S 9D") player_1_result, player_2_result = play(player_1_hand, player_2_hand) self.assertEqual(1, player_1_result) self.assertEqual(0, player_2_result)
def test_player_2_wins_flush(self): player_1_hand, player_2_hand = parse_hands( "2D 9C AS AH AC 3D 6D 7D TD QD") player_1_result, player_2_result = play(player_1_hand, player_2_hand) self.assertEqual(0, player_1_result) self.assertEqual(1, player_2_result)
def test_player_2_wins_highest_card_jack(self): player_1_hand, player_2_hand = parse_hands( "6D 7C 5D 5H 3S 5C JC 2H 5S 3D") player_1_result, player_2_result = play(player_1_hand, player_2_hand) self.assertEqual(0, player_1_result) self.assertEqual(1, player_2_result)
def test_player_1_wins_highest_card_ace(self): player_1_hand, player_2_hand = parse_hands( "5D 8C 9S JS AC 2C 5C 7D 8S QH") player_1_result, player_2_result = play(player_1_hand, player_2_hand) self.assertEqual(1, player_1_result) self.assertEqual(0, player_2_result)
def test_player_1_wins_royal_flush(self): player_1_hand, player_2_hand = parse_hands( "TH JH QH KH AH QC TC 6D 7C KS") player_1_result, player_2_result = play(player_1_hand, player_2_hand) self.assertEqual(1, player_1_result) self.assertEqual(0, player_2_result)
def test_high_card_player_2(self): player_1_hand, player_2_hand = parse_hands( "8H 9H TH JH QH 9S TS JS QS KS") player_1_result, player_2_result = play(player_1_hand, player_2_hand) self.assertEqual(0, player_1_result) self.assertEqual(1, player_2_result)
def main(file_path): player_1_score, player_2_score = 0, 0 with open(file_path, 'r') as input_file: for line in input_file: player_1_hand, player_2_hand = parse_hands(line) player_1_result, player_2_result = play(player_1_hand, player_2_hand) player_1_score += player_1_result player_2_score += player_2_result print("Player 1 won {0} games".format(player_1_score)) print("Player 2 won {0} games".format(player_2_score))
def test_valid_input(self): player_1_hand, player_2_hand = parse_hands( "KH 4H AS JS QS QC TC 6D 7C KS") self.assertIn(Card('K', 'H'), player_1_hand.cards) self.assertIn(Card('4', 'H'), player_1_hand.cards) self.assertIn(Card('A', 'S'), player_1_hand.cards) self.assertIn(Card('J', 'S'), player_1_hand.cards) self.assertIn(Card('Q', 'S'), player_1_hand.cards) self.assertIn(Card('Q', 'C'), player_2_hand.cards) self.assertIn(Card('T', 'C'), player_2_hand.cards) self.assertIn(Card('6', 'D'), player_2_hand.cards) self.assertIn(Card('7', 'C'), player_2_hand.cards) self.assertIn(Card('K', 'S'), player_2_hand.cards)
def test_full_house(self): player_1_hand, player_2_hand = parse_hands("2H 2D 2C 3S 3H QC TC 6D 7C KS") rank_scores = full_house(player_1_hand) self.assertEqual([2, 2, 2, 3, 3], rank_scores)
def test_royal_flush(self): player_1_hand, player_2_hand = parse_hands("TH JH QH KH AH QC TC 6D 7C KS") rank_scores = royal_flush(player_1_hand) self.assertEqual([14, 13, 12, 11, 10], rank_scores)
def test_one_pair(self): player_1_hand, player_2_hand = parse_hands("8H 2D 8S 3C 6S QC TC 6D 7C KS") rank_scores = one_pair(player_1_hand) self.assertEqual([8, 8], rank_scores)
def test_test_two_pairs(self): player_1_hand, player_2_hand = parse_hands("6H 2D 8S 2C 6S QC TC 6D 7C KS") rank_scores = two_pairs(player_1_hand) self.assertEqual([6, 6, 2, 2], rank_scores)
def test_three_of_a_kind(self): player_1_hand, player_2_hand = parse_hands("6H 2D 2S 2C 6S QC TC 6D 7C KS") rank_scores = three_of_a_kind(player_1_hand) self.assertEqual([2, 2, 2], rank_scores)
def test_straight(self): player_1_hand, player_2_hand = parse_hands("2H 3H 4D 5C 6S QC TC 6D 7C KS") rank_scores = straight(player_1_hand) self.assertEqual([6, 5, 4, 3, 2], rank_scores)
def test_flush(self): player_1_hand, player_2_hand = parse_hands("2H 3H JH 6H AH QC TC 6D 7C KS") rank_scores = flush(player_1_hand) self.assertEqual([14, 11, 6, 3, 2], rank_scores)
def test_invalid_input(self): with self.assertRaises(InvalidPokerHand): parse_hands("KH 4H AS JS QS QC TC 6D 7C KS 2H")
def test_four_of_a_kind(self): player_1_hand, player_2_hand = parse_hands("2H 2D 2C 2S 6H QC TC 6D 7C KS") rank_scores = four_of_a_kind(player_1_hand) self.assertEqual([2, 2, 2, 2], rank_scores)