Amazing-Python-Scripts
222 строки · 5.3 Кб
1# IMPORT MODULES AND DEFINE VARIABLES
2
3import random
4
5playing = True
6
7
8# CLASSES
9
10class Card: # Creates all the cards
11
12def __init__(self, suit, rank):
13self.suit = suit
14self.rank = rank
15
16def __str__(self):
17return self.rank + ' of ' + self.suit
18
19
20class Deck: # creates a deck of cards
21
22suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
23ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
24'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
25
26def __init__(self):
27self.deck = [] # haven't created a deck yet
28for suit in Deck.suits:
29for rank in Deck.ranks:
30self.deck.append(Card(suit, rank))
31
32def __str__(self):
33deck_comp = ''
34for card in self.deck:
35deck_comp += '\n ' + card.__str__()
36return 'The deck has: ' + deck_comp
37
38def shuffle(self): # shuffle all the cards in the deck
39random.shuffle(self.deck)
40
41def deal(self): # pick out a card from the deck
42single_card = self.deck.pop()
43return single_card
44
45
46class Hand: # show all the cards that the dealer and player have
47
48values = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8,
49'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}
50
51def __init__(self):
52self.cards = []
53self.value = 0
54self.aces = 0 # keep track of aces
55
56def add_card(self, card): # add a card to the player's or dealer's hand
57self.cards.append(card)
58self.value += Hand.values[card.rank]
59if card.rank == 'Ace':
60self.aces += 1
61
62def adjust_for_ace(self):
63while self.value > 21 and self.aces:
64self.value -= 10
65self.aces -= 1
66
67
68class Chips: # keep track of chips
69
70def __init__(self):
71self.total = 100
72self.bet = 0
73
74def win_bet(self):
75self.total += self.bet
76
77def lose_bet(self):
78self.total -= self.bet
79
80
81# FUNCTIONS
82
83def take_bet(chips): # ask for user's bet
84
85while True:
86try:
87chips.bet = int(input("How many chips would you like to bet? "))
88except ValueError:
89print("Sorry! Please can you type in a number: ")
90else:
91if chips.bet > chips.total:
92print("You bet can't exceed 100!")
93else:
94break
95
96
97def hit(deck, hand):
98hand.add_card(deck.deal())
99hand.adjust_for_ace()
100
101
102def hit_or_stand(deck, hand): # hit or stand
103global playing
104
105while True:
106ask = input(
107"\nWould you like to hit or stand? Please enter 'h' or 's': ")
108
109if ask[0].lower() == 'h':
110hit(deck, hand)
111elif ask[0].lower() == 's':
112print("Player stands, Dealer is playing.")
113playing = False
114else:
115print("Sorry! I did not understand that! Please try again!")
116continue
117break
118
119
120def show_some(player, dealer):
121print("\nDealer's Hand: ")
122print(" <card hidden>")
123print("", dealer.cards[1])
124print("\nPlayer's Hand: ", *player.cards, sep='\n ')
125
126
127def show_all(player, dealer):
128print("\nDealer's Hand: ", *dealer.cards, sep='\n ')
129print("Dealer's Hand =", dealer.value)
130print("\nPlayer's Hand: ", *player.cards, sep='\n ')
131print("Player's Hand =", player.value)
132
133
134# Game endings
135
136def player_busts(player, dealer, chips):
137print("PLAYER BUSTS!")
138chips.lose_bet()
139
140
141def player_wins(player, dealer, chips):
142print("PLAYER WINS!")
143chips.win_bet()
144
145
146def dealer_busts(player, dealer, chips):
147print("DEALER BUSTS!")
148chips.win_bet()
149
150
151def dealer_wins(player, dealer, chips):
152print("DEALER WINS!")
153chips.lose_bet()
154
155
156def push(player, dealer):
157print("Its a push! Player and Dealer tie!")
158
159
160# Gameplay!
161
162while True:
163print("Welcome to BlackJack!")
164
165# create an shuffle deck
166deck = Deck()
167deck.shuffle()
168
169player_hand = Hand()
170player_hand.add_card(deck.deal())
171player_hand.add_card(deck.deal())
172
173dealer_hand = Hand()
174dealer_hand.add_card(deck.deal())
175dealer_hand.add_card(deck.deal())
176
177# set up the player's chips
178player_chips = Chips()
179
180# ask player for bet
181take_bet(player_chips)
182
183# show cards
184show_some(player_hand, dealer_hand)
185
186while playing:
187
188hit_or_stand(deck, player_hand)
189show_some(player_hand, dealer_hand)
190
191if player_hand.value > 21:
192player_busts(player_hand, dealer_hand, player_chips)
193break
194
195if player_hand.value <= 21:
196
197while dealer_hand.value < 17:
198hit(deck, dealer_hand)
199
200show_all(player_hand, dealer_hand)
201
202if dealer_hand.value > 21:
203dealer_busts(player_hand, dealer_hand, player_chips)
204
205elif dealer_hand.value > player_hand.value:
206dealer_wins(player_hand, dealer_hand, player_chips)
207
208elif dealer_hand.value < player_hand.value:
209player_wins(player_hand, dealer_hand, player_chips)
210
211if player_hand.value > 21:
212player_busts(player_hand, dealer_hand, player_chips)
213
214print("\nPlayer's winnings stand at", player_chips.total)
215
216new_game = input("\nWould you like to play again? Enter 'y' or 'n': ")
217if new_game[0].lower() == 'y':
218playing = True
219continue
220else:
221print("\nThanks for playing!")
222break
223