-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
138 lines (107 loc) · 3.6 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
import math
from player import Player, Action
from dealer import Dealer
from winner import ShowHands
# Action Set : {check-0, call-1, bet-2, raise-3, fold-4}
# State Set : {pre-flop 0, flop 1, turn 2, river 3}
class Game:
def __init__(self, player_cnt = 2):
self.players_ = []
for i in range(player_cnt):
self.players_.append(Player(i+1))
self.pot_ = 0
self.winner_ = None
def __str__(self):
return "\n\t[Pot : " + str(self.pot_) + "]\n"
def play_one_state(self, state_type):
last_action = None
player_turn = 0
while not last_action == Action(4): # While not FOLD
# Switch player
player = self.players_[player_turn % 2]
player.act(state_type, last_action) # 0 for pre-flop
if player.last_action_ == None:
# no more turns left, so go to the next state
# deal the next community cards
break
player.print_last_action()
# Update the new last action
last_action = player.last_action_
player.add_action_history(last_action)
# Update the current stack according to the action
if player.decrease_stack(last_action):
# Update the current pot according to the action
self.pot_ += 1
if last_action.index_ == 3:
self.pot_ += 1
# Handle 2 special cases here!!!
# Call -> Check and Raise -> Bet
if player_turn == 0:
if last_action.index_ == 1:
last_action.index_ = 0
elif last_action.index_ == 3:
last_action.index_ = 2
#Update the counter
player_turn += 1
if last_action == Action(4):
self.winner_ = self.players_[player_turn % 2] # other player won
return False
return True
def play_one_round(self):
dealer = Dealer() # Create new dealer for every round
self.pot_ = 3 # Clear the pot by setting it to small + big blinds
self.winner_ = None
self.players_[0].set_blind(0) # Player 0 is always small blind
self.players_[1].set_blind(1) # Player 1 is always big blind
self.show_game_status()
for player in self.players_:
player.set_cards(dealer.deal_pockets())
player.show_cards()
if self.play_one_state(0): # pre-flop
dealer.deal_flop()
self.show_game_status()
if self.play_one_state(1): # pre-turn
self.show_game_status()
dealer.deal_turn()
if self.play_one_state(1): # pre-river
dealer.deal_river()
# Find the winner of the round
if not self.winner_ == None:
self.winner_.increase_stack(self.pot_)
else: # User winner.py
for player in self.players_:
player.show_cards()
winner_id = ShowHands.Winner(self.players_[0].pocket_cards_, self.players_[1].pocket_cards_, dealer.communitycards_)
#print("Debugging Test winner_id : " + str(winner_id))
if not winner_id == None:
for player in self.players_:
if player.check_id(winner_id+1):
player.increase_stack(self.pot_)
self.winner_ = player
break
else: #Then it's a tie!!!
for player in self.players_:
player.increase_stack(self.pot_/2)
self.show_game_status()
def start(self):
game_end = False
round_cnt = 0
while round_cnt < 3 and not game_end:
print("\n----------------------------------\n")
self.play_one_round()
print("\n----------------------------------\n")
#self.show_game_status()
# Before going into the next round, do a few things!
# Check game over conditions:
for player in self.players_:
if player.lost():
game_end = True
# Switch players for the next round:
players = self.players_.reverse()
round_cnt += 1
def show_game_status(self):
print(self)
print(self.players_) # Show player status
print()
G = Game()
G.start()