Skip to content

Instantly share code, notes, and snippets.

@Eugeny
Created April 6, 2010 15:20
Show Gist options
  • Save Eugeny/357707 to your computer and use it in GitHub Desktop.
Save Eugeny/357707 to your computer and use it in GitHub Desktop.
import gtk
class Player(object):
stack = 0
bets = 0
name = ''
room = None
fold = False
all_in = False
done = False
def __init__(self, n, r):
self.name = n
self.room = r
self.fold = False
self.all_in = False
def bet(self, amount, rs=True):
self.bets += amount
if rs:
for p in self.room.players:
p.done = False
self.done = True
def call(self):
self.bet(self.room.get_max_bet()-self.bets, False)
self.done = True
def do_all_in(self):
self.all_in = True
for p in self.room.players:
p.done = False
self.done = True
class Room(object):
players = None
big_blind = 0
first_pl = 0
turn = 0
cur_round = 0
BIG_BLIND = 100
def __init__(self):
self.players = []
def add_player(self, p):
self.players.append(p)
def win(self, n):
print "Player", self.players[n].name, "wins the pot"
s = 0
for p in self.players:
if p.all_in:
p.bets = self.lowest_stack()
for p in self.players:
s += p.bets
p.stack -= p.bets
p.bets = 0
self.players[n].stack += s
def next_round(self):
if len(self.players)==0: return
self.big_blind += 1
self.big_blind %= len(self.players)
sm_blind = self.big_blind - 1
sm_blind %= len(self.players)
self.first_pl = self.big_blind + 1
self.turn = self.first_pl-1
self.players[self.big_blind].bet(self.BIG_BLIND, False)
self.players[sm_blind].bet(self.BIG_BLIND/2, False)
for p in self.players:
p.fold = False
p.all_in = False
p.done = False
self.turn = self.big_blind + 1
self.turn %= len(self.players)
self.cur_round = 0
def next_turn(self):
self.turn += 1
self.turn %= len(self.players)
rdy = True
for p in self.players:
if not p.fold:
rdy &= p.done
print self.all_ready(), rdy
if self.all_ready() and rdy: #(self.first_pl+len(self.players))%len(self.players) == self.turn:
self.cur_round += 1
for p in self.players:
p.done = False
if self.cur_round==1:
print "\n!!! Deal three cards"
elif self.cur_round < 4:
print "\n!!! Deal one card"
else:
print "\n!!! Dealing is finished. Showtime!"
self.first_pl = self.big_blind + len(self.players) - 1
self.first_pl %= len(self.players)
self.turn = self.first_pl
self.turn %= len(self.players)
self.all_ready()
c = 0
for p in self.players:
if p.fold:
c += 1
if c == len(self.players):
for p in self.players:
if p.fold:
self.win(self.players.index(p))
return
while self.players[self.turn].fold:
self.turn += 1
self.turn %= len(self.players)
def lowest_stack(self):
m = 1024*1024*1024
for p in self.players:
if p.all_in:
m = min(m, p.stack)
return m
def all_ready(self):
for p in self.players:
if p.all_in:
p.bets = self.lowest_stack()
m = -1
for p in self.players:
if not p.fold:
if m == -1:
m = p.bets
else:
if p.bets != m:
return False
return True
def get_pot(self):
s = 0
for p in self.players:
s += p.bets
return s
def get_max_bet(self):
ai = True
for p in self.players:
ai &= p.all_in or p.fold
if ai:
return self.lowest_stack()
m = 0
for p in self.players:
if p.bets > m: m = p.bets
return m
def add_stacks(self, a):
for p in self.players:
p.stack += a
#------------
w = gtk.Window()
room = Room()
def info(r):
print ""
for i in range(1,len(r.players)+1):
print i,
if room.big_blind==i-1:
print "BIG ",
if room.big_blind==i:
print "SM ",
if room.big_blind==0 and i==len(room.players):
print "SM ",
print "\t",
print ""
for p in r.players:
s = ''
if not p.done: s = '*'
if r.players.index(p) == room.turn:
print s + '[' + p.name + ']', "\t",
else:
print s + p.name, "\t",
print ""
for p in r.players:
print p.stack, "\t",
print ""
for p in r.players:
print p.bets, "\t",
print ""
for p in r.players:
if p.fold:
print "FOLD\t",
elif p.all_in:
print "ALL-IN\t",
else:
print "\t",
print ""
pot = room.get_pot()
print pot, "in the pot at round #", (room.cur_round+1)
print ""
room.next_round()
while True:
info(room)
print '> ',
s = raw_input()
a = filter(lambda x: x!='', s.split(' '))
for i in range(1,100):
print ""
if len(a)==0: continue
if a[0]=='a':
room.add_player(Player(a[1], room))
elif a[0]=='b':
if len(a)==3:
room.players[int(a[1])-1].bet(int(a[2]))
else:
room.players[room.turn].bet(int(a[1]))
room.next_turn()
elif a[0]=='w':
room.win(int(a[1])-1)
room.next_round()
elif a[0]=='s':
room.players[int(a[1])-1].stack += int(a[2])
elif a[0]=='n':
room.players[room.turn].done = True
room.next_turn()
elif a[0]=='c':
room.players[room.turn].call()
room.next_turn()
elif a[0]=='f':
room.players[room.turn].fold = True
room.next_turn()
elif a[0]=='ai':
room.players[room.turn].do_all_in()
room.next_turn()
elif a[0]=='ss':
room.add_stacks(int(a[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment