def deal(self): print("Player - ${}".format(self.player.money)) self.deck = Deck() self.deck.shuffle() self.player.clear() self.dealer.clear() self.player.receive_card(self.deck.draw_card()) self.dealer.receive_card(self.deck.draw_card()) self.player.receive_card(self.deck.draw_card()) self.dealer.receive_card(self.deck.draw_card()) self.player_choice()
def __init__(self, human=False): self.human = human self.deck = Deck() self.players = [] # dealer goes to end of list self.players.append(self.create_dealer("Dealer", 1000000, self.deck)) # players get added to the front of list self.players.insert(0, self.create_player("Player_1", 200, self.deck)) self.winner = None self.loser = None self.pot = []
def __init__(self, human=False): self.human = human self.deck = Deck() self.players = [] # dealer goes to end of list self.players.append( self.create_dealer("Dealer", 1000000, self.deck) ) # players get added to the front of list self.players.insert( 0, self.create_player("Player_1", 200, self.deck) ) self.winner = None self.loser = None self.pot = []
import random from time import sleep from blackjack_deck import Deck if __name__ == '__main__': deck = Deck() while True: if deck.cards != []: random_card = deck.random_card() print(random_card) print(deck) else: print('\nNew deck\n') deck = Deck() random_card = deck.random_card() print(random_card) print(deck) sleep(0.25)
class Blackjack(object): def __init__(self, human=False): self.human = human self.deck = Deck() self.players = [] # dealer goes to end of list self.players.append(self.create_dealer("Dealer", 1000000, self.deck)) # players get added to the front of list self.players.insert(0, self.create_player("Player_1", 200, self.deck)) self.winner = None self.loser = None self.pot = [] def create_dealer(self, title, cash, deck): if self.human: name = raw_input("Enter %s's name: " % title) cash = raw_input("Enter %s's cash: " % cash) else: name = title cash = 0 if cash == "amount" else cash print 'Created Dealer ' + name + ' with $' + str(cash) return Dealer(name, cash, deck) def create_player(self, title, cash, deck): if self.human: name = raw_input("Enter %s's name: " % title) cash = raw_input("Enter %s's cash: " % cash) else: name = title cash = 0 if cash == "amount" else cash print 'Created Player ' + name + ' with $' + str(cash) return Player(name, cash, deck) def play_game(self): print '=== start playin ===' # while len(self.players) > 1: # there are players besides the dealer self.play_round() self.pause() print '=== play 1 round & exit ===' sys.exit() return def play_round(self): self.place_bets() self.deck.shuffle() self.deal() self.display_hands() # play action print '\n--- play action! ---' for player in self.players: player.play_action() self.determine_win_loss() self.do_money() self.display_hands() def place_bets(self): print '\n--- placing bets ---' for player in self.players: player.make_bet( ) # returns a false if out of $$$. need to do something..... return def deal(self): print '\n--- dealing hands ---' # give each player 1st card, then 2nd card for i in range(2): for player in self.players: player.receive_card(self.deck.draw_card()) def determine_win_loss(self): print '\n--- deterining winners / losers ---' # check dealer status... dealer = self.players[-1] if dealer.busted: print 'Dealer BUSTED. EVERYONE WIN!!!!!!!' else: dealer_value = dealer.hand_value() print 'Dealer = ' + str(dealer_value) # loop thru each player for player in self.players[:-1]: player_val = player.hand_value() print player.name + ' hand = ' + str(player_val) if player_val > dealer_value and (not player.busted): print player.name + ' WON!' else: print player.name + ' Loss...' def do_money(self): print '\n--- do money ---' for player in self.players: pass pass def display_hands(self): for player in self.players: player.display_hand() def display_play(self, card1, card2): if self.human: print "%s plays %s" % (self.player1.name, str(card1)) print "%s plays %s" % (self.player2.name, str(card2)) def display_receive(self, player): if self.human: self.pot.sort(reverse=True) pot_str = self.cards_to_str(self.pot) print "%s receives the cards: %s" % (player.name, pot_str) def display_war(self, cards1, cards2): if self.human: print "WAR!" print "%s plays %s" % (self.player1.name, self.cards_to_str(cards1)) print "%s plays %s" % (self.player2.name, self.cards_to_str(cards2)) def draw_card(self, player, other_player): card = player.play_card() if not card: self.winner = other_player.name self.loser = player.name return self.pot.append(card) return card def draw_cards(self, player, other_player, n): cards = [] n = min(n, len(player.hand), len(other_player.hand)) for i in xrange(n): card = self.draw_card(player, other_player) if not card: return cards.append(card) return cards def war(self): cards1 = self.draw_cards(self.player1, self.player2, self.WarCnt) cards2 = self.draw_cards(self.player2, self.player1, self.WarCnt) self.display_war(cards1, cards2) def give_cards(self, player): player.receive_cards(self.pot) self.display_receive(player) self.pot = [] def pause(self): if self.human: raw_input("") def cards_to_str(self, cards): return " ".join(str(card) for card in cards) def display_winner(self): if self.human: print "The winner is %s!!!" % self.winner def play_two_of_three(self): for i in range(3): winner = self.play_game() if not self.win_counts.get(winner): self.win_counts[winner] = 1 else: self.win_counts[winner] += 1
from blackjack_deck import Deck q = True while q: deal = input("Want to be dealt in? y/n\n") if deal.lower() == 'y': # Creat instance of Deck class called deck, shuffle deck, deal hand to player and dealer. deck = Deck() deck.shuffle_deck() player = deck.deal_player_hand() dealer = deck.deal_dealer_hand() # Display player hand and one of dealer's cards. print("You were dealt the following cards: {}".format(player)) print("Dealer is showing one of their cards: {}".format(dealer[0])) # Player has the option to hit or stay while True: hit = input("Do you want to hit? 'y' or 'n' ") if hit == 'y' and deck.get_player_total() <= 21: deck.player_hit_me() print("You were dealt the following card: {}".format( player[-1])) print("Your current total: {}".format(deck.get_player_total())) else: break # Display player deck and total player_total = deck.get_player_total()
class Blackjack(object): def __init__(self, human=False): self.human = human self.deck = Deck() self.players = [] # dealer goes to end of list self.players.append( self.create_dealer("Dealer", 1000000, self.deck) ) # players get added to the front of list self.players.insert( 0, self.create_player("Player_1", 200, self.deck) ) self.winner = None self.loser = None self.pot = [] def create_dealer(self, title, cash, deck): if self.human: name = raw_input("Enter %s's name: " % title) cash = raw_input("Enter %s's cash: " % cash) else: name = title cash = 0 if cash == "amount" else cash print 'Created Dealer '+name+' with $'+str(cash) return Dealer(name, cash, deck) def create_player(self, title, cash, deck): if self.human: name = raw_input("Enter %s's name: " % title) cash = raw_input("Enter %s's cash: " % cash) else: name = title cash = 0 if cash == "amount" else cash print 'Created Player '+name+' with $'+str(cash) return Player(name, cash, deck) def play_game(self): print '=== start playin ===' # while len(self.players) > 1: # there are players besides the dealer self.play_round() self.pause() print '=== play 1 round & exit ===' sys.exit() return def play_round(self): self.place_bets() self.deck.shuffle() self.deal() self.display_hands() # play action print '\n--- play action! ---' for player in self.players: player.play_action() self.determine_win_loss() self.do_money() self.display_hands() def place_bets(self): print '\n--- placing bets ---' for player in self.players: player.make_bet() # returns a false if out of $$$. need to do something..... return def deal(self): print '\n--- dealing hands ---' # give each player 1st card, then 2nd card for i in range(2): for player in self.players: player.receive_card( self.deck.draw_card() ) def determine_win_loss(self): print '\n--- deterining winners / losers ---' # check dealer status... dealer = self.players[-1] if dealer.busted: print 'Dealer BUSTED. EVERYONE WIN!!!!!!!' else: dealer_value = dealer.hand_value() print 'Dealer = '+str(dealer_value) # loop thru each player for player in self.players[:-1]: player_val = player.hand_value() print player.name+' hand = '+str(player_val) if player_val > dealer_value and (not player.busted): print player.name+' WON!' else: print player.name+' Loss...' def do_money(self): print '\n--- do money ---' for player in self.players: pass pass def display_hands(self): for player in self.players: player.display_hand() def display_play(self, card1, card2): if self.human: print "%s plays %s" % (self.player1.name, str(card1)) print "%s plays %s" % (self.player2.name, str(card2)) def display_receive(self, player): if self.human: self.pot.sort(reverse=True) pot_str = self.cards_to_str(self.pot) print "%s receives the cards: %s" % (player.name, pot_str) def display_war(self, cards1, cards2): if self.human: print "WAR!" print "%s plays %s" % (self.player1.name, self.cards_to_str(cards1)) print "%s plays %s" % (self.player2.name, self.cards_to_str(cards2)) def draw_card(self, player, other_player): card = player.play_card() if not card: self.winner = other_player.name self.loser = player.name return self.pot.append(card) return card def draw_cards(self, player, other_player, n): cards = [] n = min(n, len(player.hand), len(other_player.hand)) for i in xrange(n): card = self.draw_card(player, other_player) if not card: return cards.append(card) return cards def war(self): cards1 = self.draw_cards(self.player1, self.player2, self.WarCnt) cards2 = self.draw_cards(self.player2, self.player1, self.WarCnt) self.display_war(cards1, cards2) def give_cards(self, player): player.receive_cards(self.pot) self.display_receive(player) self.pot = [] def pause(self): if self.human: raw_input("") def cards_to_str(self, cards): return " ".join(str(card) for card in cards) def display_winner(self): if self.human: print "The winner is %s!!!" % self.winner def play_two_of_three(self): for i in range(3): winner = self.play_game() if not self.win_counts.get(winner): self.win_counts[winner] = 1 else: self.win_counts[winner] += 1
def play(self): playing = True no_of_games = 0 while playing and no_of_games < 50: self.deck = Deck() self.deck.shuffle() self.player_hand = Hand() self.player_hand_split = Hand() self.dealer_hand = Hand(dealer=True) for i in range(2): self.player_hand.add_card(self.deck.deal()) self.dealer_hand.add_card(self.deck.deal()) print("------------") print("You:") self.player_hand.display() print() print("Dealer:") self.dealer_hand.display() print() game_over = False is_first_round = True split = False while not game_over: has_blackjack = (self.player_hand.get_value() == 21) or (self.player_hand_split.get_value() == 21) choice = "" if not has_blackjack: if (self.player_hand.cards[0].value == self.player_hand.cards[1].value) and is_first_round: if self.isAI: dealer_up_card = "" if self.dealer_hand.cards[1].value == "10" or self.dealer_hand.cards[1].value == "J" or self.dealer_hand.cards[1].value == "Q" or self.dealer_hand.cards[1].value == "K": dealer_up_card = "T" else: dealer_up_card = self.dealer_hand.cards[1].value player_up_card = "" if self.player_hand.cards[0].value == "10" or self.player_hand.cards[0].value == "J" or self.player_hand.cards[0].value == "Q" or self.player_hand.cards[0].value == "K": player_up_card = "T" else: player_up_card = self.player_hand.cards[0].value choice = chart_pairs[dealer_up_card][player_up_card] else: choice = input("Hit(h), Stick(s), Split(p) or Double Down(d): ").lower() if choice in ['p']: split = True self.player_hand_split.cards.append(self.player_hand.cards[1]) self.player_hand.cards.pop(1) is_first_round = False else: if self.isAI: dealer_up_card = "" if self.dealer_hand.cards[1].value == "10" or self.dealer_hand.cards[1].value == "J" or self.dealer_hand.cards[1].value == "Q" or self.dealer_hand.cards[1].value == "K": dealer_up_card = "T" else: dealer_up_card = self.dealer_hand.cards[1].value player_up_card = "" if self.player_hand.cards[0].value == "A" or self.player_hand.cards[1].value == "A": if self.player_hand.cards[0].value == "A": player_up_card = self.player_hand.cards[1].value else: player_up_card = self.player_hand.cards[0].value choice = chart_ace[dealer_up_card][player_up_card] else: player_total = 0 if self.player_hand.cards[0].value == "J" or self.player_hand.cards[0].value == "Q" or self.player_hand.cards[0].value == "K": player_total += 10 else: player_total += int(self.player_hand.cards[0].value) if self.player_hand.cards[1].value == "J" or self.player_hand.cards[1].value == "Q" or self.player_hand.cards[1].value == "K": player_total += 10 else: player_total += int(self.player_hand.cards[1].value) player_up_card = str(player_total) choice = chart_totals[dealer_up_card][player_up_card] else: choice = input("Hit(h), Stick(s), or Double Down(d): ").lower() is_first_round = False if choice in ['h','p']: if self.player_hand.get_value() < 21: self.player_hand.add_card(self.deck.deal()) self.player_hand.display() if split == True: if self.player_hand_split.get_value() < 21: self.player_hand_split.add_card(self.deck.deal()) print("--------") self.player_hand_split.display() print() if split == True: if self.player_hand.get_value() > 21 and self.player_hand_split.get_value() > 21: print("You are Bust!") self.losses += 1 game_over = True else: if self.player_hand.get_value() > 21: print("You are Bust!") self.losses += 1 game_over = True elif choice in ['s','d'] or choice == "": player_hand_split_value = 0 if choice == 'd': self.player_hand.add_card(self.deck.deal()) self.player_hand.display() if split == True: self.player_hand_split.add_card(self.deck.deal()) print("--------") self.player_hand_split.display() print() player_hand_value = self.player_hand.get_value() player_hand_split_value = self.player_hand_split.get_value() while self.dealer_hand.get_value() < 17: self.dealer_hand.add_card(self.deck.deal()) dealer_hand_value = self.dealer_hand.get_value() player_value = 0 if split: if player_hand_value <= 21 and player_hand_split_value <= 21: if player_hand_value == player_hand_split_value: player_value = player_hand_value elif player_hand_value > player_hand_split_value: player_value = player_hand_value else: player_value = player_hand_split_value elif player_hand_value > 21 and player_hand_split_value <= 21: player_value = player_hand_split_value else: player_value = player_hand_value else: player_value = player_hand_value print() print("Final Results") print("-------------") print("You:", player_value) if player_value > 21: print("Dealer Wins!") self.losses += 1 else: print("Dealer:", dealer_hand_value) if player_value > dealer_hand_value: print("You Win!") self.wins += 1 elif player_value == dealer_hand_value: print("Tie!") self.ties += 1 else: if dealer_hand_value <= 21: print("Dealer Wins!") self.losses += 1 else: print("You Win!") self.wins += 1 game_over = True self.num += 1 if self.isAI: no_of_games += 1 if no_of_games == 50: playing = False print("Wins: ",self.wins) print("Ties: ",self.ties) print("Losses: ",self.losses) playing = False else: game_over = False else: again = input("Play Again? [Y/N] ") if again.lower() == "n": playing = False print("Wins: ",self.wins) print("Ties: ",self.ties) print("Losses: ",self.losses) else: game_over = False print()
class Blackjack(object): def __init__(self, starting_cash=10000, bet=2000): self.starting_cash = starting_cash self.bet = bet self.player = Player(starting_cash) self.dealer = Dealer() def deal(self): print("Player - ${}".format(self.player.money)) self.deck = Deck() self.deck.shuffle() self.player.clear() self.dealer.clear() self.player.receive_card(self.deck.draw_card()) self.dealer.receive_card(self.deck.draw_card()) self.player.receive_card(self.deck.draw_card()) self.dealer.receive_card(self.deck.draw_card()) self.player_choice() def show_player(self): print(self.player.hand_text() + " ({})".format(self.player.hand_total)) def hide_dealer(self): print("Dealer: " + str(self.dealer.hand[0]) + " ?") def show_dealer(self): print(self.dealer.hand_text() + " ({})".format(self.dealer.hand_total)) def player_choice(self): self.show_player() self.hide_dealer() print("Options: Hit Stay Quit ") p_choice = input('>>> ') p_choice.lower() if p_choice == 'hit': self.player_hit() elif p_choice == 'stay': self.player_stay() elif p_choice == 'quit': self.player.money -= self.bet self.end_game() else: print("Not an option") self.player_choice() def player_hit(self): self.player.receive_card(self.deck.draw_card()) if self.player.hand_total > 21: self.show_player() self.show_dealer() self.dealer_win() elif self.player.hand_total == 21: self.show_player() self.dealer_choice() else: self.player_choice() def player_stay(self): self.dealer_choice() def end_game(self): print("Player leaves with ${}".format(self.player.money)) def dealer_choice(self): self.show_dealer() while self.dealer.hand_total < 17: self.dealer.receive_card(self.deck.draw_card()) self.show_dealer() if self.dealer.hand_total > 21 or self.dealer.hand_total < self.player.hand_total: self.player_win() else: self.dealer_win() def play_again(self): again = input('>>> ') again.lower() if again == 'y': self.deal() elif again == 'n': self.end_game() else: print("?") self.play_again() def player_win(self): self.player.money += self.bet / 2 print("You win ${} (${}). Play again? Y/N".format( self.bet, self.player.money)) self.play_again() def dealer_win(self): print('You lose ${}'.format(self.bet)) self.player.money -= self.bet if self.player.money < self.bet: print("Dealer wins. Out of money.") else: print("Dealer wins. You have $({}). Play again? Y/N".format( self.player.money)) self.play_again()
#Welcome Message import time print('Welcome to Blackjack\n') time.sleep(0.2) #Register Players for testing #playerlist, num_of_players = register_players() time.sleep(2) playerlist = register_players() dealer1 = Dealer() #Game Play #Build Deck gamedeck = Deck(6) game_on = True while game_on == True: #Collect player bets for player in playerlist: valid = False x = 0 while valid == False: try: x = int(input(f'{player.name}, Place your bet: '))