-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
142 lines (105 loc) · 4.5 KB
/
main.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
import random
import time
NUM_LINES= 4
NUM_COLUMNS= 3
SYMBOLS= ['A', 'B', 'C']
WON__K = 2 # Ratio of a winning bet
class Game:
def make_bet(self, amount:int):
while True:
temp_bet_amount= input('Make your bet $')
if temp_bet_amount.isdigit():
self.bet_amount=int(temp_bet_amount)
if self.bet_amount*self.lines_amount > amount:
print(f'Sorry! your bet(${self.bet_amount} * {self.lines_amount}) more than your amount(${amount})')
else:
break
else:
print('Money! Bet the money!')
print(f"Congratulations! You made a wager for ${self.bet_amount} amount, let's begin")
return self.bet_amount
def choose_lines(self):
while True:
input_n_lines= input(f'(to QUIT - press "q")) OR Choose how many lines will be the winner (1-{NUM_LINES}): ')
if input_n_lines == 'q':
return False
if input_n_lines.isdigit():
if 0 < int(input_n_lines) <= NUM_LINES:
self.lines_amount= int(input_n_lines)
break
else:
print(f'Put the right digit - 1 .. {NUM_LINES}')
print(f"GREATE! You chose {self.lines_amount} lines to win")
return self.lines_amount
class Slot:
"""
Game wheel is the main spinned device
"""
lines= []
result_lines= []
def spin(self):
"""
spin slot machine
"""
#fill the rows of drum
self.columns=[[random.choice(SYMBOLS) for j in range(NUM_LINES)] for i in range(NUM_COLUMNS)]
# print(self.columns)
self.lines.clear()
for row in range(NUM_LINES):
self.lines.append([])
for c in self.columns:
self.lines[row].append(c[row])
def show_result(self):
print('\n')
print('='*13)
self.result_lines=[]
for row in self.lines:
s= "| "
s+= ''.join([c + ' | ' for c in row])
s+= ' - \033[31m!WIN!\033[0m' if all([row[0] ==_ for _ in row]) else ' - LOSE'
self.result_lines.append(1 if all([row[0] ==_ for _ in row]) else - 0)
print(s)
print('='*13)
# print(self.result_lines)
# print(type(self.result_lines))
print(f'\n Won {sum(self.result_lines)} lines! ')
class Cashier:
def fill_amount(self):
temp_amount = input('Put you money to the slot $')
if temp_amount.isdigit():
self.amount = float(temp_amount)
print(f"You have ${self.amount} amount!")
def calc_profit(self, res_lines:list, bet_lines:int, bet:int) -> int:
sum_lines = sum(res_lines)
if sum_lines > 0:
if bet_lines <= sum_lines:
print(f'Congratilations! You won ${(min(bet_lines, sum_lines) * bet * WON__K) - (min(bet_lines, sum_lines) * bet)} !')
self.amount+= min(bet_lines, sum_lines)*bet*WON__K - (min(bet_lines, sum_lines) * bet)
else: # won less than bet lines
print(f'Congratilations! You just won ${(sum_lines * bet * WON__K) - (bet_lines * bet)} !')
self.amount+= (sum_lines * bet * WON__K) - (bet_lines * bet)
else:
print(f'Sorry! You loose ${bet * bet_lines}!')
self.amount-= bet * bet_lines
print(f'Your balance is: ${self.amount}')
return self.amount
def main_cycle():
cashier = Cashier()
cashier.fill_amount()
slot= Slot()
game= Game()
exit_flag= False
while not exit_flag:
if not game.choose_lines():
print(f'Your balance is ${cashier.amount}, BYE!')
break
game.make_bet(cashier.amount)
slot.spin()
slot.show_result()
cashier.calc_profit(slot.result_lines, game.lines_amount, game.bet_amount)
if cashier.amount==0:
print('Sorry! you have 0 at balance, back with money and start again')
exit_flag = True
time.sleep(1)
if __name__ == '__main__':
main_cycle()