-
Notifications
You must be signed in to change notification settings - Fork 3
/
game.py
155 lines (130 loc) · 4.36 KB
/
game.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
143
144
145
146
147
148
149
150
151
152
153
154
155
from cards import Hand
import handscore
"""
Data classes relating to the game of poker
"""
class Match(object):
"""Container for information about a group of games"""
def reset_match(self):
self.round = 0
self.opponents = []
self.me = None
self.time_per_move = 500
def update_match(self):
if 'your_bot' in self.sharedData:
self.me = self.sharedData.pop('your_bot')
if self.me in self.opponents:
del self.opponents[self.me]
if 'round' in self.sharedData:
round_string = self.sharedData.pop('round')
try:
self.round = int(round_string)
except ValueError:
pass
if 'bots' in self.sharedData:
bot_list = self.sharedData.pop('bots')
for bot in bot_list:
if bot == self.me:
continue
self.opponents.append(bot)
if 'time_per_move' in self.sharedData:
time_string = self.sharedData.pop('time_per_move')
try:
self.time_per_move = int(time_string)
except ValueError:
pass
class Round(object):
"""Memory for a full hand of poker"""
def reset_round(self):
self.table_cards = []
self.hand = None
self.pot = 0
self.to_call = 0
self.bets = {}
self.big_blind = 0
self.small_blind = 0
self.button = None
self.stacks = {}
self.preflop_fear = -1
self.hand_fear = handscore.HandScore()
def update_round(self):
if 'roundOver' in self.sharedData:
round_is_over = self.sharedData.pop('roundOver')
if round_is_over:
self.reset_round()
self.parse_blinds()
self.parse_cards()
self.parse_pot()
self.parse_bets()
self.parse_stacks()
if 'on_button' in self.sharedData:
self.button = self.sharedData.pop('on_button')
def parse_cards(self):
if hasattr(self, 'me') and self.me:
key = ('hand', self.me)
if key in self.sharedData:
cards = self.sharedData.pop(key)
self.hand = Hand(cards[0], cards[1])
if 'table' in self.sharedData:
self.table_cards = self.sharedData.pop('table')
def parse_pot(self):
if 'pot' in self.sharedData:
pot_str = self.sharedData.pop('pot')
try:
self.pot = int(pot_str)
except ValueError:
pass
if 'amount_to_call' in self.sharedData:
call_str = self.sharedData.pop('amount_to_call')
try:
self.to_call = int(call_str)
except ValueError:
pass
def parse_blinds(self):
if 'small_blind' in self.sharedData:
sb_string = self.sharedData.pop('small_blind')
try:
self.small_blind = int(sb_string)
except ValueError:
pass
if 'big_blind' in self.sharedData:
bb_string = self.sharedData.pop('big_blind')
try:
self.big_blind = int(bb_string)
except ValueError:
pass
def parse_bets(self):
reset = (self.to_call == 0)
for bot in self.__get_bots():
bet_key = ('bet', bot)
bet = self.sharedData.get(bet_key)
if reset:
self.bets[bot] = 0
elif bet:
self.bets[bot] = bet
def parse_stacks(self):
for bot in self.__get_bots():
stack_key = ('stack', bot)
stack = self.sharedData.get(stack_key, "")
try:
self.stacks[bot] = int(stack)
except ValueError:
self.stacks[bot] = 0
def __get_bots(self):
if not hasattr(self, 'opponents') or not self.opponents:
try:
return [self.me]
except AttributeError:
return []
return self.opponents + [self.me]
class GameData(Match, Round):
"""Aggregate data classes mixed in together"""
def __init__(self, sharedData):
self.sharedData = sharedData
self.reset()
def reset(self):
self.reset_match()
self.reset_round()
def update(self):
self.update_match()
self.update_round()