131 lines
3.1 KiB
Python
131 lines
3.1 KiB
Python
import random
|
|
|
|
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
|
|
suits = ["of Clubs", "of Diamonds", "of Hearts", "of Spades"]
|
|
player_hand = []
|
|
dealer_hand = []
|
|
deck = []
|
|
|
|
class Player:
|
|
def draw(self):
|
|
card = random.choice(deck)
|
|
deck.remove(card)
|
|
player_hand.append(card)
|
|
return card
|
|
|
|
def hand_value(self):
|
|
total = 0
|
|
for _ in player_hand:
|
|
total += get_value(_)
|
|
return total
|
|
|
|
class Dealer:
|
|
def draw(self):
|
|
card = random.choice(deck)
|
|
deck.remove(card)
|
|
dealer_hand.append(card)
|
|
return card
|
|
|
|
def hand_value(self):
|
|
total = 0
|
|
for _ in dealer_hand:
|
|
total += get_value(_)
|
|
return total
|
|
|
|
|
|
def create_deck():
|
|
counter = 0
|
|
x = 0
|
|
for _ in range(len(suits)):
|
|
y = 0
|
|
for _ in range(len(ranks)):
|
|
deck.append(f"{ranks[y]} {suits[x]}")
|
|
y += 1
|
|
counter += 1
|
|
x += 1
|
|
return deck
|
|
|
|
def get_value(card):
|
|
if "2" in card:
|
|
return 2
|
|
if "3" in card:
|
|
return 3
|
|
if "4" in card:
|
|
return 4
|
|
if "5" in card:
|
|
return 5
|
|
if "6" in card:
|
|
return 6
|
|
if "7" in card:
|
|
return 7
|
|
if "8" in card:
|
|
return 8
|
|
if "9" in card:
|
|
return 9
|
|
if "10" in card:
|
|
return 10
|
|
if "Jack" in card or "Queen" in card or "King" in card:
|
|
return 10
|
|
if "Ace" in card:
|
|
return 11
|
|
return None
|
|
|
|
def main():
|
|
bank = 1000
|
|
while True:
|
|
global player
|
|
global dealer
|
|
player = Player()
|
|
dealer = Dealer()
|
|
create_deck()
|
|
p_stand = False
|
|
d_stand = False
|
|
for _ in range(2):
|
|
player.draw()
|
|
dealer.draw()
|
|
print(f"your current balance: {bank}")
|
|
bet = int(input("Enter your bet: "))
|
|
bank -= bet
|
|
|
|
while True:
|
|
print(f"Your hand: {player_hand} value: {player.hand_value()}")
|
|
print()
|
|
print(f"dealer card: {dealer_hand} value: {dealer.hand_value()}")
|
|
print()
|
|
|
|
if not p_stand:
|
|
if input("hit or stand (h/s): ") == "h":
|
|
print(f"you draw a card: {player.draw()}")
|
|
print()
|
|
else:
|
|
p_stand = True
|
|
|
|
if not d_stand:
|
|
if dealer.hand_value() < 17:
|
|
dealer.draw()
|
|
print(f"dealer draws a card: {dealer.draw()}")
|
|
print()
|
|
else:
|
|
d_stand = True
|
|
|
|
if player.hand_value() > 21:
|
|
print("you loose")
|
|
player_hand.clear()
|
|
dealer_hand.clear()
|
|
break
|
|
if player.hand_value() == 21:
|
|
print(f"you win ({bet * 2})")
|
|
bank = bank + bet * 2
|
|
player_hand.clear()
|
|
dealer_hand.clear()
|
|
deck.clear()
|
|
break
|
|
|
|
if bank == 0:
|
|
print("YOU HAVE NO MONEY")
|
|
break
|
|
if input("Do you want to play again? (y/n): ") == "n":
|
|
break
|
|
|
|
|
|
main() |