def single_round(current_deck): """ Currently, not a full round. Just deals two cards to the player and two cards to the dealer then prints both hands. As these hands are defined within the 'single_round' function (and are not returned at the end), they only exist for a single round. For now, think this is fine. When writing info to StatJack, will need to ensure hand info is written from this function. """ face_direction = ['up', 'down'] # Array storing two possible hand orientations: face-up and face-down players_hand = Hand('Player') # Initialises a hand object for the player dealers_hand = Hand('Dealer') # Initialises a hand object for the computer-controlled dealer for direction in face_direction: players_hand.draw_card(current_deck, 'up') dealers_hand.draw_card(current_deck, direction) # Loop ensures dealer's first card is face-up, second face-down dealers_hand.print_hand() # Prints the dealer's hand players_hand.print_hand() # Prints the player's hand
class WinotaTester: def __init__(self): self.hand = Hand("decklists/winota.txt") self.trials = 0 self.castWinotaHits = 0 self.noWinotaTrigger = 0 self.castWinotaMiss = 0 self.iterations = 100000 self.cardsDrawn = 7 self.AllHits = 0 def checkWinotaTrigger(self): winotaTrigger = [] for i in range(6): winotaTrigger.append(self.hand.deck[self.hand.draw_card()]) return 'human' in winotaTrigger def run(self): for i in range(self.iterations): if i % 5000 == 0: print(str(i) + " of " + str(self.iterations)) self.trials += 1 self.hand.new_hand(self.cardsDrawn) numLands = self.hand.count_of('land') winotaDrawn = self.hand.contains('winota') goblinDrawn = self.hand.contains('goblin') wouldHit = self.checkWinotaTrigger() if numLands < 4 or not winotaDrawn or not goblinDrawn: self.noWinotaTrigger += 1 elif wouldHit: self.castWinotaHits += 1 else: self.castWinotaMiss += 1 if wouldHit: self.AllHits += 1 self.printResults() def printResults(self): print("Trials " + str(self.trials)) misses = self.trials - self.AllHits hitRate = self.AllHits / self.trials print ("All Hits: " + str(self.AllHits) + " All Miss: " + str(misses) + " All Hit rate: " + str(hitRate)) realHitRate = self.castWinotaHits / (self.castWinotaHits + self.castWinotaMiss) print ("Real Hits: " + str(self.castWinotaHits) + " Real Miss: " + str(self.castWinotaMiss) + " Real Hit rate: " + str(realHitRate))
class Game: def __init__(self): self.deck = Deck() self.dealer = Hand() self.player = Hand() def start(self): print('Welcome to Python Black Jack!') query = int(input('1. Start Game\n2. Quit\n\n')) if query == 1: print('Shuffling deck...') self.deck.shuffle_deck() print('Dealing hands...') self.player.draw_card(self.deck.deal()) self.dealer.draw_card(self.deck.deal()) self.player.draw_card(self.deck.deal()) self.dealer.draw_card(self.deck.deal()) elif query == 2: quit()
class Game(object): """ A wrapper for an ongoing game of Blackjack. """ def __init__(self, starting_money=100.00): self._src_deck = Deck() self._player_hand = None self._dealer_hand = None self._player_money = starting_money self._have_built_hand_player = False self._have_built_hand_dealer = False def _build_player_hand(self): """ Builds the player's hand until they either bust or stand. Returns True if the player busts in the process, of False if not. This method must be called before build_dealer_hand in a given round. """ if self._player_hand is None: self._player_hand = Hand(self._src_deck) else: self._player_hand.get_new_hand(self._src_deck) if self._player_hand.is_blackjack(): print("Blackjack!") print("The onus now falls to the dealer to one-up you.", end="\n\n") self._have_built_hand_player = True return False while True: print( f"Your hand has a soft value of {self._player_hand.soft_value()}" ) print( f"Your hand has a hard value of {self._player_hand.hard_value()}" ) print( "You have {} aces, and your hand's optimal value is {}".format( self._player_hand.num_aces(), self._player_hand.optimal_value())) print(self._player_hand, end="\n\n") response_hit_stand = parse_reply_hit_stand( "Do you wish to hit again, or stand? ") if response_hit_stand == "HIT": self._player_hand.draw_card(self._src_deck) if self._player_hand.is_bust(): print("BUST!") self._have_built_hand_player = True return True else: print( f"You've stood with a hand worth {self._player_hand.optimal_value()} points" ) print("The onus now falls to the dealer to better your score.", end="\n\n") self._have_built_hand_player = True return False def _build_dealer_hand(self): """ Builds the dealer's hand automatically, standing on seventeen. Returns True if the dealer busts, or False if not. If the player's hand has not yet been built this round, this method has no effect and merely returns None. If the player previously busted, the dealer's hand will not be built, but this method will return False as though it had been. """ if self._dealer_hand is None: self._dealer_hand = Hand(self._src_deck) else: self._dealer_hand.get_new_hand(self._src_deck) if not self._have_built_hand_player: return None elif self._player_hand.is_bust(): self._have_built_hand_dealer = True return False if self._dealer_hand.is_blackjack(): print("The end is nigh! The dealer has drawn a blackjack.", end="\n\n") self._have_built_hand_dealer = True return False while self._dealer_hand.optimal_value() < DEALER_STAND_THRESHOLD: self._dealer_hand.draw_card(self._src_deck) if self._dealer_hand.is_bust(): print("Praise the gods! The dealer has busted.", end="\n\n") self._have_built_hand_dealer = True return True print("The dealer stands. Their their hand is worth {} points.".format( self._dealer_hand.optimal_value()), end="\n\n") self._have_built_hand_dealer = True return False def has_player_won(self): """ Returns whether the player has won. If the player has not busted and their hand is more valuable than the dealer's, or if the player has achieved blackjack and the dealer has not, then the player has won the round. Both the player's and the dealer's hands must be built for this round for anyone to have won. """ return (self._have_built_hand_player and self._have_built_hand_dealer and not self._player_hand.is_bust() and (self._player_hand.optimal_value() > self._dealer_hand.optimal_value() or (self._player_hand.is_blackjack() and not self._dealer_hand.is_blackjack()) or self._dealer_hand.is_bust())) def is_tie(self): """ Returns whether the current round is a tie. The game is tied if both player and dealer have drawn, and their hands are of equal value, and neither has achieveed a blackjack. """ return (self._have_built_hand_player and self._have_built_hand_dealer and not self._player_hand.is_blackjack() and not self._dealer_hand.is_blackjack() and (self._player_hand.optimal_value() == self._dealer_hand.optimal_value() or self._player_hand.is_bust() and self._dealer_hand.is_bust())) def has_dealer_won(self): """ Returns whether the dealer has won. If the dealer has drawn a hand more valuable than the player's without busting, if the dealer has achieveed a blackjack,, or if the player has busted and dealer has not, then the dealer has won the round. Both the player's and the dealer's hands must be built for this round for anyone to have won. """ return (self._have_built_hand_player and self._have_built_hand_dealer and not self._dealer_hand.is_bust() and (self._dealer_hand.optimal_value() > self._player_hand.optimal_value() or self._dealer_hand.is_blackjack() or self._player_hand.is_bust())) def player_money(self): """ Returns the amount of money the player has. """ return self._player_money def play_round(self, bet_amt): """ Runs one round of blackjack with the provided bet. The player's bet is returned twofold if they win, or lost if they lose. This method does not handle asking the player how much they want to bet, nor does it make additional commentary beyond that they have won, lost, or tied and how much money they've gained or lost. """ self._build_player_hand() self._build_dealer_hand() if self.has_player_won(): print("You are victorious!") self._player_money += bet_amt print(f"You have won ${bet_amt}.") elif self.has_dealer_won(): print("You have lost!") self._player_money -= bet_amt print(f"You have lost ${bet_amt}.") else: print("It's a tie.")
total_credits = 20.00 bet = 5 credit_value = 0.25 num_cards = 5 while (command.lower() != 'q') and (total_credits>0): os.system('clear') total_credits -= bet * credit_value print('Total credits: ${0:0.2f}'.format(total_credits)) print('') deck = Deck() hand = Hand() for _ in range(num_cards): hand.draw_card(deck) print("Select cards to hold separated by a space") print('Hit return to discard all') print(hand) print('[1] [2] [3] [4] [5]') print('') hold_cards = input('> ') if len(hold_cards) == 0: hold_cards = [] else: hold_cards = hold_cards.split(" ") hold_cards = [int(val)-1 for val in hold_cards] for index in range(5): pos = 4 - index
class Game(): def __init__(self, cards_list = None): self.mode = 0 self.win_hand = 0 # 0: round start, valid bet # place_bet() # 1: cards dealt for round 1 # round_start() # 2: cards dealt for round 2 # judge() # 3: round end, everything finalized # payout() # default variables self.total_credits = 20.00 self.bet = 5 self.credit_value = 0.25 self.hold_cards = [False, False, False, False, False] self.deck = Deck() self.hand = Hand() def place_bet(self): if (self.bet <= self.total_credits): self.total_credits -= self.bet return True else: return False def set_bet(self, new_bet): if (new_bet <= self.total_credits): self.bet = self.bet def round_start(self): self.deck = Deck() self.hand = Hand() for _ in range(5): self.hand.draw_card(self.deck) self.hold_cards = [False, False, False, False, False] def hold(self, num): num = int(num) if (num >= 1 & num <= 5): self.hold_cards[num-1] = not self.hold_cards[num-1] def judge(self): #print(self.hold_cards) for index in range(4, -1, -1): #print(index, self.hold_cards[index]) if self.hold_cards[index] == False: #print("Attempting to discard #", str(index)) #print(self.hand) self.hand.discard(index) #print("Success:\t", self.hand) #else: #print("Not attempting to discard #", str(index)) print("Length:\t", (5 - self.hand.length())) for _ in range(5 - self.hand.length()): self.hand.draw_card(self.deck) # update hand GUI score = score_hand(self.hand) # to do: convert change score_hand into an array output in the format [score_name,score]) return score def test_class(self): #Def os.system('clear') print(''' Testing Video Poker Terminal Version! ============================== Please enter a command: q - quit n - new game ''') command = input('> ') if command.lower() == 'q': sys.exit() else: if (self.set_bet(5)): #nothing i = True else: self.total_credits = 20 self.set_bet(5) self.credit_value = 0.25 while (command.lower() != 'q') and (self.total_credits > 0): os.system('clear') self.round_start() self.total_credits -= self.bet * self.credit_value print('Total credits: ${0:0.2f}'.format(self.total_credits)) print('') self.round_start() print("Select cards to hold separated by a space") print('Hit return to discard all') print(self.hand) print('[1] [2] [3] [4] [5]') print('') # Using original implementation of hold command for this test temp_cards = input('> ') self.hold_cards = [False, False, False, False, False] #Debug if len(temp_cards) == 0: self.hold_cards = [False, False, False, False, False] else: temp_cards = temp_cards.split(" ") print(temp_cards, len(temp_cards)) for i in range(5,0,-1): print("Test:\t", temp_cards, "\t", i) if temp_cards.count(str(i)) > 0: self.hold_cards[i-1] = True #End original implementation result = self.judge() print("Cards:", self.hand) pay_table = { "Royal Flush": 800, "Straight Flush": 50, "4 of a Kind": 25, "Full House": 9, "Flush": 6, "Straight": 4, "3 of a Kind": 3, "2 Pair": 2, "Jacks or Better": 1, "Not a Winning Hand": 0, } self.total_credits += pay_table[result] * self.bet * self.credit_value print('Total credits: ${0:0.2f}'.format(self.total_credits)) print("Press enter to play again, q to quit:") command = input('> ')