def test_dealCardsToTable(): """ Test dealing of cards to the table """ # Create the player player = Player(money=100, name="John") player2 = Player(money=100, name="Sue") # Create the UI ui = UI() # Get our rule set houseRules = ui.selectHouseRules("Mystic Lake -- Shakopee, MN") # Set up the table table = Table() table.addPlayer(player) table.addPlayer(player2) # We want to hit all these cases cases = { "Insurance": False, "NotInsurance": False, "BlackJack": False, "NotBlackJack": False } # TODO: Maybe change how I check these paths? Technically this could run indefinitely long :-/ while True: # Set up the dealer each time to ensure we don't run out of cards... dealer = Dealer(houseRules=houseRules, ui=ui) table.setDealer(dealer) # Clear the table table.reset() # Do the dealing insurance, blackjack = dealer.dealHandsToTable(table) # Basic sanity checks assert len(player.getHands()) == 1 assert len(player2.getHands()) == 1 assert len(dealer.getHands()) == 1 assert len(player.getHand().getCards()) == 2 assert len(player2.getHand().getCards()) == 2 assert len(dealer.getHand().getCards()) == 2 # Update our cases to track what we've seen cases["Insurance"] |= insurance cases["NotInsurance"] |= not insurance cases["BlackJack"] |= blackjack cases["NotBlackJack"] |= not blackjack # See if we've exercized every case yet if set(cases.values()) == {True}: break
def test_playActiveHands(monkeypatch): """ Testing playing active hands """ # Create the table table = Table() # Create the UI ui = UI(table) # Get our rule set houseRules = ui.selectHouseRules("Mystic Lake -- Shakopee, MN") # Set up the dealer (don't waste time between cards) dealer = Dealer(houseRules=houseRules,ui=ui,dealCardDelay=0) # Set up the players player = Player(money=100,name="Ted") player2 = Player(money=100,name="James") # Add to table table.addPlayer(player) table.addPlayer(player2) table.setDealer(dealer) # Add hands hand = Hand(Card("5","Club"),Card("5","Spade")) hand2 = Hand(Card("6","Club"),Card("7","Diamond")) player.addHand(hand) player.addHand(hand2) hand3 = Hand(Card("A","Spade"),Card("7","Club")) player2.addHand(hand3) hand4 = Hand(Card("5","Diamond"),Card("7","Spade")) dealer.addHand(hand4) # Interaction here can get ugly. We're only going to test # How many times we get our hands run def countHands(x,y): global count count += 1 global count count = 0 # Add the monkeypatch monkeypatch.setattr(dealer,"facilitatePlayerHand",countHands) monkeypatch.setattr(dealer,"playDealersHand",lambda _: None) # Try it out table.playActiveHands() # Make sure we did all hands assert count == 3
def test_dealCardsToTable(): """ Test dealing of cards to the table """ # Create the player player = Player(money=100,name="John") player2 = Player(money=100,name="Sue") # Create the UI ui = UI() # Get our rule set houseRules = ui.selectHouseRules("Mystic Lake -- Shakopee, MN") # Set up the table table = Table() table.addPlayer(player) table.addPlayer(player2) # We want to hit all these cases cases = {"Insurance":False,"NotInsurance":False,"BlackJack":False,"NotBlackJack":False} # TODO: Maybe change how I check these paths? Technically this could run indefinitely long :-/ while True: # Set up the dealer each time to ensure we don't run out of cards... dealer = Dealer(houseRules=houseRules,ui=ui) table.setDealer(dealer) # Clear the table table.reset() # Do the dealing insurance,blackjack = dealer.dealHandsToTable(table) # Basic sanity checks assert len(player.getHands()) == 1 assert len(player2.getHands()) == 1 assert len(dealer.getHands()) == 1 assert len(player.getHand().getCards()) == 2 assert len(player2.getHand().getCards()) == 2 assert len(dealer.getHand().getCards()) == 2 # Update our cases to track what we've seen cases["Insurance"] |= insurance cases["NotInsurance"] |= not insurance cases["BlackJack"] |= blackjack cases["NotBlackJack"] |= not blackjack # See if we've exercized every case yet if set(cases.values()) == {True}: break
def test_placeAndClearBets(monkeypatch): """ Testing placing and clearing bets """ table = Table() player = Player(money=100,name="Steve") player2 = Player(money=100,name="Bob") table.addPlayer(player) table.addPlayer(player2) # Need to monkeypatch input #global count = 0 def placeBet(): global count if count == 0: count += 1 self.placeBet(10) else: self.placeBet(20) # Since we're basically changing the call variables # we need to copy the original method and call it separately player.placeBetOriginal = player.placeBet player2.placeBetOriginal = player2.placeBet monkeypatch.setattr(player,"placeBet",lambda : player.placeBetOriginal(10)) monkeypatch.setattr(player2,"placeBet",lambda : player2.placeBetOriginal(20)) # Place the bets table.placeBets() # Test that it worked assert len(player.getBets()) == 1 assert player.getBets()[0] == 10 assert len(player2.getBets()) == 1 assert player2.getBets()[0] == 20 # Now clear them table.clearBets() assert len(player.getBets()) == 0 assert len(player2.getBets()) == 0
def test_payoutTable(): """ Testing payoutTable method """ # Create the table table = Table() # Create the UI ui = UI(table) # Get our rule set houseRules = ui.selectHouseRules("Mystic Lake -- Shakopee, MN") # Set up the dealer (don't waste time between cards) dealer = Dealer(houseRules=houseRules, ui=ui, dealCardDelay=0) # Set up the player player = Player(money=100, name="Bill") player2 = Player(money=100, name="Dave") table.addPlayer(player) table.addPlayer(player2) table.setDealer(dealer) ######################################### # Dealer is busted / Player 2 is busted # ######################################### # Bet player.placeBet(5) player2.placeBet(10) hand = Hand(Card("K", "Spade"), Card("Q", "Spade")) hand.addCard(Card("5", "Club")) # He busted player2.addHand(hand) # Player 1 hasn't busted hand2 = Hand(Card("J", "Spade"), Card("2", "Club")) player.addHand(hand2) # Dealer has busted hand3 = Hand(Card("5", "Diamond"), Card("7", "Club")) hand3.addCard(Card("10", "Club")) dealer.addHand(hand3) # Play it dealer.payoutTable(table) # Test it assert player.getMoney() == 105 assert player2.getMoney() == 90 ########################################### # Dealer Beats Player 1 / Pushes Player 2 # ########################################### table.reset() # Bet player.placeBet(10) player2.placeBet(20) player.clearHands() hand = Hand(Card("K", "Spade"), Card("5", "Spade")) player.addHand(hand) player2.clearHands() hand2 = Hand(Card("J", "Spade"), Card("8", "Club")) player2.addHand(hand2) # Dealer has 18 dealer.clearHands() hand3 = Hand(Card("8", "Diamond"), Card("J", "Club")) dealer.addHand(hand3) # Play it dealer.payoutTable(table) # Test it assert player.getMoney() == 95 assert player2.getMoney() == 90
def test_playDealersHand(monkeypatch): """ Test playDealersHand method """ # Create the table table = Table() # Create the UI ui = UI(table) # Get our rule set houseRules = ui.selectHouseRules("Mystic Lake -- Shakopee, MN") # Set up the dealer (don't waste time between cards) dealer = Dealer(houseRules=houseRules, ui=ui, dealCardDelay=0) # Patching UI call as we can test that separately monkeypatch.setattr(dealer.ui, "drawTable", lambda: None) # Set up the player player = Player(money=100, name="Mike") ############################ # No active hands at table # ############################ # Give the player a busted hand hand = Hand(Card("10", "Diamond"), Card("10", "Spade")) hand.addCard(Card("5", "Spade")) player.addHand(hand) # Give the dealer a hand hand2 = Hand(Card("5", "Spade"), Card("10", "Club")) dealer.addHand(hand2) # Setup table table.addPlayer(player) table.setDealer(dealer) # Play the dealer dealer.playDealersHand(table) # We should not have gotten any cards assert len(dealer.getHand().getCards()) == 2 ################ # Dealer Busts # ################ # Give the player a busted hand player.clearHands() hand = Hand(Card("10", "Diamond"), Card("5", "Spade")) player.addHand(hand) # Give the dealer a hand dealer.clearHands() hand2 = Hand(Card("5", "Spade"), Card("10", "Club")) dealer.addHand(hand2) # Make sure the next card busts him dealer.shoe.cards[0] = Card("K", "Club") dealer.playDealersHand(table) # Check that he busted assert dealer.getHand().isBusted() assert len(dealer.getHand().getCards()) == 3 ############################ # Dealer Stands on Hard 17 # ############################ # Give the player a busted hand player.clearHands() hand = Hand(Card("10", "Diamond"), Card("5", "Spade")) player.addHand(hand) # Give the dealer a hand dealer.clearHands() hand2 = Hand(Card("5", "Spade"), Card("10", "Club")) dealer.addHand(hand2) # Make sure the next card busts him dealer.shoe.cards[0] = Card("2", "Club") dealer.playDealersHand(table) # Check our results assert not dealer.getHand().isBusted() assert len(dealer.getHand().getCards()) == 3 assert dealer.getHand().getValue()[-1] == 17 ############################ # Dealer Stands on Soft 18 # ############################ # Give the player a busted hand player.clearHands() hand = Hand(Card("10", "Diamond"), Card("5", "Spade")) player.addHand(hand) # Give the dealer a hand dealer.clearHands() hand2 = Hand(Card("A", "Spade"), Card("7", "Club")) dealer.addHand(hand2) dealer.playDealersHand(table) # Check our results assert not dealer.getHand().isBusted() assert len(dealer.getHand().getCards()) == 2 assert dealer.getHand().getValue()[-1] == 18 ################################# # Dealer Hits on Soft 16 and 17 # ################################# # Give the player a busted hand player.clearHands() hand = Hand(Card("10", "Diamond"), Card("5", "Spade")) player.addHand(hand) # Give the dealer a hand dealer.clearHands() hand2 = Hand(Card("A", "Spade"), Card("5", "Club")) dealer.addHand(hand2) # Next two cards to be dealt dealer.shoe.cards[0] = Card("A", "Spade") dealer.shoe.cards[1] = Card("K", "Club") dealer.playDealersHand(table) # Check our results assert not dealer.getHand().isBusted() assert len(dealer.getHand().getCards()) == 4 assert dealer.getHand().getValue()[-1] == 17 ############################ # Dealer Stands on Soft 17 # ############################ # Get our rule set houseRules = ui.selectHouseRules("Generic -- Liberal Two Deck") # Set up the dealer (don't waste time between cards) dealer = Dealer(houseRules=houseRules, ui=ui, dealCardDelay=0) # Patching UI call as we can test that separately monkeypatch.setattr(dealer.ui, "drawTable", lambda: None) # Re-add the dealer table.setDealer(dealer) # Give the player a busted hand player.clearHands() hand = Hand(Card("10", "Diamond"), Card("5", "Spade")) player.addHand(hand) # Give the dealer a hand dealer.clearHands() hand2 = Hand(Card("A", "Spade"), Card("6", "Club")) dealer.addHand(hand2) dealer.playDealersHand(table) # Check our results assert not dealer.getHand().isBusted() assert len(dealer.getHand().getCards()) == 2 assert dealer.getHand().getValue()[-1] == 17 assert len(dealer.getHand().getValue()) == 2
""" clearScreen() print( """.------..------..------..------..------..------..------..------..------..------..------. |P.--. ||Y.--. ||B.--. ||L.--. ||A.--. ||C.--. ||K.--. ||J.--. ||A.--. ||C.--. ||K.--. | | :/\: || (\/) || :(): || :/\: || (\/) || :/\: || :/\: || :(): || (\/) || :/\: || :/\: | | (__) || :\/: || ()() || (__) || :\/: || :\/: || :\/: || ()() || :\/: || :\/: || :\/: | | '--'P|| '--'Y|| '--'B|| '--'L|| '--'A|| '--'C|| '--'K|| '--'J|| '--'A|| '--'C|| '--'K| `------'`------'`------'`------'`------'`------'`------'`------'`------'`------'`------' """) # Init the table table = Table() # Create UI ui = UI(table) # Welcome banner printBanner() # Get house rules to play by houseRules = ui.selectHouseRules() ui.printHouseRules(houseRules) name = input("What's your name?: ") money = input("How much money to start with?: ")
def test_payoutTable(): """ Testing payoutTable method """ # Create the table table = Table() # Create the UI ui = UI(table) # Get our rule set houseRules = ui.selectHouseRules("Mystic Lake -- Shakopee, MN") # Set up the dealer (don't waste time between cards) dealer = Dealer(houseRules=houseRules,ui=ui,dealCardDelay=0) # Set up the player player = Player(money=100,name="Bill") player2 = Player(money=100,name="Dave") table.addPlayer(player) table.addPlayer(player2) table.setDealer(dealer) ######################################### # Dealer is busted / Player 2 is busted # ######################################### # Bet player.placeBet(5) player2.placeBet(10) hand = Hand(Card("K","Spade"),Card("Q","Spade")) hand.addCard(Card("5","Club")) # He busted player2.addHand(hand) # Player 1 hasn't busted hand2 = Hand(Card("J","Spade"),Card("2","Club")) player.addHand(hand2) # Dealer has busted hand3 = Hand(Card("5","Diamond"),Card("7","Club")) hand3.addCard(Card("10","Club")) dealer.addHand(hand3) # Play it dealer.payoutTable(table) # Test it assert player.getMoney() == 105 assert player2.getMoney() == 90 ########################################### # Dealer Beats Player 1 / Pushes Player 2 # ########################################### table.reset() # Bet player.placeBet(10) player2.placeBet(20) player.clearHands() hand = Hand(Card("K","Spade"),Card("5","Spade")) player.addHand(hand) player2.clearHands() hand2 = Hand(Card("J","Spade"),Card("8","Club")) player2.addHand(hand2) # Dealer has 18 dealer.clearHands() hand3 = Hand(Card("8","Diamond"),Card("J","Club")) dealer.addHand(hand3) # Play it dealer.payoutTable(table) # Test it assert player.getMoney() == 95 assert player2.getMoney() == 90
def test_playDealersHand(monkeypatch): """ Test playDealersHand method """ # Create the table table = Table() # Create the UI ui = UI(table) # Get our rule set houseRules = ui.selectHouseRules("Mystic Lake -- Shakopee, MN") # Set up the dealer (don't waste time between cards) dealer = Dealer(houseRules=houseRules,ui=ui,dealCardDelay=0) # Patching UI call as we can test that separately monkeypatch.setattr(dealer.ui,"drawTable",lambda : None) # Set up the player player = Player(money=100,name="Mike") ############################ # No active hands at table # ############################ # Give the player a busted hand hand = Hand(Card("10","Diamond"),Card("10","Spade")) hand.addCard(Card("5","Spade")) player.addHand(hand) # Give the dealer a hand hand2 = Hand(Card("5","Spade"),Card("10","Club")) dealer.addHand(hand2) # Setup table table.addPlayer(player) table.setDealer(dealer) # Play the dealer dealer.playDealersHand(table) # We should not have gotten any cards assert len(dealer.getHand().getCards()) == 2 ################ # Dealer Busts # ################ # Give the player a busted hand player.clearHands() hand = Hand(Card("10","Diamond"),Card("5","Spade")) player.addHand(hand) # Give the dealer a hand dealer.clearHands() hand2 = Hand(Card("5","Spade"),Card("10","Club")) dealer.addHand(hand2) # Make sure the next card busts him dealer.shoe.cards[0] = Card("K","Club") dealer.playDealersHand(table) # Check that he busted assert dealer.getHand().isBusted() assert len(dealer.getHand().getCards()) == 3 ############################ # Dealer Stands on Hard 17 # ############################ # Give the player a busted hand player.clearHands() hand = Hand(Card("10","Diamond"),Card("5","Spade")) player.addHand(hand) # Give the dealer a hand dealer.clearHands() hand2 = Hand(Card("5","Spade"),Card("10","Club")) dealer.addHand(hand2) # Make sure the next card busts him dealer.shoe.cards[0] = Card("2","Club") dealer.playDealersHand(table) # Check our results assert not dealer.getHand().isBusted() assert len(dealer.getHand().getCards()) == 3 assert dealer.getHand().getValue()[-1] == 17 ############################ # Dealer Stands on Soft 18 # ############################ # Give the player a busted hand player.clearHands() hand = Hand(Card("10","Diamond"),Card("5","Spade")) player.addHand(hand) # Give the dealer a hand dealer.clearHands() hand2 = Hand(Card("A","Spade"),Card("7","Club")) dealer.addHand(hand2) dealer.playDealersHand(table) # Check our results assert not dealer.getHand().isBusted() assert len(dealer.getHand().getCards()) == 2 assert dealer.getHand().getValue()[-1] == 18 ################################# # Dealer Hits on Soft 16 and 17 # ################################# # Give the player a busted hand player.clearHands() hand = Hand(Card("10","Diamond"),Card("5","Spade")) player.addHand(hand) # Give the dealer a hand dealer.clearHands() hand2 = Hand(Card("A","Spade"),Card("5","Club")) dealer.addHand(hand2) # Next two cards to be dealt dealer.shoe.cards[0] = Card("A","Spade") dealer.shoe.cards[1] = Card("K","Club") dealer.playDealersHand(table) # Check our results assert not dealer.getHand().isBusted() assert len(dealer.getHand().getCards()) == 4 assert dealer.getHand().getValue()[-1] == 17 ############################ # Dealer Stands on Soft 17 # ############################ # Get our rule set houseRules = ui.selectHouseRules("Generic -- Liberal Two Deck") # Set up the dealer (don't waste time between cards) dealer = Dealer(houseRules=houseRules,ui=ui,dealCardDelay=0) # Patching UI call as we can test that separately monkeypatch.setattr(dealer.ui,"drawTable",lambda : None) # Re-add the dealer table.setDealer(dealer) # Give the player a busted hand player.clearHands() hand = Hand(Card("10","Diamond"),Card("5","Spade")) player.addHand(hand) # Give the dealer a hand dealer.clearHands() hand2 = Hand(Card("A","Spade"),Card("6","Club")) dealer.addHand(hand2) dealer.playDealersHand(table) # Check our results assert not dealer.getHand().isBusted() assert len(dealer.getHand().getCards()) == 2 assert dealer.getHand().getValue()[-1] == 17 assert len(dealer.getHand().getValue()) == 2
""" Print out "pyBlackJack" in ASCII art characters """ clearScreen() print(""".------..------..------..------..------..------..------..------..------..------..------. |P.--. ||Y.--. ||B.--. ||L.--. ||A.--. ||C.--. ||K.--. ||J.--. ||A.--. ||C.--. ||K.--. | | :/\: || (\/) || :(): || :/\: || (\/) || :/\: || :/\: || :(): || (\/) || :/\: || :/\: | | (__) || :\/: || ()() || (__) || :\/: || :\/: || :\/: || ()() || :\/: || :\/: || :\/: | | '--'P|| '--'Y|| '--'B|| '--'L|| '--'A|| '--'C|| '--'K|| '--'J|| '--'A|| '--'C|| '--'K| `------'`------'`------'`------'`------'`------'`------'`------'`------'`------'`------' """) # Init the table table = Table() # Create UI ui = UI(table) # Welcome banner printBanner() # Get house rules to play by houseRules = ui.selectHouseRules() ui.printHouseRules(houseRules) name = input("What's your name?: ") money = input("How much money to start with?: ")