-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathmain.py
More file actions
211 lines (167 loc) · 5.17 KB
/
main.py
File metadata and controls
211 lines (167 loc) · 5.17 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
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
import random
MAX_LINES = 3
MAX_BET = 100
MIN_BET = 1
ROWS = 3
COLS = 3
# Dictionary defining the count of each symbol
symbol_count = {
"A": 2,
"B": 4,
"C": 6,
"D": 8
}
# Dictionary defining the value of each symbol
symbol_value = {
"A": 5,
"B": 4,
"C": 3,
"D": 2
}
def check_winnings(columns, lines, bet, values):
"""
Calculate the winnings based on the columns, lines, bet, and symbol values.
Args:
columns (list): The slot machine columns.
lines (int): Number of lines to bet on.
bet (int): Bet amount for each line.
values (dict): Symbol values.
Returns:
tuple: A tuple containing the total winnings and the winning lines.
"""
winnings = 0
winning_lines = []
for line in range(lines):
symbol = columns[0][line]
for column in columns:
symbol_to_check = column[line]
if symbol != symbol_to_check:
break
else:
winnings += values[symbol] * bet
winning_lines.append(line + 1)
return winnings, winning_lines
def get_slot_machine_spin(rows, cols, symbols):
"""
Generate a random spin of the slot machine.
Args:
rows (int): Number of rows in the slot machine.
cols (int): Number of columns in the slot machine.
symbols (dict): Dictionary defining the count of each symbol.
Returns:
list: A list of columns representing the slot machine spin.
"""
all_symbols = []
for symbol, symbol_count in symbols.items():
for _ in range(symbol_count):
all_symbols.append(symbol)
columns = []
for _ in range(cols):
column = []
current_symbols = all_symbols[:]
for _ in range(rows):
value = random.choice(current_symbols)
current_symbols.remove(value)
column.append(value)
columns.append(column)
return columns
def print_slot_machine(columns):
"""
Print the slot machine spin.
Args:
columns (list): The slot machine columns.
"""
for row in range(len(columns[0])):
for i, column in enumerate(columns):
if i != len(columns) - 1:
print(column[row], end=" | ")
else:
print(column[row], end="")
print()
def deposit():
"""
Prompt the user to enter the deposit amount.
Returns:
int: The deposited amount.
"""
while True:
amount = input("What would you like to deposit? $")
if amount.isdigit():
amount = int(amount)
if amount > 0:
break
else:
print("Amount must be greater than 0.")
else:
print("Please enter a number.")
return amount
def get_number_of_lines():
"""
Prompt the user to enter the number of lines to bet on.
Returns:
int: The number of lines to bet on.
"""
while True:
lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
if lines.isdigit():
lines = int(lines)
if 1 <= lines <= MAX_LINES:
break
else:
print("Enter a valid number of lines.")
else:
print("Please enter a number.")
return lines
def get_bet():
"""
Prompt the user to enter the bet amount for each line.
Returns:
int: The bet amount.
"""
while True:
amount = input("What would you like to bet on each line? $")
if amount.isdigit():
amount = int(amount)
if MIN_BET <= amount <= MAX_BET:
break
else:
print(f"Amount must be between ${MIN_BET} - ${MAX_BET}.")
else:
print("Please enter a number.")
return amount
def spin(balance):
"""
Perform a spin of the slot machine.
Args:
balance (int): The current balance.
Returns:
int: The updated balance after the spin.
"""
lines = get_number_of_lines()
while True:
bet = get_bet()
total_bet = bet * lines
if total_bet > balance:
print(f"You do not have enough to bet that amount, your current balance is: ${balance}")
else:
break
print(f"You are betting ${bet} on {lines} lines. Total bet is equal to: ${total_bet}")
slots = get_slot_machine_spin(ROWS, COLS, symbol_count)
print_slot_machine(slots)
winnings, winning_lines = check_winnings(slots, lines, bet, symbol_value)
print(f"You won ${winnings}.")
print(f"You won on lines:", *winning_lines)
return balance + winnings - total_bet
def main():
"""
Main function to run the slot machine game.
"""
balance = deposit()
while True:
print(f"Current balance is ${balance}")
answer = input("Press enter to play (q to quit).")
if answer == "q":
break
balance = spin(balance)
print(f"You left with ${balance}")
main()