4 # ***************************************
5 from os import system, kill
6 from popen2 import popen2,popen3
8 def __init__(self, name='', brain=''):
13 self.IN, self.OUT = 0,0
14 self.brain = brain.strip()
21 self.OUT, self.IN = popen2("./"+self.brain)
24 self.IN.write("END\n"); self.IN.flush()
25 system("pkill -9 "+self.name)
27 def bet(self, wager, other_player):
29 if wager > self.cash: wager = self.cash
30 if wager < 0: wager = 0
31 if wager-self.owe > other_player.cash:
32 wager = other_player.cash
35 self.owe = self.owe if self.owe < 0 else 0
36 GP("ACTION Player ", self.name, " bets: ", wager)
39 def record_info(self, pot, min_raise, flop, turn, river, other_player):
41 self.IN.write("INFO NAME " + str(self.name) + '\n'); self.IN.flush()
42 self.IN.write("INFO ONAME " + str(other_player.name) + '\n'); self.IN.flush()
43 self.IN.write("INFO STACK " + str(self.cash) + '\n'); self.IN.flush()
44 self.IN.write("INFO OSTACK " + str(other_player.cash) + '\n'); self.IN.flush()
45 self.IN.write("INFO POT " + str(pot) + '\n'); self.IN.flush()
46 self.IN.write("INFO MINRAISE " + str(min_raise)+'\n'); self.IN.flush()
47 self.IN.write("INFO OWE " + str(self.owe) + '\n'); self.IN.flush()
48 self.IN.write("INFO HOLE " + pp_hand(self.hole) + '\n'); self.IN.flush()
49 self.IN.write("INFO FLOP " + pp_hand(flop) + '\n'); self.IN.flush()
50 self.IN.write("INFO TURN " + pp_hand(turn) + '\n'); self.IN.flush()
51 self.IN.write("INFO RIVER " + pp_hand(river) + '\n'); self.IN.flush()
52 self.GP_FLUSH(GLOBAL_TXT)
54 def GP_FLUSH(self, TXT):
56 if g not in self.GP_BUFFER:
57 self.GP_BUFFER.append(g)
58 self.IN.write(g + "\n")
61 def human_play(self, pot, min_raise, flop, turn, river, other_player):
62 if GLOBAL_TXT: print GLOBAL_TXT[-1]
63 print "[", pp_hand(self.hole), "] Board: ",
64 print "[", pp_hand(flop), pp_hand(turn), pp_hand(river), "]"
65 print "POT: ", pot, " OWE: ", self.owe, " MIN_RAISE: ", min_raise,
66 print " | ", "STACK: ", self.cash, "Opp. STACK: ", other_player.cash
67 return raw_input("What do you want to bet, " + self.name + ": ")
69 def decide(self, pot, min_raise, flop, turn, river, other_player,endgame=False):
74 in_bet = self.human_play(pot,min_raise, flop, turn, river, other_player)
77 self.record_info(pot,min_raise,flop,turn,river,other_player)
78 self.IN.write("MOVE \n"); self.IN.flush()
79 in_bet = self.OUT.readline().strip()
81 if endgame: return False
84 in_bet = in_bet.upper()
85 if in_bet == 'A': in_bet = self.cash
86 elif in_bet == 'C': in_bet = self.owe
87 else : self.FOLD = True
89 try : in_bet = int(in_bet)
90 except: self.FOLD = True
92 if self.FOLD: return False
94 if in_bet >= self.cash or in_bet == self.owe or in_bet >= min_raise:
95 out_bet = self.bet(in_bet, other_player)
96 other_player.owe += out_bet
99 # Bets between whats owed and min_raise are considered a call
100 if in_bet >= self.owe and in_bet < min_raise:
101 out_bet = self.bet(self.owe, other_player)
102 other_player.owe += out_bet
105 # Illegal bets will be counted as folds!
106 GP("ACTION Player ", self.name, "bets ", str(in_bet), " illegally and FOLDS. Valid bets are ", str(self.owe), " or anything >= ", min_raise)
111 def GP(*S): GLOBAL_TXT.append(''.join(map(str,S)))
112 def returnGP(): return GLOBAL_TXT
114 def hand_judgement(A,B, board, pot):
116 GP("ACTION Player ", A.name, " folds" )
117 GP("ACTION Player ", B.name, " wins: ", pot)
122 GP("ACTION Player ", B.name, "folds")
123 GP("ACTION Player ", A.name, "wins: ", pot)
127 S = HOLDEM_score(A, B, board)
130 GP("ACTION Player ", A.name, "wins: ", pot)
135 GP( "ACTION Player ", B.name, "wins: ", pot)
140 GP("ACTION Split pot ")
141 split_pot, carry = pot/2, pot%2
142 A.cash += split_pot + carry
147 if A.FOLD or B.FOLD: return True
150 def betting_round(P,pot,min_raise,flop,turn,river):
154 while((P[0].owe>0 or P[1].owe>0) or (P[0].option or P[1].option) and (P[0].cash)):
155 action = P[0].decide(pot, min_raise, flop,turn,river, P[-1])
158 if action >= min_raise: min_raise = 2*action
159 if checkFOLD(P[0],P[1]): return pot
162 P = [P[-1]] + P[:-1] # Cycle the player order
166 def gameplay(DECK, A, B, smallB, ):
169 # Shuffle the deck and deal two cards to each player
172 A.hole = [DECK[n] for n in xrange(2) ]
173 B.hole = [DECK[n] for n in xrange(2,4)]
174 board = [DECK[n] for n in xrange(4,9)]
176 flop, turn, river = '', '', ''
182 A.FOLD, B.FOLD = False, False
184 # Handle the case if a player can't post the blinds completely, ALL_IN
186 pot += B.bet(B.owe, A)
188 return hand_judgement(A,B,board,pot)
190 pot += A.bet(A.owe, B)
192 return hand_judgement(A,B,board,pot)
194 # Both players can post the blinds
195 pot += A.bet(A.owe, B)
196 pot += B.bet(B.owe, A)
198 # PRE-FLOP ACTION - Player B is small blind, first to act
203 pot = betting_round(play_order,pot,min_raise,flop,turn,river)
204 if checkFOLD(A,B): return hand_judgement(A,B,board,pot)
205 if not A.cash or not B.cash: return hand_judgement(A,B,board,pot)
207 # FLOP ACTION - Player A is now first to act
209 GP( "ACTION FLOP ", pp_hand(flop) )
213 pot = betting_round(play_order,pot,min_raise,flop,turn,river)
214 if checkFOLD(A,B): return hand_judgement(A,B,board,pot)
215 if not A.cash or not B.cash: return hand_judgement(A,B,board,pot)
219 GP( "ACTION TURN ", pp_hand(turn) )
222 pot = betting_round(play_order,pot,min_raise,flop,turn,river)
223 if checkFOLD(A,B): return hand_judgement(A,B,board,pot)
224 if not A.cash or not B.cash: return hand_judgement(A,B,board,pot)
228 GP( "ACTION RIVER ", pp_hand(river))
231 pot = betting_round(play_order,pot,min_raise,flop,turn,river)
232 if checkFOLD(A,B): return hand_judgement(A,B,board,pot)
235 return hand_judgement(A,B,board,pot)