-
Notifications
You must be signed in to change notification settings - Fork 0
/
slot_machine.py
212 lines (185 loc) · 6.66 KB
/
slot_machine.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import random
# GLOBAL VALUES (these won't change)
MAX_LINES = 3
MAX_BET = 1000
MIN_BET = 1
ROWS = 3
COLS = 3
# DICTIONARY OF POSSIBLE SYMBOLS/ODDS
symbol_odds = {
"$": 4,
"!": 8,
"#": 16,
"?": 24
}
# DICTIONARY OF WIN MULTIPLIERS
symbol_values = {
"$": 16,
"!": 8,
"#": 4,
"?": 2
}
def calculate_returns(columns, lines, bet, multipliers):
# Set winnings to 0
winnings = 0
# Create list of potential winning lines
winning_lines = []
# Loop through every row/line
for line in range(lines):
# Check the first symbol.
symbol = columns[0][line]
# Loop all columns and check if the first symbol is repeated.
for column in columns:
symbol_to_check = column[line]
# If symbol is NOT the same...
if symbol != symbol_to_check:
# Break if symbols are not the same
break
# Else runs if the for loop NEVER BREAKS!
else:
# winnings += the symbols value * the bet of the line (total bet/lines)
winnings += multipliers[symbol] * bet
winning_lines.append(line + 1)
return winnings, winning_lines
# Spin function. Takes in how many rows, columns and the dictionary of symbols
def spin(rows, cols, symbols):
print("==========SPIN==========")
# EMPTY LIST FOR SYMBOLS
all_symbols = []
# FOR EACH ITEM IN THE DICTIONARY
for symbol, symbol_odd in symbols.items(): #
# ADD THE SYMBOL AS MANY TIMES AS THE ODDS OF THAT SYMBOL TO THE LIST
# "_" IS A VARIABLE THAT DOESN'T NEED TO BE USED
for _ in range(symbol_odd):
all_symbols.append(symbol)
# COLUMN 0, 1, 2 (VERTICAL)
columns = []
# Loop through 'cols' amount of times
# _ means value won't be called
for _ in range(cols):
column = []
# Create copy of 'all_symbols'. [:] clones it. Without [:], it will update with 'all_symbols'
current_symbols = all_symbols[:]
# Loop through 'rows' amount of times
# _ means value won't be called
for _ in range(rows):
# choose random value from the 'current_symbols' list
value = random.choice(current_symbols)
# remove the value from the list, so it can't be used again this spin.
current_symbols.remove(value)
# Add the value to the current column list
column.append(value)
# Add column to the columns list (list within a list)
columns.append(column)
return columns
def print_lines(columns):
# Loop through each column, but one index at a time
for row in range(len(columns[0])):
# print the symbol in that row.
for i, column in enumerate(columns):
# If it is the LAST loop, don't print " | "
if i != len(column) - 1:
# At the 'end', don't print a new line
print(column[row], end="|")
else:
# At the 'end', don't print a new line
print(column[row], end="")
# NEXT LINE
print()
def get_deposit():
while True:
# Get how much they want to deposit
amount = input("How much would you like to deposit? £")
# Check if it is an integer, greater than 0.
if amount.isdigit():
# Convert to int.
amount = int(amount)
# Final check to make sure that we don't get 0 or less.
if amount > 0:
break
else:
print("Invalid amount. Please try again.")
else:
print("Invalid input.")
return amount
def get_lines():
while True:
# Get how much they want to deposit
lines = input(f"How many lines do you want to play? (1-{str(MAX_LINES)}) ")
# Check if it is an integer, greater than 0.
if lines.isdigit():
# Convert to int.
lines = int(lines)
# Check if value is equal to or greater than 1 AND equal to or less than MAX_LINES (3)
if 1 <= lines <= MAX_LINES:
break
else:
print("Invalid amount. Please try again.")
else:
print("Invalid input.")
return lines
def get_bet():
while True:
# Get how much they want to deposit
bet = input("How much would you like to bet? £")
# Check if it is an integer, greater than 0.
if bet.isdigit():
# Convert to int.
bet = int(bet)
# Final check to make sure that bet is between MIN_BET and MAX_BET.
if MIN_BET <= bet <= MAX_BET:
break
elif bet < MIN_BET:
print(f"Minimum bet is £{MIN_BET}")
elif bet > MAX_BET:
print(f"Maximum bet is £{MAX_BET}")
else:
print("Invalid amount. Please try again.")
else:
print("Invalid input.")
return bet
def play_game():
# GET BALANCE/DEPOSIT AMOUNT
balance = get_deposit()
# ASK HOW MANY LINES THEY WANT TO BET
lines = get_lines()
if lines > balance:
print("You don't have enough to play.")
return
# ASK HOW MUCH THEY WANT TO BET PER LINE
# CHECK BET AMOUNT IS VALID
while True:
bet = get_bet()
total_bet = bet * lines
if total_bet > balance:
print(f"Insufficient funds. Balance: £{balance}")
else:
# UPDATE BALANCE
balance -= total_bet
break
# SUMMARY OF BET
print(f"BET: £{bet}, LINES: {lines}\nTOTAL BET: £{bet * lines}")
print(f"Updated Balance: £{balance}")
# SPIN AND PRINT THE SLOTS/LINES
slots = spin(ROWS, COLS, symbol_odds)
print_lines(slots)
# CALCULATE WINNINGS (if any)
winnings, winning_lines = calculate_returns(slots, lines, bet, symbol_values)
# Print how much you won (if anything)
print(f"You won: £{winnings}")
# This will only print if winning_lines is NOT empty.
print(f"WINNING LINES:", *winning_lines)
# ADD WINNINGS TO BALANCE
balance += winnings
# DISPLAY NEW BALANCE!
print(f"Total Money Returned to you: £{balance}")
# Ask if player wants to play. Yes or No.
while True:
y = ["y", "yes", "ye", "yea", "yeah", "yeahh", "yup", "yh", "ja"]
play = input("Do you want to play? ").lower()
if play not in y and play != "no":
print("Invalid entry, please try again.")
elif play in y:
play_game()
elif play == "no":
break