class Player: """ This class represents a Player of the game The Player holds his Hand as an instance variable, The Hand is a LinkedList of Cards. When a Player is dealt a Card from the Deck, the Card is added first in his Hand When a Player adds a Pile after becoming the winner, the Pile of Cards is added last to the Hand, one at a time Cards are only played from the front of the Hand """ def __init__(self): """ Constructor: Creates the empty hand """ self.__pile = Pile() self.__hand = Hand() def add_card(self, card): """ Add the passed in Card to the Hand at the front Working from broker while dealing """ self.__hand.add_first(card) def add_pile(self, pile): """ Add the passed in Pile of Cards to the Hand at the back. The Cards are removed from the Pile and added to the Hand - one at a time """ for i in range(len(pile)): card = pile.remove_first() self.__hand.add_last(card) def play_card(self, pile): """ Remove the top Card from the Hand And add it to the passed in Pile Use a method from the Pile class """ card = self.__hand.remove_first() pile.add_card(card) def get_num_cards(self): """ Return the number of Cards in the Hand """ return len(self.__hand) def display_hand(self): """ Display the Player's Hand by calling the __str__ method """ print(self.__hand.__str__())
scores = [0, 0] def do_move(current_player, first_move): move = generate_move(board, [hand1, hand2], current_player, first_move) if move is None: return None _, _, _, _, _, s, i = move [hand1, hand2][current_player].remove_piece(i) [hand1, hand2][current_player].add_piece(bank.get_piece()) return s # Handle player 2 print("hand2 has: ", hand2.__str__()) score = do_move(1, True) scores[1] += score while True: minimax(board, [hand1, hand2], 0, 0, 0) # Handle player 1 print("hand1 has: ", hand1.__str__()) score = do_move(0, False) if score is None: break scores[0] += score # Handle board drawing board.draw(screen) pygame.display.flip()
class Blackjack: # constructors def __init__(self): self.p_hand = Hand() self.d_hand = Hand() self.hands = [self.p_hand, self.d_hand] self.deck = Deck() self.playing = True self.deal(self.hands, 2) # public non-static methods # Deals random cards from the game deck into the hands specified # hands: array containing Hand instances # num: number of cards to deal to each def deal(self, hands, num): for h in hands: for i in range(0, num): h.add_card(self.deck.get_random_card()) def play(self): print("It's time to play some Blackjack!") print("I'll deal you in, get as close to 21 as possible without going over and beat me!") print("Here's your cards: " + str(self.p_hand)) print("The dealer's hand looks like this: [****, " + self.d_hand.get_card(-1).__str__() + "]") while self.playing: if self.make_action() == "1": self.__hit(self.p_hand) print("Here's your cards: " + str(self.p_hand)) print("The dealer's hand looks like this: [****, " + self.d_hand.get_card(-1).__str__() + "]") else: self.__stay() if self.p_hand.get_value() > 21: print("Bust! You went over 21.") self.__lose() def make_action(self): print("Would you like to hit or stay?") print("1: Hit\n2: Stay") ans = input("--> ") if ans == '1' or ans == '2': return ans else: print("\n=======================") print("Invalid input.") print("=======================\n") self.make_action() def __hit(self, hand): self.deal([hand], 1) print("The new card is a " + hand.get_card(-1).__str__()) def __stay(self): self.__dealer_actions() self.__end_game() # The dealer's actions def __dealer_actions(self): if self.d_hand.get_value() > 21: print("The dealer went over 21...") print("The dealer's hand looked like this: " + self.d_hand.__str__()) self.__win() elif self.d_hand.get_value() < 17: self.__hit(self.d_hand) self.__dealer_actions() else: self.__compare_scores() def __compare_scores(self): print(self.p_hand) print(self.d_hand) if self.p_hand.get_value() > self.d_hand.get_value(): self.__win() elif self.p_hand.get_value() < self.d_hand.get_value(): self.__lose() else: self.__tie() def __win(self): print("You win!") self.__end_game() def __lose(self): print("You lose...") self.__end_game() def __tie(self): print("===========================") print(" You tied! Try again ") print("===========================") self.__end_game() self.reset() def __end_game(self): self.playing = False def reset(self): self.__init__() self.play()