Пример #1
0
    def test_bust__hand1(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        for I in range(0, 3):
            card1 = self.generateCard(value=10, face=str(10), is_ace=False)
            card2 = self.generateCard(value=10, face=str(10), is_ace=False)
            testPlayer.add_card(card1, card2)

        self.assertTrue(testPlayer.check_if_bust())
    def test_bust_hand2_only(self):
        from src.player import Player

        testPlayer = Player()
        testPlayer.__set_split__()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        for I in range(0, 3):
            card2 = self.generateCard(value=10, face=str(10), is_ace=False)
            card1 = self.generateCard(value=2, face=str(2), is_ace=False)
            testPlayer.add_card(card1)

        self.assertFalse(testPlayer.check_if_bust())
    def test_can_split_true_when_able_fcn(self):
        from src.player import Player

        testPlayer = Player()

        # Generate Two cards and add them to hand
        testPlayer.add_card(
            self.generateCard(value=3, face=str(3), is_ace=False))
        testPlayer.add_card(
            self.generateCard(value=2, face=str(2), is_ace=False))

        canSplitStatus = testPlayer.__can_split__()

        self.assertFalse(canSplitStatus)
    def test_can_split_true_when_able_fcn(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        for card in range(0, 2):
            testPlayer.add_card(
                self.generateCard(value=3, face=str(3), is_ace=False))

        canSplitStatus = testPlayer.__can_split__()

        self.assertTrue(canSplitStatus)
Пример #5
0
    def test_hand_output_returns_string(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        for card in range(0, 2):
            testPlayer.add_card(self.generateCard(value=3, face=str(3), is_ace=False))

        handOutput = testPlayer.get_hand_output()

        # Hand value should be 0
        self.assertIsInstance(handOutput, str)
Пример #6
0
    def test_hand_value(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        for card in range(0, 2):
            testPlayer.add_card(self.generateCard(value=3, face=str(3), is_ace=False))

        handValue = testPlayer.get_hand_value()

        # Hand value should be 6
        self.assertEqual(handValue, 6)
    def test_hand_value_returns_list_in_split(self):
        from src.player import Player

        testPlayer = Player()
        testPlayer.__set_split__()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        for card in range(0, 2):
            testPlayer.add_card(
                self.generateCard(value=3, face=str(3), is_ace=False))

        handValue = testPlayer.get_hand_value()

        # Hand value should be 6
        self.assertIsInstance(handValue, list)
    def test_hand1_value_in_split(self):
        from src.player import Player

        testPlayer = Player()
        testPlayer.__set_split__()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        for card in range(0, 2):
            testPlayer.add_card(
                self.generateCard(value=3, face=str(3), is_ace=False),
                self.generateCard(value=4, face=str(4), is_ace=False))

        handValue = testPlayer.get_hand_value()

        # Hand value should be 8
        self.assertEqual(handValue[1], 8)
Пример #9
0
    def test_add_card_to_player(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        for card in range(0, 2):
            testPlayer.add_card(self.generateCard(value=3, face=str(3), is_ace=False))

        plyrHand1 = testPlayer.hand1
        plyrHand2 = testPlayer.hand2

        numberOfCardsInHand = plyrHand1.get_number_of_cards()

        #number of cards in hand should be 2
        self.assertEqual(numberOfCardsInHand, 2)
Пример #10
0
    def test_reset_clears_hand2(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        for card in range(0, 2):
            testPlayer.add_card(self.generateCard(value=3, face=str(3), is_ace=False), self.generateCard(value=3, face=str(3), is_ace=False))


        testPlayer.reset()

        numberOfCardsInHand = testPlayer.hand1.get_number_of_cards()

        #number of cards in hand2 should be 0
        self.assertEqual(numberOfCardsInHand, 0)
    def test_split_status_is_set_after_split(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        card1 = self.generateCard(value=3, face=str(3), is_ace=False)
        card2 = self.generateCard(value=3, face=str(3), is_ace=False)

        testPlayer.add_card(card1)
        testPlayer.add_card(card2)

        testPlayer.split()

        splitStatus = testPlayer.__get_split_status__()

        self.assertTrue(splitStatus)
    def test_clear_hand2(self):
        from src.player import Player

        testPlayer = Player()
        testPlayer.__set_split__()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        for card in range(0, 2):
            testPlayer.add_card(
                self.generateCard(value=3, face=str(3), is_ace=False))

        # Clear the hand
        testPlayer.__clear_hand__()
        handValue = testPlayer.get_hand_value()[1]

        # Hand1 value should be 0
        self.assertEqual(handValue, 0)
    def test_cards_are_added_to_correct_hand_when_in_split_hand1(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        card1 = self.generateCard(value=3, face=str(3), is_ace=False)
        card2 = self.generateCard(value=4, face=str(4), is_ace=False)

        # Set split status
        testPlayer.__set_split__()

        testPlayer.add_card(card1, card2)

        handsFromPlayer = testPlayer.__get_hand_obj__()
        plyrHand1 = handsFromPlayer[0]
        plyrHand2 = handsFromPlayer[1]

        self.assertEqual(plyrHand1[0], card1)
    def test_hand2_has_1_card_after_split(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        card1 = self.generateCard(value=3, face=str(3), is_ace=False)
        card2 = self.generateCard(value=3, face=str(3), is_ace=False)

        testPlayer.add_card(card1)
        testPlayer.add_card(card2)

        testPlayer.split()

        handsFromPlayer = testPlayer.__get_hand_obj__()
        plyrHand1 = handsFromPlayer[0]
        plyrHand2 = handsFromPlayer[1]

        self.assertEqual(len(plyrHand2), 1)
    def test_add_card_in_split_when_2nd_card_not_provided_hand1(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        card1 = self.generateCard(value=3, face=str(3), is_ace=False)
        card2 = None

        # Set split status
        testPlayer.__set_split__()

        testPlayer.add_card(card1, card2)

        handsFromPlayer = testPlayer.__get_hand_obj__()
        plyrHand1 = handsFromPlayer[0]
        plyrHand2 = handsFromPlayer[1]

        self.assertEqual(len(plyrHand1), 0)
    def test_cards_are_moved_when_split(self):
        from src.player import Player

        testPlayer = Player()

        # Create two cards
        # Generate Two "3" cards and add them to hand
        card1 = self.generateCard(value=3, face=str(3), is_ace=False)
        card2 = self.generateCard(value=3, face=str(3), is_ace=False)

        testPlayer.add_card(card1)
        testPlayer.add_card(card2)

        testPlayer.split()

        # canSplitStatus = testPlayer.__can_split__()

        handsFromPlayer = testPlayer.__get_hand_obj__()
        plyrHand1 = handsFromPlayer[0]
        plyrHand2 = handsFromPlayer[1]

        self.assertEqual(plyrHand1[0], card1)
Пример #17
0
class Game():

    def __init__(self):
        self.player = Player()
        self.dealer = Dealer()
        self.deck = Deck()


    def run(self):
        # Deal cards
        self.new_run()

        # Print board
        self.print_board()

        # Player 1 goes first
        self.players_turn()

        self.print_board()

        # dealers turn
        self.dealer_turn()

        # check who won
        winner = self.check_winner()

        print(winner)

    def new_run(self):

        # Deal cards to player
        self.__deal_to_plyr__()


        # deal cards to dealer
        self.__deal_to_dealer__()

    def __deal_to_plyr__(self):
        for i in range(0,2):
            self.player.add_card(self.deck.get_card(), None)

    def __deal_to_dealer__(self):
        self.dealer.add_card(self.deck.get_card(), None)
        self.dealer.add_card(self.deck.get_hidden_card(), None)

    def players_turn(self):

        while True:
            #  Get players's decision 
            decision = self.player.play()

            # If stand then break the loop
            if decision == 2:
                break

            elif decision == 1: #IF the player hits
                self.hit_player()
                self.print_board()

            elif decision == 3: # Split for the player 
                self.split_player()
                self.print_board()

            elif decision == 4: # Insurer the player
                pass
        
            # Check if bust
            if self.player.check_if_bust():
                break

    def hit_player(self):
        # If player is in a split
        if self.player.__get_split_status__():
            self.player.add_card(card1=self.deck.get_card(), card2=self.deck.get_card())
        else:
            self.player.add_card(card1=self.deck.get_card(), card2=None)


    def split_player(self):
        self.player.split()
        self.hit_player()


    def insure_player(self):
        pass

    
    def hit_dealer(self):
        self.dealer.add_card(card1=self.deck.get_card(), card2=None)
        
    def dealer_reveal(self, inTest=False):
        # Reveal hidden card
        self.dealer.unhide_card()

        if not inTest:
            # Print board
            self.print_board()
            print("Dealer revealed hidden card")
            time.sleep(2)

    def dealer_turn(self):

        # Reveal hidden card
        self.dealer_reveal()

        
        while True:

            #  Get players's decision 
            decision = self.dealer.play()

            # If stand then break the loop
            if decision == 2:
                break

            elif decision == 1: #IF the dealer hits
                self.hit_dealer()

                self.print_board()
                print("Dealer chose to hit")
                time.sleep(1)

    # CHECK WINNER BY BUST
    def __check_tie_by_both_bust__(self, player_bust_status, dealer_bust_status):

        if (player_bust_status and dealer_bust_status):
            return True
        else:
            return False
    
    def __check_player_bust_and_dealer_hasnt__(self, player_bust_status, dealer_bust_status):
        if (player_bust_status and not dealer_bust_status):
            return True
        else:
            return False

    def __check_dealer_bust_and_player_hasnt__(self, player_bust_status, dealer_bust_status):
        if (not player_bust_status and dealer_bust_status):
            return True
        else:
            return False

    def __winner_by_bust__(self, player_bust_status, dealer_bust_status):
        # Tie if both bust
        if self.__check_tie_by_both_bust__(player_bust_status, dealer_bust_status):
            return self.tie()

        # If player bust and dealer hasn't
        if self.__check_player_bust_and_dealer_hasnt__(player_bust_status, dealer_bust_status):
            return self.bust()

        # If dealer bust and player hasn't
        if self.__check_dealer_bust_and_player_hasnt__(player_bust_status, dealer_bust_status):
            return self.winner()

        return None


    # CHECK WINNER BY VALUE
    def __check_plyr_hand_value_larger_than_dealer__(self, plyrHandValue, dealerHandValue):
        
        if plyrHandValue > dealerHandValue:
            return True
        else:
            return False

    def __check_player_and_dealer_hands_are_equal__(self, plyrHandValue, dealerHandValue):
        if plyrHandValue == dealerHandValue:
            return True
            
        else:
            return False

    def __winner_by_value__(self, plyrHand1Value, plyrHand2Value, dealerHandValue, playerSplit=False):

        # Check hand 1 or hand2 is larger than dealers
        if self.__check_plyr_hand_value_larger_than_dealer__(plyrHand1Value, dealerHandValue) or self.__check_plyr_hand_value_larger_than_dealer__(plyrHand2Value, dealerHandValue):
            return self.winner()
        
        # # Check hand 2 is larger than dealers
        # elif self.__check_plyr_hand_value_larger_than_dealer__(plyrHand2Value, dealerHandValue) and playerSplit:
        #     return self.winner()

        # Check if hand1 or is tied with dealer
        elif self.__check_player_and_dealer_hands_are_equal__(plyrHand1Value, dealerHandValue) or self.__check_player_and_dealer_hands_are_equal__(plyrHand2Value, dealerHandValue):
            return self.tie()

        # # Check if hand2 is tied with dealer
        # elif self.__check_player_and_dealer_hands_are_equal__(plyrHand2Value, dealerHandValue) and playerSplit:
        #     return self.tie()

        # Otherwise player lost
        else:
            return self.loose()




    def check_winner(self):
        player_bust_status = self.player.check_if_bust()
        dealer_bust_status = self.dealer.check_if_bust()
        
        dealer_hand_value = self.dealer.get_hand_value()
        # hand_values = self.player.get_hand_value()
        hand1_value = self.player.hand1.get_hand_value()
        hand2_value = self.player.hand2.get_hand_value()

        player_in_split = self.player.__get_split_status__()


        # Check winner by bust
        winnerByBustOutcome = self.__winner_by_bust__(player_bust_status, dealer_bust_status)

        if winnerByBustOutcome:
            return winnerByBustOutcome

        # Check Winner By Value

        winnerByValueOutcome = self.__winner_by_value__(hand1_value, hand2_value, dealer_hand_value, player_in_split)

        if winnerByValueOutcome:
            return winnerByValueOutcome

        return "UNKNOWN OUTCOME"

    def winner(self):
        return "You Win"
    
    def tie(self):
        return "Game Tied"

    def loose(self):
        return "You Lose"

    def bust(self):
        return "BUST - " + self.loose()


    def print_board(self):
        # Clear screen
        self.clear_screen()

        # print Dealer
        print("Dealer")
        print(self.dealer.get_hand_output())


        # print("\n\n")

        # print Player
        print("Player 1")
        print(self.player.get_hand_output())

        print("")

    def clear_screen(self):
        if platform.system()=="Windows":
            subprocess.Popen("cls", shell=True).communicate()
        else: #Linux and Mac
            print("\033c", end="")