forked from andreaswestre/Cashgame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_cashgame
203 lines (186 loc) · 6.87 KB
/
main_cashgame
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
all_states = ["set_players", "add_player", "rebuy", "print_stacks",
"print_final_stacks","change_chipvalue", "chip_to_kr","calculate"]
all_states_names = ["Decide number of players", "Add player", "Rebuy/Buyout(?)", "Stack table", "Final stacks(?)", "Change chip value", "Chip to kr", "Calculate"]
players = {} #Key: navn, value: stackvalue
final_stacks = {} #Slutt stacks
actual_pot: float = 0 #Faktisk
potsize: float = 0 #Teoretisk
chipvalue: float = 2.0 #Kr / chip
num: int = 0 #Antall spillere
currency = "chips" #Kr eller chips
def main():
print("WELCOME TO A GREAT CASHGAME ")
set_players()
while True:
i = 0
print("\nPick mode: ")
for mode in all_states_names:
i += 1
print(i,mode)
mode = input("Mode: ") # 1,2,3,4,5
if mode in all_states:
eval(mode + "()")
elif mode.isdigit():
mode = int(mode)
if mode > 0 and mode < len(all_states):
state = all_states[mode-1] + "()"
eval(state)
else:
print("Number must be between 1 and ", len(all_states))
else:
print("Mode not valid. ")
def get_potsize():
global potsize
potsize = 0
for player in players:
potsize += players[player]
def add_player():
global num #antall spillere
info = input("Name and stack ")
list = info.split()
while (len(list) < 2) or not list[-1].isdigit():
print("Wrong syntax.")
info = input("Name and stack ")
list = info.split()
length = len(list)
stack = int(list[-1])#Siste element i lista
name = ""
for nom in list[:-1]:
name += nom + " "
name = name[:-1]
players[name] = stack
num += 1
final_stacks[name] = 0 #Initialiserer sluttstacken
get_potsize()
def correction(newname):
for player in players:
name = "error"
for i in range(3):
if(len(newname) == i+1 or len(newname) > 2):
if player[:i+1] == newname[:+1]:
name:str = player
print("Did you mean: ", name , "? y/n")
ans = input()
if ans != "n":
return name
else:
print("Try again, idiot")
return "error"
return "error"
def set_players():
number = int(input("How many players? "))
while (number > 0):
add_player()
number-=1
def rebuy():
global potsize
name = input("Who? ")
while name not in players or name == "error":
name = correction(name)
if name == "error":
print("Enter another name, not valid ")
name = input("Who? ")
value = int(input("How much?(negative for buy-out)"))
if value < 0:
final_stacks[name] -= value
print(name,": ")
players[name] += value
potsize += value
print(name, " ", value)
def print_stacks():
global potsize
print("\nStack sizes")
print("="*35)
print("Name".ljust(20), "Size".ljust(5), "Currency".ljust(10))
print("-"*35)
for p in players:
print(str(p).ljust(20), str(players[p]).ljust(5),str(currency).ljust(10))
get_potsize()
print("\nPot size should be ",potsize)
def print_final_stacks():
print()
for p in final_stacks:
print(p, final_stacks[p],currency)
get_potsize()
print("Pot size should be", potsize, chips)
print()
def finalize(): #Setter opp sluttstacks og sjekker om alt stemmer
global actual_pot
global potsize
global chipvalue
print("Potsize: ", potsize)
remaining = potsize
actual_pot = 0
for player in players:
who = int(input(player + " "))
if player not in final_stacks:
final_stacks[player] = who
else:
final_stacks[player] += who
actual_pot += who #counting
remaining -= who #For nice print
print("Remaining: ", remaining)
final_stacks[player] -= players[player] #Gir en liste som viser hvem som har vunnet/tapt alt
print("Potsize is: ", actual_pot, currency,". Should be " , potsize)
if (actual_pot != potsize):
new = input("Do you want to calculate a new chipvalue? y/n ")
if new == "y":
new_chipvalue()
def calculate():#Manipulerer final_stacks til alt er riktig
global chipvalue
finalize()
chip_to_kr()
print_final_stacks()
simple_pay()
for player in final_stacks:
for loser in final_stacks:
if ( final_stacks[loser] < 0 and final_stacks[player] > 0 ): # Noen skal betale når vi har
if (final_stacks[loser] + final_stacks[player] < 0): # Taperen skylder mer enn vinneren skal ha
print(loser, " pays ", player, "kr ", round(final_stacks[player],0) )
final_stacks[loser] += final_stacks[player]
final_stacks[player] = 0
else: #Vinneren skal ha alt taperen har
print(loser, " pays ", player, "kr ", round(final_stacks[loser] * -1,0))
final_stacks[player] += final_stacks[loser]
final_stacks[loser] = 0
print_final_stacks()
chip_to_kr()
def simple_pay(): #Sjekker om noen skylder og skal ha det samme beløpet
for player in final_stacks:
for loser in final_stacks:
if (loser != player and final_stacks[loser] != 0): # a player cannot pay himself
if (final_stacks[player] + final_stacks[loser] == 0):
print(loser, " simply pays ", player, "kr ", final_stacks[loser] * -1)
final_stacks[player] = 0
final_stacks[loser] = 0
def new_chipvalue():
global chipvalue
chipvalue = 2
global actual_pot
global potsize
in_play = float(actual_pot * chipvalue )#How many KR in play
round(in_play, 2)
if potsize < actual_pot:
tooMany = actual_pot - potsize
print("There are", tooMany, " chips too many.")
else:
tooFew = potsize - actual_pot
print("There are", tooFew, " chips missing.")
chipvalue *= potsize / actual_pot #krone / chip
print("Chipvalue is changed to ", round(chipvalue,2), "KR / chip" )
def change_chipvalue():
global chipvalue
chipvalue = input("Set new chipvalue: (kr / chip )")
def chip_to_kr():
global chipvalue
global currency
if currency == "chips":
for player in final_stacks:
final_stacks[player] *= chipvalue
players[player] *= chipvalue
currency = "kr"
elif currency == "kr":
for player in final_stacks:
final_stacks[player] /= chipvalue
players[player] /= chipvalue
currency = "chips"