def test_chips_types(self): #Make sure type errors are raised test_chips_values = Chips() test_chips_values.bet = 'hey' test_chips_values.total = 'waht' self.assertRaises(TypeError, Chips.take_bet, test_chips_values)
def test_chips_values(self): #Make sure negative value errors are raised test_chips = Chips() test_chips.bet = -1 test_chips.total = 100 self.assertRaises(ValueError, Chips.take_bet, test_chips)
def computer_take_bet(chips: Chips): if chips.money >= 100: chips.bet = 50 chips.money -= 50 elif chips.money >= 50 < 100: chips.bet = 25 chips.money -= 25 elif chips.money > 10 < 50: chips.bet = 5 chips.money -= 5 else: chips.bet = 1 chips.money -= 1
def take_bet(chips: Chips) -> None: while True: try: chips.bet = int(input('How many chips would you like to bet? ')) except: print('Sorry, please provide an integer') else: if chips.bet > chips.total: print( f'Sorry, you don\'t have enough chips! You have {chips.total}' ) else: break
def main(): while True: print("Welcome to Black Jack!") """ Create & shuffle the deck, deal two cards to each player """ deck = Deck() deck.shuffle() player = Hand() # player's hand dealer = Hand() # dealer's hand for i in range(0, 2): player.add_card(deck.deal()) dealer.add_card(deck.deal()) """ Prompt the Player for their bet and set up the Player's chips """ chips_info = take_bet() chips = Chips(chips_info[0], chips_info[1]) show_some(player, dealer) while playing: hit_or_stand(deck, player) show_some(player, dealer) if player.value > 21: player_loses(chips) break if player.value <= 21: # If Player hasn't busted, play Dealer's hand until Dealer reaches or exceeds 17 while dealer.value < 17: hit(deck, dealer) show_all(player, dealer) """ Run different winning scenarios """ if dealer.value > 21 or player.value > dealer.value: player_wins(chips) elif player.value < dealer.value: player_loses(chips) print("You now have {} chips.\n").format(chips.total) play_again = raw_input( "Would you like to play again? yes/no \n").lower() if play_again != "yes": print("Thanks for playing!\n") break
def take_bet(chips: Chips): while True: print(f'Place bet, you have {chips.money} avaliable:') try: chips.bet = int(input()) except ValueError: print( 'INVALID INPUT, must be a number... \nyou imbecile muttonhead..' ) else: if chips.bet <= 0: print( f'INVALID, number must be over zero. \n-???... errr.. no words.. oh wait, ..you moron.' ) elif chips.bet > chips.money: print( f'You cannot bet {chips.bet}, when you only have {chips.money} avaliable. \n-did you even graduated kindergarden!! haha, you bonehead' ) else: chips.money -= chips.bet break
def bust(hand): return hand.value > 21 def player_win(): print("You win!!!") playerchip.win_bet() def player_lose(): print("You lose!!!") playerchip.lose_bet() playerchip = Chips() while playagain: deck = Deck() deck.suffer() playerhand = Hand() dealerhand = Hand() playerchip.take_bet() hit(playerhand, deck) hit(playerhand, deck) hit(dealerhand, deck) hit(dealerhand, deck) display(playerhand, dealerhand, playerchip, False)
''' BlackJack main logic file ''' from hand import Hand from chips import Chips from deck import Deck import actions import show players_chips = Chips(200) while True: show.clear_screen() playing_deck = Deck() dealer = Hand() player = Hand() print("Hello, player! This is Black Jack game! Let's start") actions.take_bet(players_chips) for i in range(0, 4): if i in range(0, 2): actions.hit(playing_deck, dealer) else: actions.hit(playing_deck, player) playing = True dealer_playing = True while playing: show.clear_screen() #print(f"Your chips <<{players_chips.total}>>") print(f"Your bet <<{players_chips.bet}>>") show.show_some(player, dealer)
class Player: # Create instans of Chips class # chips is now an Attribute of the class Player chips = Chips() # Lifecycle for a game as a player def player_game(player): # Empty cards for every new game # Player cards player_cards = [] # Dealer cards dealer_cards = [] if player == "play": # Player makes the bet Chips.take_bet(Player.chips) # Deal the cards # Display the cards # Dealer cards while len(dealer_cards) != 2: dealer_cards.append(random.randint(1, 11)) if len(dealer_cards) == 2: print("Dealer has: [ X &", dealer_cards[1], "]") # Deal the cards # Display the cards # Player cards while len(player_cards) != 2: player_cards.append(random.randint(1, 11)) if len(player_cards) == 2: print("You have: [", player_cards[0], "&", player_cards[1], "]") # Ask if the player wants to stay or hit while the player_card sum is under 21 while sum(player_cards) < 21: # Ask player: stay or hit action_taken = str( input("Do you want to stay or hit? " + "\n" + "-" * 40)) # Answer: hit --> give new card to the player if action_taken == "hit": player_cards.append(random.randint(1, 11)) print( "You now have a total of " + str(sum(player_cards)) + " from these cards ", player_cards) # Answer: not hit and sum of dealer_cards == 21 --> dealer wins elif sum(dealer_cards) == 21: time.sleep(2) print( "The dealer has BLACKJACK! " + str(sum(dealer_cards)) + " with ", dealer_cards) print( "You have a total of " + str(sum(player_cards)) + " with ", player_cards) print("DEALER WINS!") print("-" * 40) Chips.lose_bet(Player.chips) break # Answer: stay elif action_taken == "stay": # if sum of dealer_cards is higher than player_cards --> Dealer wins if sum(dealer_cards) > sum(player_cards): time.sleep(2) print( "The dealer has a total of " + str(sum(dealer_cards)) + " with ", dealer_cards) print( "You have a total of " + str(sum(player_cards)) + " with ", player_cards) print("DEALER WINS!") print("-" * 40) Chips.lose_bet(Player.chips) break else: # If sum of dealer_cards is under 21 --> give dealer new card while sum(dealer_cards) < 21: print("The dealer turns a card!") time.sleep(2) dealer_cards.append(random.randint(1, 11)) print( "The dealer has a total of " + str(sum(dealer_cards)) + " with ", dealer_cards) print("-" * 40) time.sleep(2) # If sum of dealer_cards is higher than player_cards and under 21 --> Dealer wins if (sum(dealer_cards) > sum(player_cards) and sum(dealer_cards) < 21): print( "The dealer has a total of " + str(sum(dealer_cards)) + " with ", dealer_cards) print( "You have a total of " + str(sum(player_cards)) + " with ", player_cards) print("DEALER WINS!") print("-" * 40) Chips.lose_bet(Player.chips) break # If the sum of the cards are equal --> Dealer wins elif sum(dealer_cards) == sum(player_cards): print( "The dealer has a total of " + str(sum(dealer_cards)) + " with ", dealer_cards) print( "You have a total of " + str(sum(player_cards)) + " with ", player_cards) print("DEALER WINS!") print("-" * 40) Chips.lose_bet(Player.chips) break # If the sum of dealer_cards is 21 --> Dealer wins elif sum(dealer_cards) == 21: print( "The dealer has BLACKJACK! " + str(sum(dealer_cards)) + " with ", dealer_cards) print( "You have a total of " + str(sum(player_cards)) + " with ", player_cards) print("DEALER WINS!") print("-" * 40) Chips.lose_bet(Player.chips) # If the sum of dealer_cards is higher than 21 --> You win elif sum(dealer_cards) > 21: print( "The dealer has busted! with " + str(sum(dealer_cards)), dealer_cards) print( "You have a total of " + str(sum(player_cards)) + " with ", player_cards) print("YOU WIN!") print("-" * 40) Chips.win_bet(Player.chips) break # If player answer is not equal to stay or hit else: print( 'That is not possible, you have to enter "stay" or "hit"!' ) # If the sum of player_cards is higher than 21 --> Dealer wins if sum(player_cards) > 21: time.sleep(2) print("YOU BUSTED! DEALER WINS!") print("-" * 40) Chips.lose_bet(Player.chips) # You have BLACKJACK elif sum(player_cards) == 21: time.sleep(2) print("You have BLACKJACK! You Win! 21") print("-" * 40) Chips.win_bet(Player.chips)
def __init__(self, bank=100): Hand.__init__(self) Chips.__init__(self, bank)
def player_busts(player: Hand, dealer: Hand, chips: Chips) -> None: print('Player busts') chips.lose_bet()
from deck import Deck from chips import Chips from hand import Hand import hit_or_stand import take_bet import show import bust_or_win playing = True new_game = True player_chips = Chips(100) while new_game: # Print an opening statement print("Welcome to Black Jack!") # Create & shuffle the deck, deal two cards to each player deck = Deck() deck.shuffle() player_hand = Hand() player_hand.add_card(deck.deal()) player_hand.add_card(deck.deal()) dealer_hand = Hand() dealer_hand.add_card(deck.deal()) dealer_hand.add_card(deck.deal()) # Set up the Player's chips
def play(): # the game # flags for game states is_playing = True is_continue = True is_drawing_card = True while is_playing: # reset them to true if they were false and started a new game is_continue = True is_drawing_card = True clear_terminal() # get if the user is a player or a dealer role = promt_player_dealer() clear_terminal() # get the players money money = promt_money_amount() clear_terminal() # create the playeres with an empty hand if role == Player.PLAYER: player = Player(Player.PLAYER, Hand(), Chips(money), Player.YOU) dealer = Player(Player.DEALER, Hand(), Chips(0), Player.COMPUTER) else: player = Player(Player.PLAYER, Hand(), Chips(money), Player.COMPUTER) dealer = Player(Player.DEALER, Hand(), Chips(0), Player.YOU) # play round while, player money > 0 while is_continue: is_drawing_card = True # create a deck and shuffle the deck deck = Deck() random.shuffle(deck.cards) # get bet for the player if role == player.PLAYER: clear_terminal() take_bet(player.chips) else: clear_terminal() computer_take_bet(player.chips) # clear the hands if they already placed a round player.hand = Hand() dealer.hand = Hand() # deal starting hand player.hand.add_card(deck.deal()) player.hand.add_card(deck.deal()) dealer.hand.add_card(deck.deal()) dealer.hand.add_card(deck.deal()) # game logic for when the user is the player if role == player.PLAYER: # show starting hand clear_terminal_show_cards(player, dealer, True) # deal cards until the player stops while is_drawing_card == True and calculate_hand( player.hand.cards) < 21: is_drawing_card = prompt_hit_stand(player.hand, deck, hit) clear_terminal_show_cards(player, dealer, True) # if drawing was cancelled by 21 set it to false is_drawing_card = False # calculate totals for player player_total = calculate_hand(player.hand.cards) # see if the player lost or if the dealer have to draw cards if player_total > 21: you_lost(role, player) else: # show dealers card clear_terminal_show_cards(player, dealer, False) # dealers turn while calculate_hand(dealer.hand.cards) < 17: hit(dealer.hand, deck) clear_terminal_show_cards(player, dealer, False) dealer_total = calculate_hand(dealer.hand.cards) # calculate who wins if dealer_total > 21: you_won(role, player) elif dealer_total < player_total: you_won(role, player) elif dealer_total == player_total: tie(role, player) else: you_lost(role, player) # game when user is the dealer else: dealer_total = calcultate_one_card(dealer.hand.cards[1]) # logic for computer to hit computer_is_playing = True while computer_is_playing: clear_terminal_show_cards(player, dealer, True) prompt_user_to_continue() player_total = calculate_hand(player.hand.cards) if player_total <= 11: hit(player.hand, deck) elif dealer_total >= 10 and player_total <= 17: hit(player.hand, deck) elif dealer_total < 10 and player_total <= 15: hit(player.hand, deck) else: break # calculate totals for player player_total = calculate_hand(player.hand.cards) # see if the player lost or the dealer have to play if player_total > 21: you_won(role, player) else: clear_terminal_show_cards(player, dealer, False) prompt_user_to_continue() # dealers turn while calculate_hand(dealer.hand.cards) < 17: hit(dealer.hand, deck) clear_terminal_show_cards(player, dealer, False) prompt_user_to_continue() dealer_total = calculate_hand(dealer.hand.cards) if dealer_total > 21: you_lost(role, player) elif dealer_total < player_total: you_lost(role, player) elif dealer_total == player_total: tie(role, player) else: you_won(role, player) # promt for continue, new game or quit if player.chips.money <= 0: is_continue = False is_continue = prompt_new_game() else: is_continue = promt_to_contionue()
def __init__(self, deck): self.my_hand = Hand() self.my_chips = Chips() self.deck = deck
Dealer hits until she reaches 17. Aces count as 1 or 11.') # Create & shuffle the deck, deal two cards to each player deck = Deck() deck.shuffle() player_hand = Hand() player_hand.add_card(deck.deal()) player_hand.add_card(deck.deal()) dealer_hand = Hand() dealer_hand.add_card(deck.deal()) dealer_hand.add_card(deck.deal()) # Set up the Player's chips player_chips = Chips() # remember the default value is 100 # Prompt the Player for their bet: take_bet(player_chips) # Show the cards: show_some(player_hand, dealer_hand) while playing: # recall this variable from our hit_or_stand function # Prompt for Player to Hit or Stand hit_or_stand(deck, player_hand) show_some(player_hand, dealer_hand) if player_hand.value > 21: player_busts(player_hand, dealer_hand, player_chips)
def continue_playing(): while True: ans = input('Continue playing? Yes or no: ') if ans.lower() != 'yes' and ans.lower() != 'no': print('Invalid input') else: return True if ans.lower() == 'yes' else False if __name__ == '__main__': ''' Initializing the chips ''' chips = Chips() # pass value if you want to start with a different totals. Default is 100 ''' Game Phase ''' playing = True while playing: ''' Initializing the deck ''' deck = Deck() init_deck(deck) deck.shuffle() ''' Taking a bet from a player
def player_wins(player: Hand, dealer: Hand, chips: Chips) -> None: print('Player wins') chips.win_bet()
def dealer_busts(player: Hand, dealer: Hand, chips: Chips) -> None: print('Dealer busts') chips.win_bet()
def dealer_wins(player: Hand, dealer: Hand, chips: Chips) -> None: print('Dealer wins') chips.lose_bet()
deck.shuffle() # print(deck) # 2. setup the player and dealers initial hands player_hand = Hand() player_hand.add_card(deck.deal()) player_hand.add_card(deck.deal()) # two cards! # print(player_hand) dealer_hand = Hand() dealer_hand.add_card(deck.deal()) dealer_hand.add_card(deck.deal()) # print(dealer_hand) # 3. setup the players chips player_chips = Chips() # 4. prompt player for their bet: take_bet(player_chips) # 5. show cards show_some(player_hand, dealer_hand) # 6. betting! while playing: # this comes from the hit or stand function # prompt player to hit or stand hit_or_stand(deck, player_hand) # show cards (keep one from dealer hidden) show_some(player_hand, dealer_hand)
# Create & shuffle the deck, deal two cards to each player the_deck = Deck() the_deck.shuffle() player_one = Hand() dealer = Hand() player_one.add_card(the_deck.deal()) dealer.add_card(the_deck.deal()) player_one.add_card(the_deck.deal()) dealer.add_card(the_deck.deal()) # Set up the Player's chips deposit_chips = int(input("How many chips do you want to deposit? ")) player_chips = Chips(deposit_chips) # Prompt the Player for their bet take_bet(player_chips) # Show cards (but keep one dealer card hidden) show_some(player_one, dealer) while playing: # recall this variable from our hit_or_stand function # Prompt for Player to Hit or Stand playing = hit_or_stand(the_deck, player_one) # Show cards (but keep one dealer card hidden) show_some(player_one, dealer)