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