-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
145 lines (119 loc) · 4.13 KB
/
main.py
File metadata and controls
145 lines (119 loc) · 4.13 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
# Player
# Table
# Bet
from random import randrange, choice
class Player:
def __init__(self, name, depo, table):
"""
Player object
:param name:
:param depo:
:param table:
"""
self.name = name
self.depo = depo
self.table = table
def make_bet(self, amount, cond: tuple):
"""
Create bet with win condition, add it on table
"""
Player.check_valid(self, cond)
if 0 < amount < self.depo:
self.depo -= amount
self.table.bet_list.append(Bet(amount, cond, self))
elif amount > self.depo:
print(choice(["You can't bet more than you have", "Out of money", "Cannot afford", "Need more money"]))
elif amount < 0:
print(choice(["You can't bet less than 1!", "Nice try", "Try one more time"]))
def check_valid(self, cond: tuple):
# TODO: Make different messages
print(f"Attempt to bet {cond}")
if len(cond) != 2:
raise BetError('Wrong value(s)')
elif cond[0] not in ['num', 'colour', 'half', 'dozen', 'parity']:
raise BetError('Wrong value(s)')
elif cond[0] == 'num' and (cond[1] > 36 or cond[1] < 1):
raise BetError('Wrong value(s)')
elif cond[0] == 'colour' and cond[1] not in ['black', 'red']:
raise BetError('Wrong value(s)')
elif cond[0] == 'half' and cond[1] not in [1, 2]:
raise BetError('Wrong value(s)')
elif cond[0] == 'dozen' and cond[1] not in [1, 2, 3]:
raise BetError('Wrong value(s)')
elif cond[0] == 'parity' and cond[1] not in ['odd', 'even']:
raise BetError('Wrong value(s)')
class Bet:
coefs = {'num': 72, 'colour': 2, 'half': 2, 'dozen': 4, 'parity': 2}
def __init__(self, amount, condition: tuple, player: Player):
"""
Bet made by player.
"""
self.amount = amount
self.condition = condition
self.player = player
def check_for_win(self, result):
"""
Returns True if bet wins
"""
return self.condition in result.items()
@property
def coef(self):
"""
Determine self.coef based on self.condition
"""
return Bet.coefs[self.condition[0]]
class Table:
"""
Has bet list, random generator
"""
desk = {
"red": [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36],
"black": [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35]
}
def __init__(self):
self.bet_list = []
self.result = None
self.winners = []
def ball_throw(self):
"""
Result contains colour, num, half and dozen in which num is -> (num, colour, half, dozen)
"""
num = randrange(1, 37)
parity = ["odd", "even"][num % 2 == 0]
colour = ["red", "black"][num in Table.desk["black"]] # исправлено
half = [1, 2][19 <= num <= 36]
if 0 <= num <= 12:
dozen = 1
elif 13 <= num <= 24:
dozen = 2
else:
dozen = 3
self.result = {'num': num, 'colour': colour, 'half': half, 'dozen': dozen, 'parity': parity}
def update_depo(self):
"""
Update depo for each win bet owner according to bet coef
"""
while self.bet_list:
bet = self.bet_list.pop()
if bet.check_for_win(self.result):
self.winners.append(bet.player.name)
bet.player.depo += bet.amount * bet.coef
def no_more_bets(self):
print('Ball has been thrown!')
self.ball_throw()
print('Current result: {}'.format(self.result))
self.update_depo()
print('Lucky ones: {}'.format(self.winners))
self.winners.clear()
class BetError(Exception):
"""
BetError class
"""
###################################################
# Пример работы
# table = Table()
# player1 = Player("Player 1", 1000, table)
# player2 = Player("Player 2", 1000, table)
# player1.make_bet(100, {"half": 1})
# player2.make_bet(200, {"color": "red"})
# table.no_more_bets()