Code review
C Smith
illusiontechniques at gmail.com
Tue Nov 4 10:28:33 EST 2014
I was wondering if I could get some feedback on the biggest thing I
have done as an amateur Python coder. The sidepots algorithm isn't
correct yet, but I haven't worked on it in a while and thought I would
get some advice before diving back in.
import random, os
##################################################################
class Table(object):
def __init__(self,bigblind=20,PLAYERNUM=0,pot=0,PLAYERORDER=None,
hand_done=0,\
left_to_act=None,cost_to_play=0,in_hand=None,last_raise=0,cards_in_play=None,round='preflop'):
if cards_in_play is None:
cards_in_play = []
if in_hand is None:
in_hand = []
if PLAYERORDER is None:
PLAYERORDER = []
if left_to_act is None:
left_to_act = []
self.hand_done = hand_done
self.round = round
self.cards_in_play = cards_in_play
self.last_raise = last_raise
self.in_hand = in_hand
self.cost_to_play = cost_to_play
self.left_to_act = left_to_act
self.PLAYERORDER = PLAYERORDER
self.bigblind = bigblind
self.PLAYERNUM = PLAYERNUM
self.pot = pot
##################################################################
def fillseats(self,listofplayers, playas):
seating = [x for x in range(1,len(listofplayers)+1)]
random.shuffle(seating)
for y in listofplayers:
playas[y].seatnumber = seating.pop()
for x in listofplayers:
newx = str(playas[x].seatnumber)+x
self.PLAYERORDER.append(newx)
self.PLAYERORDER = sorted(self.PLAYERORDER)
neworder = []
for x in self.PLAYERORDER:
neworder.append(x[1:])
self.PLAYERORDER = neworder
self.PLAYERNUM = len(self.PLAYERORDER)
##########################################################
def postblinds(self,playas,bigblind,deck):
# establish player.start_stack for sidepots
for player in self.PLAYERORDER:
playas[player].start_stack = playas[player].stacksize
# Heads up post blinds
if len(self.PLAYERORDER) == 2:
if bigblind >= playas[self.PLAYERORDER[1]].stacksize:
if bigblind == playas[self.PLAYERORDER[1]].stacksize:
playas[self.PLAYERORDER[1]].stacksize = 0
self.pot += bigblind
playas[self.PLAYERORDER[1]].all_in = 1
playas[self.PLAYERORDER[1]].in_front = bigblind
playas[self.PLAYERORDER[1]].put_in_pot = bigblind
else:
self.pot += playas[self.PLAYERORDER[1]].stacksize
playas[self.PLAYERORDER[1]].in_front =
playas[self.PLAYERORDER[1]].stacksize
playas[self.PLAYERORDER[1]].put_in_pot =
playas[self.PLAYERORDER[1]].stacksize
playas[self.PLAYERORDER[1]].stacksize = 0
playas[self.PLAYERORDER[1]].all_in = 1
if (bigblind/2) >= playas[self.PLAYERORDER[0]].stacksize:
if bigblind == playas[self.PLAYERORDER[0]].stacksize:
playas[self.PLAYERORDER[0]].in_front = bigblind/2
playas[self.PLAYERORDER[0]].put_in_pot = bigblind/2
playas[self.PLAYERORDER[0]].stacksize = 0
self.pot += bigblind/2
playas[self.PLAYERORDER[0]].all_in = 1
else:
playas[self.PLAYERORDER[0]].put_in_pot =
playas[self.PLAYERORDER[0]].stacksize
playas[self.PLAYERORDER[0]].in_front =
playas[self.PLAYERORDER[0]].stacksize
playas[self.PLAYERORDER[0]].stacksize = 0
playas[self.PLAYERORDER[0]].all_in = 1
if playas[self.PLAYERORDER[0]].all_in == 0:
playas[self.PLAYERORDER[0]].stacksize -= bigblind/2
playas[self.PLAYERORDER[0]].in_front = bigblind/2
playas[self.PLAYERORDER[0]].put_in_pot = bigblind/2
self.pot += bigblind/2
if playas[self.PLAYERORDER[1]].all_in == 0:
playas[self.PLAYERORDER[1]].stacksize -= bigblind
playas[self.PLAYERORDER[1]].in_front = bigblind
playas[self.PLAYERORDER[1]].put_in_pot = bigblind
self.pot += bigblind
self.left_to_act = self.PLAYERORDER[:]
self.cost_to_play = bigblind
self.last_raise = bigblind
else: # post blinds for more that 2 players
# player has not enough or just enough for big blind
if playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].stacksize
<= bigblind:
if
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].stacksize <
bigblind:
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].put_in_pot =
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].stacksize
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].in_front =
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].stacksize
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].all_in = 1
self.pot +=
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].stacksize
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].stacksize = 0
elif
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].stacksize ==
bigblind:
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].in_front = bigblind
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].put_in_pot =
bigblind
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].all_in = 1
self.pot += bigblind
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].stacksize = 0
else:
# Normal post bigblind
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].stacksize -=
bigblind
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].in_front = bigblind
playas[self.PLAYERORDER[2%len(self.PLAYERORDER)]].put_in_pot =
bigblind
self.pot += bigblind
# player has not enough or just enough for small blind
if playas[self.PLAYERORDER[1]].stacksize <= bigblind/2:
if playas[self.PLAYERORDER[1]].stacksize < bigblind/2:
playas[self.PLAYERORDER[1]].all_in = 1
self.pot += playas[self.PLAYERORDER[1]].stacksize
playas[self.PLAYERORDER[1]].in_front =
playas[self.PLAYERORDER[1]].stacksize
playas[self.PLAYERORDER[1]].put_in_pot =
playas[self.PLAYERORDER[1]].stacksize
playas[self.PLAYERORDER[1]].stacksize = 0
elif playas[self.PLAYERORDER[1]].stacksize == bigblind/2:
playas[self.PLAYERORDER[1]].all_in = 1
self.pot += bigblind/2
playas[self.PLAYERORDER[1]].in_front = bigblind/2
playas[self.PLAYERORDER[1]].put_in_pot = bigblind/2
playas[self.PLAYERORDER[1]].stacksize = 0
else: # normal post small blind
playas[self.PLAYERORDER[1]].stacksize -= bigblind/2
playas[self.PLAYERORDER[1]].in_front = bigblind/2
playas[self.PLAYERORDER[1]].put_in_pot = bigblind/2
self.pot += bigblind/2
self.left_to_act =
self.PLAYERORDER[3%len(self.PLAYERORDER):]+self.PLAYERORDER[:3%len(self.PLAYERORDER)]
self.cost_to_play = bigblind
self.last_raise = bigblind
for x in self.PLAYERORDER:
playas[x].drawcard(deck.draw())
for x in self.PLAYERORDER:
playas[x].drawcard(deck.draw())
for x in self.PLAYERORDER:
print x+" stacksize is "+str(playas[x].stacksize)
print x+" is at seat number "+str(playas[x].seatnumber)
print raw_input('to begin, hit any key')
self.get_action(playas, bigblind, deck)
##################################################################
def showdown(self,playas,bigblind,deck):
print "in_hand equals %s" % self.in_hand
handtobeat = 0
for player in self.in_hand:
final_hand = self.cards_in_play + playas[player].hand
ranks = [x[0] for x in final_hand]
suits = [x[1] for x in final_hand]
#ints with Ace as 14 and 2 for straights
straightranks = []
#ints with Ace as 14 for pairs
pairranks = []
for rank in ranks:
if rank == 'T':
straightranks.append(10)
elif rank == 'J':
straightranks.append(11)
elif rank == 'Q':
straightranks.append(12)
elif rank == 'K':
straightranks.append(13)
elif rank == 'A':
straightranks.append(1)
straightranks.append(14)
else:
straightranks.append(int(rank))
pairranks = [x for x in straightranks if x != 1]
straightranks = sorted(list(set(straightranks)),reverse=True)
########### USE straightranks FOR STRAIGHTS
if straight_flush_finder(final_hand)[0] == True:
playas[player].hand_rank =
(9,straight_flush_finder(final_hand))
elif four_of_a_kind_finder(pairranks)[0] == True:
playas[player].hand_rank = (8,four_of_a_kind_finder(pairranks))
elif fullhouse_finder(pairranks)[0] == True:
playas[player].hand_rank = (7,fullhouse_finder(pairranks))
elif flush_finder(final_hand)[0] == True:
playas[player].hand_rank = (6,flush_finder(final_hand))
elif straight_finder(straightranks)[0] == True:
playas[player].hand_rank = (5,straight_finder(straightranks))
elif three_of_a_kind_finder(pairranks)[0] == True:
playas[player].hand_rank = (4,three_of_a_kind_finder(pairranks))
elif two_pair_finder(pairranks)[0] == True:
playas[player].hand_rank = (3,two_pair_finder(pairranks))
elif one_pair_finder(pairranks)[0] == True:
playas[player].hand_rank = (2,one_pair_finder(pairranks))
else:
playas[player].hand_rank = (1,highcard_finder(pairranks))
# check if any player has a different amount in the
pot (sidepot necessary)
one_put_in_pot = playas[self.in_hand[0]].put_in_pot
for player in self.in_hand:
if playas[player].put_in_pot != one_put_in_pot:
bigstack = 0
bigplayer = ''
for player in self.in_hand:
if playas[player].put_in_pot > bigstack:
bigplayer = player
bigstack = playas[player].put_in_pot
in_hand_copy = self.in_hand[:]
in_hand_copy.remove(bigplayer)
lowstack = 999999999999999
lowman = ''
for player in in_hand_copy:
if playas[player].put_in_pot < lowstack:
lowman = player
lowstack = playas[player].put_in_pot
if playas[bigplayer].put_in_pot > playas[lowman].put_in_pot:
print 'bigplayer %s put in pot is %s' %
(bigplayer, playas[bigplayer].put_in_pot)
print 'lowman %s put in pot %s' % (lowman,
playas[lowman].put_in_pot)
for player in self.in_hand:
print '%s is in hand and put %s in pot' %
(player, playas[player].put_in_pot)
raw_input("Sidepot split necessary, hit enter to continue")
sidepot1 = 0
for player in self.PLAYERORDER:
sidepot1 +=
min(playas[player].put_in_pot,playas[lowman].put_in_pot)
sidepot1elig = self.in_hand[:]
self.pot -= sidepot1
sidepot1winners = []
handtobeat = 0
for player in sidepot1elig:
if playas[player].hand_rank[0] >= handtobeat:
handtobeat = playas[player].hand_rank[0]
sidepot1winners.append(player)
for player in sidepot1winners:
if playas[player].hand_rank[0] < handtobeat:
sidepot1winners.remove(player)
sidepot1winners = tie_breaker(sidepot1winners,playas)
for player in sidepot1winners[0]:
print '%s you win %s chips' %
(player,sidepot1/len(sidepot1winners[0]))
playas[player].stacksize +=
(sidepot1/len(sidepot1winners[0]))
showmuck = 'x'
while showmuck not in ['s','m']:
showmuck = raw_input('%s show or muck
hand? (hit s OR m) ' % player)
if showmuck == 's':
playas[player].showhand()
print playas[player].hand_rank
if showmuck == 'm':
playas[player].muckhand()
anothercopy = self.in_hand[:]
for player in anothercopy:
if playas[player].start_stack == playas[lowman].start_stack:
self.in_hand.remove(player)
break
############## rest of showdown
handtobeat = 0
winningplayers = []
for player in self.in_hand:
if playas[player].hand_rank[0] >= handtobeat:
handtobeat = playas[player].hand_rank[0]
winningplayers.append(player)
winnerscopy = winningplayers[:]
for player in winnerscopy:
if playas[player].hand_rank[0] < handtobeat:
winningplayers.remove(player)
# tie_breaker changes winningplayers into nested list
winningplayers = tie_breaker(winningplayers,playas)
for winnerwinner in winningplayers[0]:
self.hand_done = 1
print '%s you win %s chips' %
(winnerwinner,self.pot/len(winningplayers[0]))
playas[winnerwinner].stacksize += (self.pot/len(winningplayers[0]))
showmuck = 'x'
while showmuck not in ['s','m']:
showmuck = raw_input('%s show or muck hand? (hit s OR
m) ' % winnerwinner)
if showmuck == 's':
playas[winnerwinner].showhand()
print playas[winnerwinner].hand_rank
if showmuck == 'm':
playas[winnerwinner].muckhand()
self.end_hand(playas,bigblind,deck)
#################################################################################
def turn_one(self, deck):
self.cards_in_play.append(deck.draw())
print self.cards_in_play[-1] + ' drawn'
raw_input('Hit Enter')
def burn_one(self, deck):
deck.burn()
print 'Card Burnt'
raw_input('Hit Enter')
##################################
def end_hand(self, playas, bigblind, deck):
print 'hand done'
playercopy = self.PLAYERORDER[:]
for x in playercopy:
print '%s stacksize is %s' % (x, playas[x].stacksize)
if playas[x].stacksize == 0:
print '%s you are out of the game.' % x
self.PLAYERORDER.remove(x)
if len(self.PLAYERORDER) == 1:
print 'tournament over, %s wins' % self.PLAYERORDER[0]
exit()
raw_input('Hit enter for the next hand')
for player in self.PLAYERORDER:
playas[player].put_in_pot = 0
playas[player].all_in = 0
buttonmover = self.PLAYERORDER[0]
self.PLAYERORDER = self.PLAYERORDER[1:]
self.PLAYERORDER.append(buttonmover)
self.left_to_act = []
self.in_hand = []
self.hand_done = 0
for x in self.PLAYERORDER:
playas[x].in_front = 0
playas[x].hand = []
self.cards_in_play = []
self.pot = 0
self.round = 'preflop'
self.last_raise = 0
self.cost_to_play = 0
deck.renew()
self.postblinds(playas, bigblind, deck)
##################################################################
def get_action(self,playas,bigblind,deck):
###################### test if folded around to a player (hand done
without showdown) ##################
if len(self.left_to_act) == 0 and len(self.in_hand) == 1:
self.hand_done = 1
print '%s you win %s chips' % (self.in_hand[0],self.pot)
playas[self.in_hand[0]].stacksize += self.pot
showmuck = 'x'
while showmuck not in ['s','m']:
showmuck = raw_input('%s show or muck hand? (hit s OR
m) ' % self.in_hand[0])
if showmuck == 's':
playas[self.in_hand[0]].showhand()
if showmuck == 'm':
playas[self.in_hand[0]].muckhand()
for player in self.in_hand:
print playas[player].put_in_pot
raw_input('Hit enter')
self.end_hand(playas,bigblind,deck)
###########################################################################
################# test if river (last) round is done (SHOWDOWN) ################
if len(self.left_to_act) == 0 and self.round == 'river':
self.hand_done = 1
#return excess chips
bigstack = 0
bigplayer = ''
for player in self.in_hand:
if playas[player].put_in_pot > bigstack:
bigplayer = player
bigstack = playas[player].put_in_pot
in_hand_copy = self.in_hand[:]
in_hand_copy.remove(bigplayer)
nextbigstack = 0
nextbigplayer = ''
for player in in_hand_copy:
if playas[player].put_in_pot > nextbigstack:
nextbigplayer = player
nextbigstack = playas[player].put_in_pot
if playas[bigplayer].put_in_pot > playas[nextbigplayer].put_in_pot:
## ## nextbigstack could have folded
difference = playas[bigplayer].put_in_pot -
playas[nextbigplayer].put_in_pot
playas[bigplayer].stacksize += difference
playas[bigplayer].put_in_pot = playas[nextbigplayer].put_in_pot
self.pot -= difference
self.showdown(playas,bigblind,deck)
print self.cards_in_play
print 'Hand done'
raw_input('hit enter')
self.end_hand(playas,bigblind,deck)
#####################################################################################
################# test if turn (2nd to last round) is done
##################################
if len(self.left_to_act) == 0 and self.round == 'turn':
print self.cards_in_play
print 'Turn done'
raw_input('hit enter')
self.round = 'river'
for x in self.PLAYERORDER:
playas[x].in_front = 0
self.last_raise = bigblind
self.cost_to_play = 0
neworder = self.PLAYERORDER[1:]+self.PLAYERORDER[0:1]
for x in neworder:
if x in self.in_hand:
self.left_to_act.append(x)
self.in_hand = []
self.burn_one(deck)
self.turn_one(deck)
for player in self.in_hand:
playas[player].in_front = 0
self.get_action(playas,bigblind,deck)
#####################################################################################
################# test if flop is done
###################################################
if len(self.left_to_act) == 0 and self.round == 'postflop':
print self.cards_in_play
print 'Postflop done'
raw_input('hit enter')
self.round = 'turn'
for x in self.PLAYERORDER:
playas[x].in_front = 0
self.last_raise = bigblind
self.cost_to_play = 0
neworder = self.PLAYERORDER[1:]+self.PLAYERORDER[0:1]
for x in neworder:
if x in self.in_hand:
self.left_to_act.append(x)
self.in_hand = []
self.burn_one(deck)
self.turn_one(deck)
for player in self.in_hand:
playas[player].in_front = 0
self.get_action(playas,bigblind,deck)
#####################################################################################
################# test if preflop play is done
##################################
if len(self.left_to_act) == 0 and self.round == 'preflop':
for x in self.PLAYERORDER:
playas[x].in_front = 0
print ' stacksize is %s' % playas[x].stacksize
print 'in hand ',self.in_hand
print 'player order ',self.PLAYERORDER
print 'flop done'
print 'pot is '+str(self.pot)
self.last_raise = bigblind
self.cost_to_play = 0
neworder = self.PLAYERORDER[1:]+self.PLAYERORDER[0:1]
for x in neworder:
if x in self.in_hand:
self.left_to_act.append(x)
self.in_hand = []
self.burn_one(deck)
self.turn_one(deck)
self.turn_one(deck)
self.turn_one(deck)
print self.cards_in_play
self.round = 'postflop'
raw_input("hit enter")
self.get_action(playas,bigblind,deck)
###############
turnscreen(self.left_to_act)
###############
# Catch a player all-in and pass their turn
if playas[self.left_to_act[0]].all_in == 1:
raw_input("%s you are all in, hit enter to continue" %
self.left_to_act[0])
if self.left_to_act[0] not in self.in_hand:
self.in_hand.append(self.left_to_act[0])
del self.left_to_act[0]
self.get_action(playas,bigblind,deck)
############# Display for each action prompt
for x in self.PLAYERORDER:
print x +'stacksize is '+ str(playas[x].stacksize)
print self.left_to_act[0] + ' your hand is
'+playas[self.left_to_act[0]].hand[0]+'
'+playas[self.left_to_act[0]].hand[1]
print 'The last raise is '+str(self.last_raise)
print 'The pot is '+str(self.pot)
print 'The cost to play is
'+str(self.cost_to_play-playas[self.left_to_act[0]].in_front)
print 'You have %s in the pot' % playas[self.left_to_act[0]].put_in_pot
print 'You started the hand with %s chips' %
playas[self.left_to_act[0]].start_stack
print 'This round, you have put %s chips in the pot' %
playas[self.left_to_act[0]].in_front
showcards = (', ').join(x for x in self.cards_in_play)
print 'The cards in play are '+showcards
# Check if a player can Check their turn
if playas[self.left_to_act[0]].in_front >= self.cost_to_play:
current_action = 'q'
current_action = raw_input("%s will you check, bet, or
fold? (type c OR b OR f) " % self.left_to_act[0])
#Check your turn, if infront == cost
if current_action == 'c':
if self.left_to_act[0] in self.in_hand:
del self.in_hand[self.left_to_act[0]]
self.in_hand.append(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
# Bet , after check option
elif current_action == 'b':
currentraise = 3
while currentraise % 10 != 0:
currentraise = int(raw_input('%s what will you
bet? (increments of 10) ' % self.left_to_act[0]))
while currentraise < 2*self.last_raise:
currentraise = int(raw_input('%s what will you
bet? (increments of 10) ' % self.left_to_act[0]))
# Bet with exactly enough chips, after check option
if
self.cost_to_play-playas[self.left_to_act[0]].in_front + currentraise
== playas[self.left_to_act[0]].stacksize:
raw_input("%s you are all in" % self.left_to_act[0])
playas[self.left_to_act[0]].all_in = 1
self.last_raise = playas[self.left_to_act[0]].stacksize
self.pot +=
(self.cost_to_play-playas[self.left_to_act[0]].in_front+playas[self.left_to_act[0]].stacksize)
self.cost_to_play += playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].in_front +=
playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].put_in_pot +=
playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].stacksize = 0
if self.left_to_act[0] in self.in_hand:
self.in_hand.remove(self.left_to_act[0])
for x in self.in_hand:
if x not in self.left_to_act:
self.left_to_act.append(x)
self.in_hand.append(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
### Bet, not enough chips, after check option
elif
self.cost_to_play-playas[self.left_to_act[0]].in_front + currentraise
> playas[self.left_to_act[0]].stacksize:
raw_input("Not enough to raise that amount, %s put
all in, hit enter" % self.left_to_act[0])
playas[self.left_to_act[0]].all_in = 1
self.pot += playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].put_in_pot +=
playas[self.left_to_act[0]].stacksize
currentraise =
playas[self.left_to_act[0]].stacksize-self.cost_to_play-playas[self.left_to_act[0]].in_front
playas[self.left_to_act[0]].in_front +=
playas[self.left_to_act[0]].stacksize
self.cost_to_play += currentraise
self.last_raise = playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].stacksize = 0
if self.left_to_act[0] in self.in_hand:
self.in_hand.remove(self.left_to_act[0])
for x in self.in_hand:
if x not in self.left_to_act:
self.left_to_act.append(x)
self.in_hand.append(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
# Rest of Bet, more than enough chips after check option
self.pot += (currentraise +
self.cost_to_play-playas[self.left_to_act[0]].in_front)
playas[self.left_to_act[0]].put_in_pot +=
(currentraise +
self.cost_to_play-playas[self.left_to_act[0]].in_front)
self.last_raise = currentraise
self.cost_to_play += currentraise
playas[self.left_to_act[0]].stacksize -=
(self.cost_to_play - playas[self.left_to_act[0]].in_front)
playas[self.left_to_act[0]].in_front +=
(self.cost_to_play - playas[self.left_to_act[0]].in_front)
if self.left_to_act[0] in self.in_hand:
self.in_hand.remove(self.left_to_act[0])
for x in self.in_hand:
if x not in self.left_to_act:
self.left_to_act.append(x)
self.in_hand.append(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
# Fold, after check option. Hey, you can do it!
elif current_action == 'f':
if self.left_to_act[0] in self.in_hand:
self.in_hand.remove(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
# Prompt for action, no check option
current_action = raw_input("%s will you call, raise, or fold?
(type c OR r OR f) " % self.left_to_act[0])
# Call
if current_action == 'c':
# Not enough chips, or just enough chips to call
if self.cost_to_play-playas[self.left_to_act[0]].in_front
>= playas[self.left_to_act[0]].stacksize:
playas[self.left_to_act[0]].all_in = 1
self.pot += playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].put_in_pot +=
playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].in_front +=
playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].stacksize = 0
if self.left_to_act[0] in self.in_hand:
self.in_hand.remove(self.left_to_act[0])
self.in_hand.append(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
# Call, with more than enough chips
else:
playas[self.left_to_act[0]].stacksize -=
(self.cost_to_play-playas[self.left_to_act[0]].in_front)
self.pot +=
(self.cost_to_play-playas[self.left_to_act[0]].in_front)
playas[self.left_to_act[0]].put_in_pot +=
(self.cost_to_play-playas[self.left_to_act[0]].in_front)
playas[self.left_to_act[0]].in_front +=
(self.cost_to_play-playas[self.left_to_act[0]].in_front)
if self.left_to_act[0] in self.in_hand:
self.in_hand.remove(self.left_to_act[0])
self.in_hand.append(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
# Raise, no check option
elif current_action == 'r':
currentraise = 3
while currentraise % 10 != 0:
currentraise = int(raw_input('%s what will you raise?
(increments of 10) ' % self.left_to_act[0]))
while currentraise < 2*self.last_raise:
currentraise = int(raw_input('%s what will you raise?
(increments of 10) ' % self.left_to_act[0]))
# Raise with exactly enough chips
if self.cost_to_play-playas[self.left_to_act[0]].in_front
+ currentraise == playas[self.left_to_act[0]].stacksize:
raw_input("%s you are all in" % self.left_to_act[0])
self.pot += playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].put_in_pot +=
playas[self.left_to_act[0]].stacksize
self.cost_to_play += currentraise
self.last_raise = currentraise
playas[self.left_to_act[0]].all_in = 1
playas[self.left_to_act[0]].in_front +=
playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].stacksize = 0
if self.left_to_act[0] in self.in_hand:
self.in_hand.remove(self.left_to_act[0])
for x in self.in_hand:
if x not in self.left_to_act:
self.left_to_act.append(x)
self.in_hand.append(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
### Raise not enough chips
elif
self.cost_to_play-playas[self.left_to_act[0]].in_front + currentraise
> playas[self.left_to_act[0]].stacksize:
raw_input("%s put all in, hit enter" % self.left_to_act[0])
playas[self.left_to_act[0]].all_in = 1
self.cost_to_play = playas[self.left_to_act[0]].stacksize
self.last_raise = playas[self.left_to_act[0]].stacksize
self.pot += playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].put_in_pot +=
playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].in_front +=
playas[self.left_to_act[0]].stacksize
playas[self.left_to_act[0]].stacksize = 0
if self.left_to_act[0] in self.in_hand:
self.in_hand.remove(self.left_to_act[0])
for x in self.in_hand:
if x not in self.left_to_act:
self.left_to_act.append(x)
self.in_hand.append(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
# Raise with enough chips
self.pot += (currentraise +
self.cost_to_play-playas[self.left_to_act[0]].in_front)
playas[self.left_to_act[0]].put_in_pot += (currentraise +
self.cost_to_play-playas[self.left_to_act[0]].in_front)
self.last_raise = currentraise
self.cost_to_play += currentraise
playas[self.left_to_act[0]].stacksize -=
(self.cost_to_play - playas[self.left_to_act[0]].in_front)
playas[self.left_to_act[0]].in_front += (self.cost_to_play
- playas[self.left_to_act[0]].in_front)
if self.left_to_act[0] in self.in_hand:
self.in_hand.remove(self.left_to_act[0])
for x in self.in_hand:
if x not in self.left_to_act:
self.left_to_act.append(x)
self.in_hand.append(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
# Fold, after call option
elif current_action == 'f':
if self.left_to_act[0] in self.in_hand:
self.in_hand.remove(self.left_to_act[0])
self.left_to_act.remove(self.left_to_act[0])
self.get_action(playas,bigblind,deck)
#########################################################################
class Deck(object):
def __init__(self,cards=None):
if cards is None:
cards = []
self.cards = cards
suits = ["S","C","D","H"]
ranks = ["2","3","4","5","6","7","8","9","T","J","Q","K","A"]
for suit in suits:
for rank in ranks:
self.cards.append(rank+suit)
random.shuffle(self.cards)
def draw(self):
return self.cards.pop()
def burn(self):
self.cards = self.cards[0:-1]
def renew(self):
suits = ["S","C","D","H"]
ranks = ["2","3","4","5","6","7","8","9","T","J","Q","K","A"]
self.cards = []
for suit in suits:
for rank in ranks:
self.cards.append(rank+suit)
random.shuffle(self.cards)
##################################################################
class Player(object):
playercount = 0
def __init__(self,stacksize=0,HUMAN=True,hand=None,seatnumber=0,\
in_front=0,hand_rank=0,all_in=0,start_stack=0, put_in_pot=0):
self.all_in = all_in
self.seatnumber = seatnumber
self.start_stack = start_stack
self.hand_rank = hand_rank
self.stacksize = stacksize
self.HUMAN = HUMAN
self.in_front = in_front
self.put_in_pot = put_in_pot
if hand == None:
hand = []
self.hand = hand
Player.playercount += 1
def drawcard(self,card):
self.hand.append(card)
def muckhand(self):
self.hand = []
print 'mucked'
def showhand(self):
print self.hand
##################################################################
def tie_breaker(winningplayers,playas):
ranky = playas[winningplayers[0]].hand_rank[0]
reallywinners = []
reallyreally = []
reallyreallyreally = []
thisisitiswear = []
okaylastone = []
high_int = 0
if ranky == 9 or ranky == 5:
for player in winningplayers:
if playas[player].hand_rank[1][1] > high_int:
reallywinners = []
high_int = playas[player].hand_rank[1][1]
if player not in reallywinners:
reallywinners.append(player)
if playas[player].hand_rank[1][1] == high_int:
if player not in reallywinners:
reallywinners.append(player)
elif ranky == 8 or ranky == 7:
for player in winningplayers:
if playas[player].hand_rank[1][1] > high_int:
reallywinners = []
high_int = playas[player].hand_rank[1][1]
if player not in reallywinners:
reallywinners.append(player)
if playas[player].hand_rank[1][1] == high_int:
if player not in reallywinners:
reallywinners.append(player)
if len(reallywinners) >= 2:
high_int = 0
for player in reallywinners:
if playas[player].hand_rank[1][2] > high_int:
reallyreally = []
high_int = playas[player].hand_rank[1][2]
if player not in reallyreally:
reallyreally.append(player)
if playas[player].hand_rank[1][2] == high_int:
if player not in reallyreally:
reallyreally.append(player)
elif ranky == 6:
for player in winningplayers:
if playas[player].hand_rank[1][1][0] > high_int:
reallywinners = []
high_int = playas[player].hand_rank[1][1][0]
if player not in reallywinners:
reallywinners.append(player)
if playas[player].hand_rank[1][1][0] == high_int:
if player not in reallywinners:
reallywinners.append(player)
elif ranky == 4:
for player in winningplayers:
if playas[player].hand_rank[1][1] > high_int:
reallywinners = []
high_int = playas[player].hand_rank[1][1]
if player not in reallywinners:
reallywinners.append(player)
if playas[player].hand_rank[1][1] == high_int:
if player not in reallywinners:
reallywinners.append(player)
if len(reallywinners) >= 2:
high_int = 0
for player in reallywinners:
if playas[player].hand_rank[1][2][0] > high_int:
reallyreally = []
high_int = playas[player].hand_rank[1][2][0]
if player not in reallyreally:
reallyreally.append(player)
if playas[player].hand_rank[1][2][1] == high_int:
if player not in reallyreally:
reallyreally.append(player)
elif ranky == 3:
for player in winningplayers:
if playas[player].hand_rank[1][1] > high_int:
reallywinners = []
high_int = playas[player].hand_rank[1][1]
if player not in reallywinners:
reallywinners.append(player)
if playas[player].hand_rank[1][1] == high_int:
if player not in reallywinners:
reallywinners.append(player)
if len(reallywinners) >= 2:
high_int = 0
for player in reallywinners:
if playas[player].hand_rank[1][2] > high_int:
reallyreally = []
high_int = playas[player].hand_rank[1][2]
if player not in reallyreally:
reallyreally.append(player)
if playas[player].hand_rank[1][2] == high_int:
if player not in reallyreally:
reallyreally.append(player)
if len(reallyreally) >= 2:
high_int = 0
for player in reallyreally:
if playas[player].hand_rank[1][3] > high_int:
reallyreallyreally = []
high_int = playas[player].hand_rank[1][3]
if player not in reallyreallyreally:
reallyreallyreally.append(player)
if playas[player].hand_rank[1][3] == high_int:
if player not in reallyreallyreally:
reallyreallyreally.append(player)
elif ranky == 2:
for player in winningplayers:
if playas[player].hand_rank[1][1] > high_int:
high_int = playas[player].hand_rank[1][1]
reallywinners = []
if player not in reallywinners:
reallywinners.append(player)
if playas[player].hand_rank[1][1] == high_int:
if player not in reallywinners:
reallywinners.append(player)
if len(reallywinners) >= 2:
high_int = 0
for player in reallywinners:
if playas[player].hand_rank[1][2][0] > high_int:
high_int = playas[player].hand_rank[1][2][0]
reallyreally = []
if player not in reallyreally:
reallyreally.append(player)
if playas[player].hand_rank[1][2][0] == high_int:
if player not in reallyreally:
reallyreally.append(player)
if len(reallyreally) >= 2:
high_int = 0
for player in reallyreally:
if playas[player].hand_rank[1][2][1] > high_int:
high_int = playas[player].hand_rank[1][2][1]
reallyreallyreally = []
if player not in reallyreallyreally:
reallyreallyreally.append(player)
if playas[player].hand_rank[1][2][1] == high_int:
if player not in reallyreallyreally:
reallyreallyreally.append(player)
if len(reallyreallyreally) >= 2:
high_int = 0
for player in reallyreallyreally:
if playas[player].hand_rank[1][2][2] > high_int:
high_int = playas[player].hand_rank[1][2][2]
thisisitiswear = []
if player not in thisisitiswear:
thisisitiswear.append(player)
if playas[player].hand_rank[1][2][2] == high_int:
if player not in thisisitiswear:
thisisitiswear.append(player)
elif ranky == 1:
for player in winningplayers:
if playas[player].hand_rank[1][0] > high_int:
high_int = playas[player].hand_rank[1][0]
reallywinners = []
if player not in reallywinners:
reallywinners.append(player)
if playas[player].hand_rank[1][0] == high_int:
if player not in reallywinners:
reallywinners.append(player)
if len(reallywinners) >= 2:
high_int = 0
for player in reallywinners:
if playas[player].hand_rank[1][1] > high_int:
high_int = playas[player].hand_rank[1][1]
reallyreally = []
if player not in reallyreally:
reallyreally.append(player)
if playas[player].hand_rank[1][1] == high_int:
if player not in reallyreally:
reallyreally.append(player)
if len(reallyreally) >= 2:
high_int = 0
for player in reallyreally:
if playas[player].hand_rank[1][2] > high_int:
high_int = playas[player].hand_rank[1][2]
reallyreallyreally = []
if player not in reallyreallyreally:
reallyreallyreally.append(player)
if playas[player].hand_rank[1][2] == high_int:
if player not in reallyreallyreally:
reallyreallyreally.append(player)
if len(reallyreallyreally) >= 2:
high_int = 0
for player in reallyreallyreally:
if playas[player].hand_rank[1][3] > high_int:
high_int = playas[player].hand_rank[1][3]
thisisitiswear = []
if player not in thisisitiswear:
thisisitiswear.append(player)
if playas[player].hand_rank[1][3] == high_int:
if player not in thisisitiswear:
thisisitiswear.append(player)
if len(thisisitiswear) >= 2:
high_int = 0
for player in thisisitiswear:
if playas[player].hand_rank[1][4] > high_int:
high_int = playas[player].hand_rank[1][4]
okaylastone = []
if player not in okaylastone:
okaylastone.append(player)
if playas[player].hand_rank[1][4] == high_int:
if player not in okaylastone:
okaylastone.append(player)
if len(okaylastone) >= 1:
return [okaylastone,thisisitiswear,reallyreallyreally,reallyreally,reallywinners]
elif len(thisisitiswear) >= 1:
return [thisisitiswear,reallyreallyreally,reallyreally,reallywinners]
elif len(reallyreallyreally) >= 1:
return [reallyreallyreally,reallyreally,reallywinners]
elif len(reallyreally) >= 1:
return [reallyreally,reallywinners]
else:
return [reallywinners]
######################################################################
def turnscreen(listplayrs):
os.system('cls' if os.name=='nt' else 'clear')
raw_input("Turn the screen to %s and hit enter" % listplayrs[0])
os.system('cls' if os.name=='nt' else 'clear')
###############################################################
def straight_finder(ranks):
if len(ranks) < 5:
return (False,0)
if ranks[0] == ranks[1]+1 and ranks[1] == ranks[2]+1 and ranks[2]
== ranks[3]+1\
and ranks[3] == ranks[4]+1:
return (True,ranks[0])
else:
return straight_finder(ranks[1:])
###############################################################################
def four_of_a_kind_finder(ranks):
rankcounter = {14:0,13:0,12:0,11:0,10:0,9:0,8:0,7:0,6:0,5:0,4:0,3:0,2:0}
for rank in ranks:
rankcounter[rank] += 1
if 4 in rankcounter.values():
highcards = []
quads = 0
for card,amount in rankcounter.items():
if amount == 4:
quads = card
if 0 < amount < 4:
highcards.append(card)
return (True,quads,max(highcards))
else:
return (False,0)
################################################################################
def fullhouse_finder(ranks):
rankcounter = {14:0,13:0,12:0,11:0,10:0,9:0,8:0,7:0,6:0,5:0,4:0,3:0,2:0}
for rank in ranks:
rankcounter[rank] += 1
set3s = []
set2s = []
for card,amount in rankcounter.items():
if amount == 3:
set3s.append(card)
if amount == 2:
set2s.append(card)
if len(set3s) == 2:
set2s.append(min(have3))
set3s.remove(min(have3))
if len(set3s) == 1 and len(set2s) >= 1:
return (True,set3s[0],max(set2s))
else:
return (False,0,0)
#################################################
def two_pair_finder(pairranks):
rankcounter = {14:0,13:0,12:0,11:0,10:0,9:0,8:0,7:0,6:0,5:0,4:0,3:0,2:0}
pairsof2 = []
for rank in pairranks:
rankcounter[rank] += 1
if rankcounter[rank] == 2:
pairsof2.append(rank)
highlist = []
for rank,amount in rankcounter.items():
if amount == 1:
highlist.append(rank)
highcard = max(highlist)
if len(pairsof2) >= 2:
overpair = max(pairsof2)
pairsof2.remove(max(pairsof2))
return (True,overpair,max(pairsof2),highcard)
else:
return (False,0,0,0)
#################################################
def flush_finder(final_hand):
flushdict = {'S':0,'C':0,'H':0,'D':0}
for card in final_hand:
flushdict[card[1]] += 1
flushranks = []
for card in final_hand:
if flushdict[card[1]] >= 5:
suit = card[1]
if card[0] == 'A':
flushranks.append(14)
elif card[0] == 'K':
flushranks.append(13)
elif card[0] == 'Q':
flushranks.append(12)
elif card[0] == 'J':
flushranks.append(11)
elif card[0] == 'T':
flushranks.append(10)
else:
flushranks.append(int(card[0]))
if len(flushranks) >= 5:
return (True,sorted(flushranks,reverse=True),suit)
else:
return (False,0,0)
##################################################################
def straight_flush_finder(final_hand):
flushsuits = {'S':0,'C':0,'H':0,'D':0}
for card in final_hand:
flushsuits[card[1]] += 1
flushranks = []
for card in final_hand:
if flushsuits[card[1]] >= 5:
suit = card[1]
if card[0] == 'A':
flushranks.append(14)
elif card[0] == 'K':
flushranks.append(13)
elif card[0] == 'Q':
flushranks.append(12)
elif card[0] == 'J':
flushranks.append(11)
elif card[0] == 'T':
flushranks.append(10)
else:
flushranks.append(int(card[0]))
if len(flushranks) >= 5:
flushrankscopy = flushranks[:]
for rank in flushrankscopy:
if rank == 14:
flushranks.append(1)
newflushranks = sorted(list(set(flushranks)),reverse=True)
while len(newflushranks) >= 5:
if newflushranks[0] == newflushranks[1]+1 and
newflushranks[1] == newflushranks[2]+1 and newflushranks[2] ==
newflushranks[3]+1\
and newflushranks[3] == newflushranks[4]+1:
return (True,newflushranks[0])
else:
newflushranks = newflushranks[1:]
return (False,0,0)
return (False,0,0)
######################################################################
def three_of_a_kind_finder(pairranks):
rankcounter = {14:0,13:0,12:0,11:0,10:0,9:0,8:0,7:0,6:0,5:0,4:0,3:0,2:0}
for rank in pairranks:
rankcounter[rank] += 1
threes = []
highcards = []
for card,amount in rankcounter.items():
if amount == 3:
threes.append(card)
if 0 < amount < 3:
highcards.append(card)
highcards.sort(reverse=True)
if len(threes) >= 1:
return (True,max(threes),highcards[:2])
return (False,0)
######################################################################
def one_pair_finder(pairranks):
rankcounter = {14:0,13:0,12:0,11:0,10:0,9:0,8:0,7:0,6:0,5:0,4:0,3:0,2:0}
for rank in pairranks:
rankcounter[rank] += 1
highcards = []
pairsof2 = []
for card,amount in rankcounter.items():
if amount == 1:
highcards.append(card)
if amount == 2:
pairsof2.append(card)
if len(pairsof2) >=1:
return (True,max(pairsof2),sorted(highcards,reverse=True))
else:
return (False,0,0)
######################################################################
def highcard_finder(pairranks):
highlist = (sorted(pairranks,reverse=True))
return highlist
######################################################################
def main():
playernum = 0
while playernum not in range(2,10):
playernum = int(raw_input("Welcome to Simultron, How Many Players?"))
listofplayers = []
for x in range(int(playernum)):
listofplayers.append("player"+str(x+1))
for x in listofplayers:
print "Welcome "+x
startstack = 0
while startstack not in range(100,1000000,10):
startstack = int(raw_input("What will the starting stack sizes
be? (100-1000000 increments of 10) "))
bigblind = 19
while bigblind not in range(20,1000,20):
bigblind = int(raw_input("What will the big blind start at?
(20-1000, increments of 20) "))
deck1 = Deck()
table1 = Table(bigblind)
playas = dict((name, Player(stacksize=startstack)) for name in
listofplayers)
table1.fillseats(listofplayers,playas)
table1.postblinds(playas,bigblind,deck1)
if __name__ == "__main__":
main()
More information about the Python-list
mailing list