This repository was archived by the owner on Aug 10, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathblackjack.py
More file actions
executable file
·133 lines (118 loc) · 4.63 KB
/
Copy pathblackjack.py
File metadata and controls
executable file
·133 lines (118 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
BlackJack en Python
ASRBD 211 - Julien.G - Henri.N - Sammy.E
"""
#Importing modules
import random
from random import shuffle
import os
#Dictionary
dict_Values = {'2♠': 2, '3♠': 3, '4♠': 4, '5♠': 5, '6♠': 6, '7♠': 7, '8♠': 8, '9♠': 9, '10♠': 10, 'J♠': 10, 'Q♠': 10, 'K♠': 10, 'A♠': 11,
'2♥': 2, '3♥': 3, '4♥': 4, '5♥': 5, '6♥': 6, '7♥': 7, '8♥': 8, '9♥': 9, '10♥': 10, 'J♥': 10, 'Q♥': 10, 'K♥': 10, 'A♥': 11,
'2♦': 2, '3♦': 3, '4♦': 4, '5♦': 5, '6♦': 6, '7♦': 7, '8♦': 8, '9♦': 9, '10♦': 10, 'J♦': 10, 'Q♦': 10, 'K♦': 10, 'A♦': 11,
'2♣': 2, '3♣': 3, '4♣': 4, '5♣': 5, '6♣': 6, '7♣': 7, '8♣': 8, '9♣': 9, '10♣': 10, 'J♣': 10, 'Q♣': 10, 'K♣': 10, 'A♣': 11}
#This function creates the entire deck of 52 cards
def deckCreation():
card_Types = ['♠','♥','♦','♣']
card_Numbers = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
deck = []
for i in card_Types:
for j in card_Numbers:
deck.append(j+i)
return deck
#This function draw 2 card
def drawCards():
hand = [gameDeck.pop()]
return hand
#Function clear
def cls():
os.system('cls' if os.name=='nt' else 'clear')
#Replay function
def playAgain():
while True:
print()
choice = input("Do you want to play again ? (Y or N) ")
if choice.upper() == "Y":
play = True
cls()
break
elif choice.upper() == "N":
play = False
break
else:
print("Error please do choice again")
return play
play = True
while play == True:
#Preview of the generated deck
gameDeck = deckCreation()
#Shuffle deck
shuffle(gameDeck)
#Draw cards to players
player_hand = []
dealer_hand = []
player_hand += drawCards()
dealer_hand += drawCards()
player_hand += drawCards()
dealer_hand += drawCards()
#Calculate score
playerScore = dict_Values[player_hand[0]] + dict_Values[player_hand[1]]
dealerScore = dict_Values[dealer_hand[0]] + dict_Values[dealer_hand[1]]
#Display board
print("Welcome to BlackJack")
print()
print("Dealer's hand : "+dealer_hand[0]+" + Hidden card")
print("Dealer's score : "+str(dict_Values[dealer_hand[0]]))
print("----------")
print("Player's hand : "+player_hand[0]+" + "+player_hand[1])
print("Player's score : "+str(playerScore))
#Condition for test blackjack situation = H
if playerScore == 21:
if dealerScore == 21:
print("Blackjack for all players. Draw !")
play = playAgain()
else:
print("Blackjack for you. You win !")
play = playAgain()
else:
while True:
print()
choice = input("Enter H to Hit or S to Stand : ")
if choice.upper() == 'H':
player_hand += drawCards()
playerScore += dict_Values[player_hand[-1]]
print("You pick : "+player_hand[-1])
print("Your score : "+str(playerScore))
if playerScore > 21:
print("You made "+str(playerScore)+", you lost")
play = playAgain()
break
elif playerScore == 21:
while dealerScore < playerScore:
dealer_hand += drawCards()
dealerScore += dict_Values[dealer_hand[-1]]
print("Dealers pick "+str(dealer_hand[-1])+" this score is "+str(dealerScore))
if dealerScore > 21:
print("You win")
play = playAgain()
break
elif dealerScore > playerScore:
print("You lose")
play = playAgain()
break
elif choice.upper() == 'S':
print("Dealer's hand : "+str(dealer_hand[0])+" + "+str(dealer_hand[1]))
while dealerScore <= playerScore:
dealer_hand += drawCards()
dealerScore += dict_Values[dealer_hand[-1]]
print("Dealers pick "+str(dealer_hand[-1])+" this score is "+str(dealerScore))
if dealerScore > 21:
print("You win")
play = playAgain()
break
elif dealerScore > playerScore:
print("You lose")
play = playAgain()
break
else:
print("Error please do choice again")