1+ import random
2+
3+ # Constant variables
4+ MAX_LINE = 3
5+ MAX_BET = 100
6+ MIN_BET = 1
7+
8+ ROWS = 3
9+ COLS = 3
10+
11+ symbols_count = {
12+ 'A' :2 ,
13+ 'B' :4 ,
14+ 'C' :6 ,
15+ 'D' :8
16+ }
17+
18+ symbols_values = {
19+ 'A' :5 ,
20+ 'B' :3 ,
21+ 'C' :4 ,
22+ 'D' :2
23+ }
24+
25+
26+ def check_winnings (columns , lines , bet , values ):
27+ winnings = 0
28+ lines_won = []
29+ for line in range (lines ):
30+ symbol = columns [0 ][line ]
31+ for column in columns :
32+ symbol_to_check = column [line ]
33+ if symbol != symbol_to_check :
34+ break
35+ else :
36+ winnings += values [symbol ] * bet
37+ lines_won .append (line + 1 )
38+
39+ return winnings , lines_won
40+
41+
42+
43+
44+ def get_slot_machine_spin (rows , cols , symbols ):
45+ all_symbols = []
46+ for symbol , symbols_count in symbols .items ():
47+ for _ in range (symbols_count ):
48+ all_symbols .append (symbol )
49+
50+ columns = []
51+ for _ in range (cols ):
52+ column = []
53+ current_symbols = all_symbols [:]
54+ for _ in range (rows ):
55+ value = random .choice (current_symbols )
56+ current_symbols .remove (value )
57+ column .append (value )
58+ columns .append (column )
59+
60+ return columns
61+
62+ def print_slot (columns ):
63+ for row in range (len (columns [0 ])):
64+ for i , column in enumerate (columns ):
65+ if i != len (columns )- 1 :
66+ print (column [row ],end = ' | ' )
67+ else :
68+ print (column [row ], end = '' )
69+ print ()
70+
71+
72+
73+
74+ def deposit ():
75+ while True :
76+ amount = input ('How much would you like to deposit: $' )
77+ if amount .isdigit ():
78+ amount = float (amount )
79+ if amount > 0 :
80+ break
81+ else :
82+ print ('Amount must be greater than zero!' )
83+ else :
84+ print ('Please enter a number!' )
85+ print ('----------------------------------------------------------------' )
86+
87+ return amount
88+
89+ def get_number_of_lines ():
90+ while True :
91+ lines = input ('Enter the number of lines to bet on(1-' + str (MAX_LINE ) + '): ' )
92+ if lines .isdigit ():
93+ lines = int (lines )
94+ if 0 < lines <= MAX_LINE :
95+ break
96+ else :
97+ print ('Enter a valid number of lines!' )
98+ else :
99+ print ('Please enter a number!' )
100+ print ('----------------------------------------------------------------' )
101+
102+ return lines
103+
104+ def get_bet ():
105+ while True :
106+ amount = input ('How much would you like to bet: $' )
107+ if amount .isdigit ():
108+ amount = float (amount )
109+ if MIN_BET <= amount <= MAX_BET :
110+ break
111+ else :
112+ print (f'Betting amount must be between ${ MIN_BET } - ${ MAX_BET } !' )
113+ else :
114+ print ('Please enter a number!' )
115+
116+ print ('----------------------------------------------------------------' )
117+
118+ return amount
119+
120+ def spin (balance ):
121+ lines = get_number_of_lines ()
122+ while True :
123+ bet = get_bet ()
124+ total_bet = bet * lines
125+
126+ if total_bet > balance :
127+ print ('You do not have enough balance to bet!' )
128+ print (f'Current balance: ${ balance } ' )
129+ print ('----------------------------------------------------------------' )
130+ else :
131+ break
132+
133+ print (f'You are betting ${ bet } on ${ lines } lines.\n Total bet: ${ total_bet } ' )
134+ print ('----------------------------------------------------------------' )
135+
136+ slots = get_slot_machine_spin (ROWS , COLS , symbols_count )
137+ print_slot (slots )
138+ winnings , lines_won = check_winnings (slots , lines , bet , symbols_values )
139+ print ('----------------------------------------------------------------' )
140+ print (f'You won ${ winnings } . ' )
141+ print (f'You won on lines:' , * lines_won )
142+ print ('----------------------------------------------------------------' )
143+ return winnings - total_bet
144+
145+ def main ():
146+ balance = deposit ()
147+ while True :
148+ print (f'Current balance: ${ balance } ' )
149+ answer = input ('Press enter to play (q to quit): ' )
150+ if answer == 'q' :
151+ break
152+ balance += spin (balance )
153+
154+ print (f'Current balance: ${ balance } ' )
155+
156+ if __name__ == '__main__' :
157+ main ()
0 commit comments