below is a code to play a game of a popular card game, Pontone. you play your game against the dealer. your hand is randomly made and so is the dealers from the set of cards. you play through the normal game of pontone either "hitting" or "sticking". When you finish your play the dealer plays his and the winner is decided at the end.
import random
unused_cards = ['AS','2S','3S','4S','5S','6S','7S','8S','9S','10S','JS','QS','KS','AC','2C','3C','4C','5C','6C','7C','8C','9C','10C','JC','QC','KC','AH','2H','3H','4H','5H','6H','7H','8H','9H','10H','JH','QH','KH','AD','2D','3D','4D','5D','6D','7D','8D','9D','10D','JD','QD','KD']
used_cards = []
player_1_hand = []
dealer_hand = []
this calculates the total of each players hands
def calculate_hand(player):
global total
total = 0
for i in range(len(player)):
card = player[i]
if len(card) == 2:
value = card[0]
else:
value = card[:2]
if value == "A":
total+=1
elif value == "J" or value == "Q" or value == "K":
total += 10
else:
total += int(value)
return total
this part of the code is the dealer playing his hand
def dealer(j):
global dealer_total
for i in range(2):
drawn_card = random.choice(j)
used_cards.append(drawn_card)
unused_cards = [f for f in j if f not in used_cards]
dealer_hand.append(drawn_card)
dealer_total = calculate_hand(dealer_hand)
dealer_play = True
while dealer_play:
if dealer_total<12:
drawn_card = random.choice(j)
used_cards.append(drawn_card)
unused_cards = [f for f in j if f not in used_cards]
dealer_hand.append(drawn_card)
elif dealer_total<=12 and dealer_total<16:
determine_hit = random.randrange(2)
if determine_hit == 1:
drawn_card = random.choice(j)
used_cards.append(drawn_card)
unused_cards = [f for f in j if f not in used_cards]
dealer_hand.append(drawn_card)
else:
dealer_play = False
dealer_play = False
dealer_total = calculate_hand(dealer_hand)
if dealer_total > 21:
dealer_win = False
else:
dealer_win = True
print(dealer_hand)
return dealer_win
this part draws the original hand for the player
def drawhand(j, player):
global stick
for i in range(2):
drawn_card = random.choice(j)
used_cards.append(drawn_card)
unused_cards = [f for f in j if f not in used_cards]
player.append(drawn_card)
stick = False
print(player)
player_total = calculate_hand(player)
this is the main game loop where you play the game.
def gameloop(j):
global stick
global dealer_total
win = True
drawhand(j, player_1_hand)
while not stick:
play = input("stick or hit")
if total == 21:
print("pontone")
stick = True
if play == "stick":
stick = True
elif play == "hit":
draw_card = random.choice(j)
used_cards.append(draw_card)
unused_cards = [j for j in j if j not in used_cards]
player_1_hand.append(draw_card)
player_total = calculate_hand(player_1_hand)
print(player_1_hand)
if total == 21:
print("pontone")
stick = True
elif total>21:
print("bust")
win = False
stick = True
player_total = calculate_hand(player_1_hand)
dealer_win = dealer(j)
if (dealer_total <= player_total and win != False) or dealer_win == False:
print("you win")
else:
print("you lose")
gameloop(unused_cards)
If you have any ways on how to improve this please share.