async def play_poker(hub): num_decks = int(hub.OPT['poppy']['decks']) num_players = int(hub.OPT['poppy']['players']) hand_size = int(hub.OPT['poppy']['hand']) print(f'Shuffling {num_decks} decks together.') print(f'{num_players} players sitting down at the table.') print(f'Dealing {hand_size} cards to each player.') deck = DeckOfCards(num_decks) players = [] for i in range(num_players): players.append(PokerPlayer("Player", i)) dealer = PokerDealer(deck, players, hand_size) dealer.shuffle_cards() run_tests = hub.OPT['poppy']['tests'] if run_tests is False: dealer.deal_cards() else: print(f'test == {run_tests}') dealer.deal_cards_tests() for player in players: hand_as_string = player.hand_to_string() player_hand = await player.evaluate_hand() hand_name = player.hand_name_to_string(player_hand) print(f'{player.name} {player.number} : {hand_as_string}\t{hand_name}')
def __init__(self): self.hand = DeckOfCards() self.player = [] self.dealer = [] self.max_cards_in_hand = 5 self.face_cards = ["1", "J", "Q", "K"] self.number_cards = "23456789" self.hand_value = 0 self.start_value = 0
class TestDeckOfCards(unittest.TestCase): def setUp(self): self.deck_of_cards = DeckOfCards() def test_get_a_card(self): card_1 = self.deck_of_cards.give_a_card() card_2 = self.deck_of_cards.give_a_card() """ print 'CARTA 1' print card_1.rank print card_1.suit print card_1.value print 'CARTA 2' print card_2.rank print card_2.suit print card_2.value """ self.assertNotEqual(card_1, card_2)
async def main(): num_players = 10 hand_size = 5 num_decks = 10 deck = DeckOfCards(num_decks) players = [] for i in range(num_players): players.append(PokerPlayer("Player", i)) dealer = PokerDealer(deck, players, hand_size) dealer.shuffle_cards() dealer.deal_cards() # dealer.deal_cards_tests() for player in players: hand_as_string = player.hand_to_string() player_hand = await player.evaluate_hand() hand_name = player.hand_name_to_string(player_hand) print(f'{player.name} {player.number} : {hand_as_string}\t{hand_name}')
def setUp(self): self.deck_of_cards = DeckOfCards()
class BlackjackGame: deck_of_cards = None dealer = None player = None active = None #This indicates if the game is active or not current_player_bet = None def __init__(self): self.deck_of_cards = DeckOfCards() self.dealer = Dealer(self) self.player = HumanPlayer(self, 10000) #The human player starts with 10000 coins def start_game(self, training_repetitions, real_games_repetitions): self.player.victories = 0 self.dealer.victories = 0 self.player.coins = 10000 self.active = True # As the game begins, we set this flag the True value training_flag = True #It's time to train! training_repetitions = training_repetitions #this number can be changed for x in range(0, training_repetitions): print 'Training hand #' + str(x) + '\n' self.begin_hand(training_flag) self.deck_of_cards.restart_deck_of_cards() training_flag = False #I'm tired of training, I want to play seriously!! print 'END OF TRAINING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' self.player.print_victories() self.dealer.print_victories() #On the while condition we could ask if the player wants to keep playing, #But here we prefer the automated player to play a fixed set of hands, let's say, 250 real_hands_to_play = real_games_repetitions i = 0 while (self.not_ended() and i < real_hands_to_play): i+=1 print '--------------------------------------------------' print '\n\nReal hand #' + str(i) + '\n' self.begin_hand(training_flag) self.deck_of_cards.restart_deck_of_cards() self.player.print_victories() self.dealer.print_victories() print 'Initial coins: ' + '10000' #it starts with 10000 coins print 'Coins (after the game): ' + str(self.player.coins) def not_ended(self): return self.active def clean_hands(self): self.player.clean_hand() self.dealer.clean_hand() def get_deck(self): return self.deck_of_cards def begin_hand(self, training_flag): self.current_player_bet = self.player.bet(training_flag) self.clean_hands() #Makes sure both the dealer and the player have empty hands print '\nNEW ROUND:' print '\n Dealer Hand:' self.dealer.get_card(self.deck_of_cards.give_a_card()) self.dealer.print_hand() print '\n Human Hand:' self.player.get_card(self.deck_of_cards.give_a_card()) self.player.get_card(self.deck_of_cards.give_a_card()) self.player.print_hand() dealer_original_value = self.dealer.calculate_value() move = self.player.make_move(dealer_original_value, training_flag) if (move == 'split'): self.split_hand(training_flag) else: player_value = self.player.calculate_value() self.dealer.make_move(player_value) self.compute_and_print_hand_results(self.current_player_bet, player_value, training_flag) #This should be refactored def compute_and_print_hand_results(self, bet, player_value, training_flag): if player_value == 21: print 'BlackJack! You win' print '-------------------------------------------------' if training_flag and (len(self.player.temp_state_action) > 0): self.player.update_fg_values('win') elif not training_flag: self.player.compute_victory() self.player.get_prize(1.5 * 2 * self.current_player_bet) result = 'win' elif (self.player.calculate_value() == self.dealer.calculate_value()): if not training_flag: self.player.get_prize(self.current_player_bet) print "It's a tie! Your bet is refunded" return 'tie' elif player_value > 21: print ' \nThe Dealer WINS! (Human got over 21)' print '-------------------------------------------------' if training_flag: self.player.update_fg_values('lose') else: self.dealer.compute_victory() self.player.get_prize(self.current_player_bet) result = 'lose' self.player.restart_temp_state_action() elif self.dealer.calculate_value() > 21: print '\nHuman Player WINS! (Dealer got over 21)' print '-------------------------------------------------' if training_flag: self.player.update_fg_values('win') else: self.player.compute_victory() self.player.get_prize(2 * bet) result = 'win' self.player.restart_temp_state_action() elif (21 - player_value) < (21 - self.dealer.calculate_value()): print "\nHuman Player WINS! (Has a better score)" print '-------------------------------------------------' if training_flag: self.player.update_fg_values('win') else: self.player.compute_victory() self.player.get_prize(2 * bet) result = 'win' self.player.restart_temp_state_action() elif (21 - player_value) > (21 - self.dealer.calculate_value()): print "\nThe Dealer WINS! (Has a better score)" print '-------------------------------------------------' if training_flag: self.player.update_fg_values('lose') else: self.dealer.compute_victory() result = 'lose' self.player.restart_temp_state_action() def split_hand(self, training_flag): #If the player chooses to split, then two 'sub-hands' are played #instead of one. Each hand with one of the cards, and each hand #with the same bet. Obviously, if the player chooses to split, he #must bet again the same quantity. player_initial_hand = copy.deepcopy(self.player.hand) dealer_hand = self.dealer.hand card = self.player.hand.cards.pop() print 'SPLIT!\n' print '----Split hand 1\n' self.begin_one_split_hand(training_flag, self.player.hand, dealer_hand) player_value_a = self.player.calculate_value() aux_temp_state_action_a = copy.deepcopy(self.player.temp_state_action) print aux_temp_state_action_a print '----Split hand 2\n' self.player.restart_temp_state_action() self.player.temp_state_action.append(((player_initial_hand.calculate_status(),dealer_hand.calculate_value()), 'split')) self.player.hand.clean() self.player.hand.add_card(card) self.begin_one_split_hand(training_flag, self.player.hand, dealer_hand) self.dealer.make_move(0) #0 because it play with 2 hands at the same time player_value_b = self.player.calculate_value() #hand b print "Hand 2:" self.compute_and_print_hand_results(self.current_player_bet, player_value_a, training_flag) #hand a print "Hand 1" self.player.temp_state_action = aux_temp_state_action_a self.compute_and_print_hand_results(self.current_player_bet, player_value_b,training_flag) def begin_one_split_hand(self, training_flag, player_hand, dealer_hand): dealer_original_value = dealer_hand.calculate_value() self.player.hand = player_hand self.player.make_move(dealer_original_value, training_flag)
def __init__(self): self.deck_of_cards = DeckOfCards() self.dealer = Dealer(self) self.player = HumanPlayer(self, 10000) #The human player starts with 10000 coins
def __init__(self): hand = DeckOfCards() max_cards_in_hand = 5 player = [] dealer = [] face_cards = ["1", "J", "Q", "K"] number_cards = "23456789" hand_value = 0 print("*****************************" + "\n*****************************" + "\n**** AWESOME BLACKJACK!!!****" "\n*****************************" + "\n*****************************") # start = input("Press Enter To Start") player_card_1 = hand.deal_hand(True) player_card_2 = hand.deal_hand(True) dealer_card_1 = hand.deal_hand(True) # Don't forget to change this to False when the game is complete dealer_card_2 = hand.deal_hand(True) player.append(player_card_1) player.append(player_card_2) dealer.append(dealer_card_1) dealer.append(dealer_card_2) print("Player Hand: ", player) print("Dealer Hand: ", dealer) ''' player_start_value = [] for card in player: if card[0] in face_cards: player_start_value.append(10) elif card[0] in number_cards: player_start_value.append(int(card[0])) elif card[0] == "A": # This logic is not yet bullet proof. It needs to change based on the changing total. if sum(player_start_value) <= 10: player_start_value.append(11) else: player_start_value.append(1) elif sum(player_start_value) == 21: print("\nYOU HIT 21!!! YOU WIN!!!") exit() ''' while len(player) < max_cards_in_hand: hand_value = 0 choice = input("Would you like to Hit or Stand? H or S: ").lower() if choice == "h": new_card = hand.deal_hand(True) player.append(new_card) for card in player: if card[0] in face_cards: hand_value += 10 elif card[0] in number_cards: hand_value += int(card[0]) elif card[0] == "A" and hand_value > 11: hand_value += 1 elif card[0] == "A" and hand_value < 11: hand_value += 10 print(player) print(hand_value) if hand_value < 21: continue elif hand_value == 21: break elif hand_value > 21: break else: break dealer_hand_value = 0 ''' for card in dealer: if card[0] in face_cards: dealer_hand_value += 10 elif card[0] in number_cards: dealer_hand_value += int(card[0]) elif card[0] == "A": if dealer_hand_value <= 10: dealer_hand_value += 11 else: dealer_hand_value += 1 elif dealer_hand_value == 21: break # dealer_hand_value = dealer_start_value ''' while len(dealer) < max_cards_in_hand: if 21 >= dealer_hand_value >= 17: break elif dealer_hand_value < 17: new_card = hand.deal_hand(True) dealer.append(new_card) for card in dealer: if card[0] in face_cards: dealer_hand_value += 10 elif card[0] in number_cards: dealer_hand_value += int(card[0]) elif card[0] == "A" and dealer_hand_value > 11: dealer_hand_value += 1 elif card[0] == "A" and dealer_hand_value < 11: dealer_hand_value += 10 print(dealer, dealer_hand_value) elif dealer_hand_value > 21: break else: break if dealer_hand_value > 21: print("You Win! Dealer Busted!") exit() elif hand_value > 21: print("You've Busted! Game Over!") elif player == 21: if player == dealer: print("Tie! Dealer Wins!") exit() else: print("BlackJack! You Win!") exit() elif player > dealer: print("Player wins!") exit() elif dealer < player: print("Dealer wins!") exit() elif player == dealer: print("Tie! Dealer wins!") exit()
class BlackjackGame: deck_of_cards = None dealer = None player = None active = None #This indicates if the game is active or not def __init__(self): self.deck_of_cards = DeckOfCards() self.dealer = Dealer(self) self.player = Player(self, 10) #The human player starts with 10 coins self.start_game() def start_game(self): self.active = True # As the game begins, we set this flag the True value while (self.not_ended()): self.begin_hand() self.player.ask_if_continues() #The player if asked if he wants to keep playing def not_ended(self): return self.active def clean_hands(self): self.player.clean_hand() self.dealer.clean_hand() def get_deck(self): return self.deck_of_cards def begin_hand(self): bet = self.player.bet() self.clean_hands() #Makes sure both the dealer and the player have empty hands self.dealer.get_card(self.deck_of_cards.give_a_card()) self.player.get_card(self.deck_of_cards.give_a_card()) self.player.get_card(self.deck_of_cards.give_a_card()) if (self.player.calculate_value() == 21): print 'BlackJack! You win' self.player.get_prize(bet + 1.5*bet) else: if (self.player.has_two_equal_valued_cards): if (self.player.wants_to_split()): self.split_hand() #Code the spit hand dealer_original_value = self.dealer.calculate_value() self.player.make_move(dealer_original_value) player_value = self.player.calculate_value() self.dealer.make_move(player_value) #TODO: Calculate results. If player's hand value is higher #TODO: than dealer's hand value, then the player wins. #TODO: If both have the same hand value, the bid is returned. #TODO: If anyone's hand value is over 21, he loses def split_hand(self): #TODO: Implement #If the player chooses to split, then two 'sub-hands' are played #instead of one. Each hand with one of the cards, and each hand #with the same bet. Obviously, if the player chooses to split, he #must bet again the same quantity. pass
def __init__(self): self.deck_of_cards = DeckOfCards() self.dealer = Dealer(self) self.player = Player(self, 10) #The human player starts with 10 coins self.start_game()