def play_round(self): """ Play one round of the game for each player's hand and finally for the dealer. :return: updated card index, player(s) money. """ # check if enough cards available for the current round. if self.index + 2 * (self.player_num + 1) >= self.deck_length: self.logger.debug( "Round: {}\t Current index: {}. Cards cannot be distributed. The game finishes. " "The index is set to deck length:{}.".format( self.round, self.index, self.deck_length)) self.index = self.deck_length return False for i, player in enumerate(self.players): # TODO: each player can set up a new bet for each round. Currently playing with the minimum bet. hand = Hand() # deck[1,2] hand.hand = self.ceil_to_10(self.deck[[ self.index + i, self.index + self.player_num + 1 + i ]]) hand.sum = self.hand_sum(hand.hand) hand.bet = self.bet hand.status = "wait" player.hands = list() player.hands.append(hand) self.logger.debug("Round:{}\tPlayer-{} hand:{}".format( self.round, i, player.hands[0].hand)) self.dealer.hand = self.ceil_to_10(self.deck[[ self.index + self.player_num, self.index + 2 * self.player_num + 1 ]]) self.dealer.sum = self.hand_sum(self.dealer.hand) self.logger.debug("Round:{}\tDealer hand:{}".format( self.round, self.dealer.hand)) self.index += (self.player_num + 1) * 2 # play game for each player for player in self.players: action = Player.apply_basic_strategy(player.hands[0].hand, self.dealer.hand[0]) if not self.play_round_player(action, player.hands[0], player): return False # if all hands lost, no need to play for the dealer. if not all(hand.status == "lost" for player in self.players for hand in player.hands): if not self.play_round_dealer(): return False for player in self.players: for hand in player.hands: self.finish_round(hand, player) return True
def get_input(self, input, players): # this function belongs to bet page hand = Hand() hand.bet = int(input.get()) if len(players[0].hands) == 0: players[0].hands.append(hand) label = tk.Label(self, text="{}, {} bet was entered".format( players[0], hand.bet)) label.pack() return if len(players[1].hands) == 0: players[1].hands.append(hand) label = tk.Label(self, text="{}, {} bet was entered".format( players[1], hand.bet)) label.pack() return
def play_round_player(self, action, hand, player): """ Play one round of the game for a player. :param action: action for the hand. One of double, split, hit, stand. :param hand: instance of hand class :param player: instance of player class :return: Boolean, false if index is out of deck. """ if action == "stand": self.logger.debug( "Round:{}\tPlayer stands.\tPlayer hand:{}\tDealer hand:{}". format(self.round, hand.hand, self.dealer.hand)) if self.is_blackjack(hand.hand): hand.status = "blackjack" else: hand.status = "wait" return True elif action == "hit": if self.index + 1 >= self.cut_card: self.logger.debug( "Round: {}\t Current index: {}\t. Cut card:{}\tThe game ends." .format(self.round, self.index, self.cut_card)) return False hand.hand = np.append(hand.hand, self.ceil_to_10(self.deck[self.index])) self.index += 1 self.logger.debug( "Round:{}\tPlayer hit.\tPlayer hand:{}\tDealer hand:{}".format( self.round, hand.hand, self.dealer.hand)) hand.sum = self.hand_sum(hand.hand) # busted if hand.sum > 21: hand.status = "lost" return True else: action = Player.apply_basic_strategy(hand.hand, self.dealer.hand[0]) return self.play_round_player(action, hand, player) elif action == "double": if self.index + 1 >= self.cut_card: self.logger.debug( "Round: {}\t Current index: {}\t. Cut card:{}\tThe game ends." .format(self.round, self.index, self.cut_card)) return False hand.bet = 2 * hand.bet hand.hand = np.append(hand.hand, self.ceil_to_10(self.deck[self.index])) self.index += 1 self.logger.debug( "Round:{}\tPlayer doubled.\tPlayer hand:{}\tDealer hand:{}". format(self.round, hand.hand, self.dealer.hand)) hand.sum = self.hand_sum(hand.hand) # busted if hand.sum > 21: hand.status = "lost" else: hand.status = "wait" return True elif action == "split": if self.index + 1 >= self.cut_card: self.logger.debug( "Round: {}\t Current index: {}\t. Cut card:{}\tThe game ends." .format(self.round, self.index, self.cut_card)) return False second_hand = Hand() second_hand.status = "wait" second_hand.bet = hand.bet second_hand.hand = np.array([hand.hand[0]]) hand.hand = np.array( [hand.hand[0], self.ceil_to_10(self.deck[self.index])]) self.index += 1 self.logger.debug( "Round:{}\tPlayer split.\tPlayer hand:{}\tDealer hand:{}". format(self.round, hand.hand, self.dealer.hand)) action = Player.apply_basic_strategy(hand.hand, self.dealer.hand[0]) if not self.play_round_player(action, hand, player): return False if self.index + 1 >= self.cut_card: self.logger.debug( "Round: {}\t Current index: {}\t. Cut card:{}\tThe game ends." .format(self.round, self.index, self.cut_card)) return False second_hand.hand = np.append( second_hand.hand, self.ceil_to_10(self.deck[self.index])) second_hand.sum = self.hand_sum(second_hand.hand) self.index += 1 self.logger.debug( "Round:{}\tPlayer split.\tPlayer hand:{}\tDealer hand:{}". format(self.round, second_hand.hand, self.dealer.hand)) player.hands.append(second_hand) action = Player.apply_basic_strategy(second_hand.hand, self.dealer.hand[0]) return self.play_round_player(action, second_hand, player)