-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.py
147 lines (131 loc) · 4.79 KB
/
blackjack.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# import function P1Random from p1_random module
from p1_random import P1Random
# declare global variables
rng = P1Random()
player_card = ""
second_card = ""
computer_card = ""
num_player = 0
num_won = 0
num_lost = 0
num_ties = 0
num_played = 0;
hand = 0
player_exit = False
# while loop which loops until player wants to exit(player_exit = true)
while not player_exit:
# Gets random number and assigns card based on random number
print("START GAME#", (num_played + 1))
print()
random_number = rng.next_int(13) + 1
if random_number == 1:
card = "ACE!"
elif 2 <= random_number <= 10:
card = str(random_number) + "!"
elif random_number == 11:
card = "JACK!"
elif random_number == 12:
card = "QUEEN!"
elif random_number == 13:
card = "KING!"
# Assigns face cards with specific values
if card == 'KING!' or card == 'QUEEN!' or card == 'JACK!':
random_number = 10
hand += random_number
print("Your card is a", card)
print("Your hand is:", hand)
print()
# Loop which asks player for input on how to continue game
while not player_exit:
print("1. Get another card")
print("2. Hold hand")
print("3. Print statistics")
print("4. Exit")
print()
option = int(input("Choose an option:"))
print()
# Assigns card based on random number and userinput.
if option == 1:
second_random_number = rng.next_int(13) + 1
if second_random_number == 1:
second_card = "ACE!"
elif 2 <= second_random_number <= 10:
second_card = str(second_random_number) + "!"
elif second_random_number == 11:
second_card = "JACK!"
elif second_random_number == 12:
second_card = "QUEEN!"
elif second_random_number == 13:
second_card = "KING!"
if second_card == 'KING!' or second_card == 'QUEEN!' or second_card == 'JACK!':
second_random_number = 10
hand += second_random_number
print("Your card is a", second_card)
print("Your hand is:", hand)
print()
# Shows possible win and lose cases and breaks the outer loop
if hand == 21:
print("BLACKJACK! You win!")
num_won += 1
hand = 0
num_played += 1
break
elif hand > 21:
print("You exceeded 21! You lose.")
num_lost += 1
hand = 0
num_played += 1
break
# Shows case where user holds hand and competes with dealer
# Gives dealer a specific card and random number and chooses winner/loser based on conditions
elif option == 2:
num_played += 1
computer_number = (rng.next_int(11) + 16)
print("Dealer's hand:", computer_number)
print("Your hand is:", hand)
print()
if computer_number > 21:
print("You win!")
print()
num_won += 1
hand = 0
break
# Each conditional statement updates global variables and statistics
elif computer_number == hand:
print("It's a tie! No one wins!")
num_ties += 1
hand = 0
break
elif computer_number > hand:
print("Dealer wins!")
num_lost += 1
hand = 0
break
elif hand > computer_number:
print("You win!")
print()
num_won += 1
hand = 0
break
# Shows option where user wants to print statistics
elif option == 3:
if num_played == 0:
percentage = 0
else:
percentage = (num_won / num_played) * 100
percentage = str(percentage) + "%"
print("Number of Player wins:", num_won)
print("Number of Dealer wins:", num_lost)
print("Number of tie games:", num_ties)
print("Total # of games played is:", num_played)
print("Percentage of Player wins:", percentage)
continue
# option where user chooses to exit the game
elif option == 4:
player_exit = True
break
# if user inputs invalid input prints error and restarts inner loop
else:
print("Invalid input!")
print("Please enter an integer value between 1 and 4.")
continue