Add docstrings and doctests to table.py + Python cleanups.
[poker.git] / GAMEPLAY.py
diff --git a/GAMEPLAY.py b/GAMEPLAY.py
deleted file mode 100644 (file)
index 101f249..0000000
+++ /dev/null
@@ -1,235 +0,0 @@
-from DECK import *
-GLOBAL_TXT = []
-
-# ***************************************
-from os import system, kill
-from popen2 import popen2,popen3
-class player:
-    def __init__(self, name='', brain=''):
-        self.cash  = 0
-        self.owe   = 0
-        self.option= True
-        self.hole  = []
-        self.IN, self.OUT = 0,0
-        self.brain = brain.strip()
-        self.name  = name+' '
-        self.FOLD  = False
-        self.score = 0
-        self.GP_BUFFER = []
-        
-        if self.brain:
-            self.OUT, self.IN = popen2("./"+self.brain)
-
-    def die(self): 
-        self.IN.write("END\n"); self.IN.flush()
-        system("pkill -9 "+self.name)
-
-    def bet(self, wager, other_player):
-        wager = int(wager)
-        if wager > self.cash: wager  = self.cash
-        if wager < 0:         wager  = 0
-        if wager-self.owe > other_player.cash: 
-            wager = other_player.cash
-        self.cash -= wager
-        self.owe  -= wager
-        self.owe   = self.owe if self.owe < 0 else 0
-        GP("ACTION Player ", self.name, " bets: ", wager)
-        return wager
-    
-    def record_info(self, pot, min_raise, flop, turn, river, other_player):
-        global GLOBAL_TXT
-        self.IN.write("INFO NAME " + str(self.name) + '\n'); self.IN.flush()
-        self.IN.write("INFO ONAME " + str(other_player.name) + '\n'); self.IN.flush()
-        self.IN.write("INFO STACK " + str(self.cash) + '\n'); self.IN.flush()
-        self.IN.write("INFO OSTACK " + str(other_player.cash) + '\n'); self.IN.flush()
-        self.IN.write("INFO POT " + str(pot) + '\n'); self.IN.flush()
-        self.IN.write("INFO MINRAISE " + str(min_raise)+'\n'); self.IN.flush()
-        self.IN.write("INFO OWE " + str(self.owe)  + '\n'); self.IN.flush()
-        self.IN.write("INFO HOLE " + pp_hand(self.hole) + '\n'); self.IN.flush()
-        self.IN.write("INFO FLOP " + pp_hand(flop) + '\n'); self.IN.flush()
-        self.IN.write("INFO TURN " + pp_hand(turn) + '\n'); self.IN.flush()
-        self.IN.write("INFO RIVER " + pp_hand(river) + '\n'); self.IN.flush()
-        self.GP_FLUSH(GLOBAL_TXT)
-
-    def GP_FLUSH(self, TXT):
-        for g in TXT:
-            if g not in self.GP_BUFFER:
-                self.GP_BUFFER.append(g)
-                self.IN.write(g + "\n")
-                self.IN.flush()
-
-    def human_play(self, pot, min_raise, flop, turn, river, other_player):
-        if GLOBAL_TXT: print GLOBAL_TXT[-1]
-        print "[", pp_hand(self.hole), "] Board: ",
-        print "[", pp_hand(flop), pp_hand(turn), pp_hand(river), "]"
-        print "POT: ", pot, " OWE: ", self.owe, " MIN_RAISE: ", min_raise,
-        print "   |   ", "STACK: ", self.cash, "Opp. STACK: ", other_player.cash
-        return raw_input("What do you want to bet, " + self.name + ": ")
-
-    def decide(self, pot, min_raise, flop, turn, river, other_player,endgame=False):
-
-        in_bet = ''
-
-        if not self.brain:
-            in_bet = self.human_play(pot,min_raise, flop, turn, river, other_player)
-
-        else:
-            self.record_info(pot,min_raise,flop,turn,river,other_player)
-            self.IN.write("MOVE \n"); self.IN.flush()
-            in_bet = self.OUT.readline().strip()
-            
-        if endgame: return False
-
-        if in_bet.isalpha(): 
-            in_bet = in_bet.upper()
-            if   in_bet == 'A': in_bet = self.cash
-            elif in_bet == 'C': in_bet = self.owe
-            else              : self.FOLD = True
-        else:
-            try   : in_bet = int(in_bet)
-            except: self.FOLD = True
-
-        if self.FOLD: return False
-
-        if in_bet >= self.cash or in_bet == self.owe or in_bet >= min_raise:
-            out_bet  = self.bet(in_bet, other_player)
-            other_player.owe += out_bet
-            return out_bet
-
-        # Bets between whats owed and min_raise are considered a call
-        if in_bet >= self.owe and in_bet < min_raise:
-            out_bet  = self.bet(self.owe, other_player)
-            other_player.owe += out_bet
-            return out_bet
-
-        # Illegal bets will be counted as folds!
-        GP("ACTION Player ", self.name, "bets ", str(in_bet), " illegally and FOLDS. Valid bets are ", str(self.owe), " or anything >= ", min_raise)
-        self.FOLD = True
-        return False
-
-
-def GP(*S): GLOBAL_TXT.append(''.join(map(str,S)))
-def returnGP(): return GLOBAL_TXT
-     
-def hand_judgement(A,B, board, pot):
-    if    A.FOLD: 
-        GP("ACTION Player ", A.name, " folds" )
-        GP("ACTION Player ", B.name, " wins: ", pot)
-        B.cash += pot
-        return GLOBAL_TXT
-
-    elif  B.FOLD:
-        GP("ACTION Player ", B.name, "folds")
-        GP("ACTION Player ", A.name, "wins: ", pot)
-        A.cash += pot
-        return GLOBAL_TXT
-    S = HOLDEM_score(A, B, board)
-
-    if   S == 1: 
-        GP("ACTION Player ", A.name, "wins: ", pot)
-        A.cash += pot
-        return GLOBAL_TXT
-
-    elif S ==-1: 
-        GP( "ACTION Player ", B.name, "wins: ", pot)
-        B.cash += pot
-        return GLOBAL_TXT
-
-    elif S == 0:
-        GP("ACTION Split pot ")
-        split_pot, carry = pot/2, pot%2
-        A.cash += split_pot + carry
-        B.cash += split_pot
-        return GLOBAL_TXT
-
-def checkFOLD(A,B):
-    if   A.FOLD or B.FOLD: return True
-    return False
-
-def betting_round(P,pot,min_raise,flop,turn,river):
-    for player in P: 
-        player.option = True
-
-    while((P[0].owe>0 or P[1].owe>0) or (P[0].option or P[1].option) and (P[0].cash)):
-        action  = P[0].decide(pot, min_raise, flop,turn,river, P[-1])
-        P[0].option = False
-
-        if action >= min_raise: min_raise = 2*action
-        if checkFOLD(P[0],P[1]): return pot
-
-        pot += action
-        P = [P[-1]] + P[:-1] # Cycle the player order
-    return pot
-
-
-def gameplay(DECK, A, B, smallB, ):
-    global GLOBAL_TXT
-
-    # Shuffle the deck and deal two cards to each player
-    shuffle(DECK)
-
-    A.hole = [DECK[n] for n in xrange(2)  ]
-    B.hole = [DECK[n] for n in xrange(2,4)]
-    board  = [DECK[n] for n in xrange(4,9)]
-
-    flop, turn, river = '', '', ''
-    GLOBAL_TXT        = []
-
-    pot   = 0
-    A.owe = 2*smallB
-    B.owe = smallB
-    A.FOLD, B.FOLD = False, False
-
-    # Handle the case if a player can't post the blinds completely, ALL_IN
-    if    B.owe > B.cash:
-        pot  += B.bet(B.owe, A)
-        pot  += A.bet(pot,   B)
-        return hand_judgement(A,B,board,pot)
-    elif  A.owe > A.cash:
-        pot += A.bet(A.owe,  B)
-        pot += B.bet(pot,    A)
-        return hand_judgement(A,B,board,pot)
-
-    # Both players can post the blinds
-    pot += A.bet(A.owe, B)
-    pot += B.bet(B.owe, A)
-
-    # PRE-FLOP ACTION - Player B is small blind, first to act
-    B.owe        += smallB
-    A.owe        -= smallB
-    min_raise     = smallB*4
-    play_order    = [B,A]
-    pot = betting_round(play_order,pot,min_raise,flop,turn,river)
-    if checkFOLD(A,B): return hand_judgement(A,B,board,pot)
-    if not A.cash or not B.cash: return hand_judgement(A,B,board,pot)
-
-    # FLOP ACTION - Player A is now first to act
-    flop          = board[:3]
-    GP( "ACTION FLOP ", pp_hand(flop) )
-    A.owe, B.owe  = 0, 0
-    min_raise     = smallB*2
-    play_order    = [A,B]
-    pot = betting_round(play_order,pot,min_raise,flop,turn,river)
-    if checkFOLD(A,B): return hand_judgement(A,B,board,pot)
-    if not A.cash or not B.cash: return hand_judgement(A,B,board,pot)
-
-    # TURN ACTION
-    turn          = board[3:4]
-    GP( "ACTION TURN ", pp_hand(turn) )
-    A.owe, B.owe  = 0, 0
-    play_order    = [A,B]
-    pot = betting_round(play_order,pot,min_raise,flop,turn,river)
-    if checkFOLD(A,B): return hand_judgement(A,B,board,pot)
-    if not A.cash or not B.cash: return hand_judgement(A,B,board,pot)
-
-    # RIVER ACTION
-    river         = board[4:5]
-    GP( "ACTION RIVER ", pp_hand(river))
-    A.owe, B.owe  = 0, 0
-    play_order    = [A,B]
-    pot = betting_round(play_order,pot,min_raise,flop,turn,river)
-    if checkFOLD(A,B): return hand_judgement(A,B,board,pot)
-    
-    # SHOWDOWN!
-    return hand_judgement(A,B,board,pot)