Skip to content

Commit 8faa10b

Browse files
authored
Create BlackJackGame.py
1 parent 6fcc33f commit 8faa10b

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

BlackJackGame/BlackJackGame.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import random as rn
2+
deck = [2,3,4,5,6,7,8,9,10,'J','Q','K','A']
3+
suit = ['spade' , 'heart' , 'diamond' , 'club']
4+
arr = []
5+
pl = []
6+
dl = []
7+
8+
# Check Blackjack__________________________________________
9+
def BlackJack(s):
10+
if s==21:
11+
return True
12+
else:
13+
return False
14+
15+
# Check Bust_______________________________________________
16+
def Bust(s):
17+
if s>21:
18+
return True
19+
else:
20+
return False
21+
22+
# Check Ace is 1 or 11_____________________________________
23+
def check_For_Ace(s):
24+
if (s+11)<=21:
25+
return 11
26+
else:
27+
return 1
28+
29+
# Adjust the sum___________________________________________
30+
def check_sum(s):
31+
if s < 21:
32+
return s
33+
34+
for i in range(len(pl)-1,-1,-1):
35+
if pl[i] == 'A':
36+
s -= 10
37+
break
38+
return s
39+
40+
# creating a well shuffled pack of cards___________________
41+
def startGame():
42+
print("Welcome to the BlackJack Game:\n")
43+
for i in suit:
44+
for j in deck:
45+
arr.append([i,j])
46+
rn.shuffle(arr)
47+
48+
pl.append(arr[0])
49+
pl.append(arr[1])
50+
dl.append(arr[2])
51+
dl.append(arr[3])
52+
53+
print(f'\nDealer draws {dl[0]} and [xxxxxx,xx]\n')
54+
s = 0
55+
for i in range(0,2):
56+
if arr[i][1] == 'J' or arr[i][1] == 'K' or arr[i][1] == 'Q':
57+
s += 10
58+
elif arr[i][1] == 'A':
59+
s += check_For_Ace(s)
60+
else:
61+
s += arr[i][1]
62+
return s
63+
64+
# player's chance__________________________________________
65+
def playersTurn(s):
66+
print("You are currently at " + str(s))
67+
print(f'Current Hand: {pl}')
68+
69+
s = check_sum(s)
70+
71+
if (BlackJack(s)):
72+
print("Hurray......It's a BLACKJACK....You Won\n")
73+
return 2
74+
elif (Bust(s)):
75+
print("You got Bust.....You Lost\n")
76+
return 3
77+
78+
while(1):
79+
i = int(input("\nHit(Press 1) or Stay(Press 0): "))
80+
if i==1 or i==0:
81+
return i
82+
else:
83+
print("Enter a valid number....\n")
84+
85+
if __name__ == '__main__':
86+
s = startGame()
87+
c = j = 0
88+
for i in range(4,len(arr)):
89+
90+
j = playersTurn(s)
91+
if j == 2 or j == 3:
92+
j = i
93+
break
94+
elif j == 0:
95+
j = i
96+
c = 100
97+
break
98+
print(f'You draw {arr[i]}\n')
99+
100+
if arr[i][1] == 'J' or arr[i][1] == 'K' or arr[i][1] == 'Q':
101+
c = 10
102+
elif arr[i][1] == 'A':
103+
c = check_For_Ace(s)
104+
else:
105+
c = arr[i][1]
106+
s += c
107+
pl.append(arr[i])
108+
109+
if c == 100:
110+
sp = s
111+
s = 0
112+
for i in range(2,4):
113+
if arr[i][1] == 'J' or arr[i][1] == 'K' or arr[i][1] == 'Q':
114+
s += 10
115+
elif arr[i][1] == 'A':
116+
s += check_For_Ace(s)
117+
else:
118+
s += arr[i][1]
119+
print(f"\nDealer's second card was {dl[1]}")
120+
121+
for i in range(j,len(arr)):
122+
print(f"\nDealer is at {s}")
123+
print(f"\nDealer's current hand: {dl}\n")
124+
125+
s = check_sum(s)
126+
if (BlackJack(s)):
127+
print("Dealer got a BlackJack and won the Game\nYou Lost\n")
128+
break
129+
elif (Bust(s)):
130+
print("Dealer got Busted\nYou Won\n")
131+
break
132+
elif (sp < s):
133+
print("Dealer Won the Game\n")
134+
break
135+
136+
print(f"Dealer draws {arr[i]}")
137+
dl.append(arr[i])
138+
if arr[i][1] == 'J' or arr[i][1] == 'K' or arr[i][1] == 'Q':
139+
s += 10
140+
elif arr[i][1] == 'A':
141+
s += check_For_Ace(s)
142+
else:
143+
s += arr[i][1]

0 commit comments

Comments
 (0)