def __init__(self, starting_stack, blind_structure, hands_per_level, logging_level=logging.INFO): logging.basicConfig(stream=sys.stderr, level=logging_level) self.starting_stack = starting_stack self.blind_structure = blind_structure self.hands_per_level = hands_per_level self.players = [ mcplayer("Alice", self.starting_stack), randomplayer("Bob", self.starting_stack), randomplayer("Chris", self.starting_stack) ] self.pm = PlayerManager(self.players) self.pot = Pot() self.deck = Deck() self.board = [] self.blind_level = 1 #Uses 1 indexing not 0 indexing self.BIG_BLIND = self.blind_structure[self.blind_level - 1] self.SMALL_BLIND = self.blind_structure[self.blind_level - 1] // 2 self.num_hands = 0 self.game_state = {} self.players_in_hand = 0
def test_pot(): #This class is hard it should test sooo many situations I'll list some #write tests for them as I go #1 player wins #1 player wins but one player that contributing to pot Folded #2 way split with 3 players #2 way split with 2 players #player is all in from posting small blind -wins/loses #player is all in from posting big blind -wins/loses #everyone folds PREFLOP #the winning player doesn't cover everyone #situation with 3 side pots. IE the player with the most chips has the worst hand, player with least chips has best hand. and etc for middle hand print("TESTING POT CLASS") stack = 100 bet = 10 p1 = Player("0", stack) p2 = Player("1", stack) p3 = Player("2", stack) players = [p1, p2, p3] print("testing 3 way split") pot = Pot() deck = Deck() deck.shuffle() board = [] hand = deck.draw(2) board.append(deck.draw(5)) #Note all 3 players are given the same hand which is not realistic but shouldn't matter p1.deal_new_hand(hand, 1) p2.deal_new_hand(hand, 1) p3.deal_new_hand(hand, 1) pot.add_to_pot(p1, bet) pot.add_to_pot(p2, bet) pot.add_to_pot(p3, bet) assert(p1.get_stack() == stack - bet) assert(p2.get_stack() == stack - bet) assert(p3.get_stack() == stack - bet) assert(pot.get_pot_size() == 3*bet) pot.payout(players, board) print(f"p1 stack: {p1.get_stack()}") assert(p1.get_stack() == stack) assert(p2.get_stack() == stack) assert(p3.get_stack() == stack) assert(pot.get_pot_size() == 0) print("Pot class: 8/8 tests passed")
def test_discharge(self, mock_sleep): pot = Pot(5, 6) mock_gpio.IN = 'in' mock_gpio.OUT = 'out' pot.discharge() mock_gpio.setup.assert_has_calls([call(5, 'in'), call(6, 'out')]) mock_gpio.output.assert_called_once_with(6, False) mock_sleep.assert_called_once_with(0.005)
def POST(self): i = web.input() potstr = i.pot handstr = i.hand my_pot = Pot(set(potstr.split())) my_hand = Hand(set(handstr.split())) # my_pot = Pot(set(["AC", "2C", "2S", "KS"])) # my_hand = Hand(set(["3C", "KH"])) allotherhand = allCardsExceptHand(my_pot, my_hand) mybesthand = getBestFromPotAndHand(my_pot, my_hand) results = [mybesthand] for alist in allotherhand: besthand = getBestFromPotAndHandList(my_pot, alist) results.append(besthand) sortResults = pokerrank(results) finalResults = [] for ahand in sortResults: finalResults.append(ahand) if (set(ahand.split()) == set(mybesthand.split())): break return render.calc(finalResults)
def create_side_pots(self): reprocess_pots = True # gotta be a better way to do this but # I am getting tired of writing this bot while reprocess_pots: lowest_all_in_player = None for pot in self.pots: if pot.locked: continue lowest_all_in_player = PotManager.get_lowest_all_in_player(pot) break if lowest_all_in_player: amount = lowest_all_in_player.bet * len(pot.players) total = pot.amount difference = total - amount new_players = list(pot.players) new_players.remove(lowest_all_in_player) pot.locked = True pot.amount = amount side_pot = Pot('Side Pot {}'.format(len(self.pots)), difference, new_players) self.pots.append(side_pot) else: reprocess_pots = False
def create_initial_pot(table): amount = 0 side_pots_tmp = [] if table.ante > 0: for player in table.player_order: if player.stack > table.ante: amount += table.ante player.stack -= table.ante player.equity += table.ante else: player.equity = player.stack amount += player.stack side_pots_tmp.append(player.stack) player.stack = 0 if player.equity > (table.bb_amount + table.ante): amount += (player.equity - (table.bb_amount + table.ante)) player.equity = table.bb_amount + table.ante # instantiate pot object players = table.player_order[:] pot = Pot(players, amount) # append side pots to Pot obj in case of all-in players if side_pots_tmp: for p in side_pots_tmp: pot.side_pots.append(p) # Add the initial pot to the table table.pots.append(pot)
class Player: """ Players are objects that play Sabbacc """ def __init__(self, playerName="", startAmt=0): self.gold = Pot(startAmt) self.name = playerName def bet(amount, betCondition): if betCondition: bufferVal = self.gold.remove(amount) return bufferVal def newHand(self, startHand): self.hand = startHand def __repr__(self): return ( self.name + ":" + "\n" + "\n".join([" {0}: {1}".format(c, c.cardValue) for c in self.hand.handCards]) + "\n Score:{0}".format(self.hand.score) + "\n Your Gold:{0}".format(self.gold.value) )
def __init__(self, chairCount): players = [] pot = Pot() seats = [] dealOrder = [] button = 1 muck = collections.deque() for i in range(chairCount): seats.append('open')
def evaluate_pot(table): # Todo: Need more complete unit test coverage of this function """ Evaluates pot on table and creates side pots if necessary """ pot = table.pots[-1] if pot.side_pots: # import pdb # pdb.set_trace() pot.side_pots = sorted(pot.side_pots) while pot.side_pots: amount = pot.side_pots.pop(0) x = 0 new_players = [] for player in pot.players: player.equity -= amount x += 1 new_players.append(player) for player in pot.players: if player.stack == 0 and player.equity == 0: pot.players.remove(player) for p in pot.side_pots: ind = pot.side_pots.index(p) pot.side_pots[ind] -= amount amount = amount * x amount += pot.amount pot.amount = 0 new_pot = Pot(new_players, amount) table.pots.insert(0, new_pot) if len(pot.players) == 1: pot.side_pots = [] for player in pot.players: pot.amount += player.equity player.equity = 0 if len(pot.players) == 1: # Give last guy in pot money pot.players[0].stack += pot.amount table.pots.pop() if not table.pots: # Start new hand new_hand(table) else: while len(table.community_cards) < 5: deal(table) analyze(table) else: if len(table.community_cards) < 5: deal(table) else: analyze(table)
def __init__(self, game, button): self.game = game # make a copy of the deck for this round and shuffle it self.deck = copy.copy(game.deck) self.deck.shuffle() self.players = copy.copy(game.active_players) self.active_players = copy.copy(self.players) self.button = button self.pot = Pot() self.all_in = []
def __init__(self, game, button, evaluator): self.game = game self.ranks = [ "", "", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" ] # make a copy of the deck for this round and shuffle it self.deck = copy.copy(game.deck) self.deck.shuffle() self.players = copy.copy(game.active_players) self.active_players = copy.copy(self.players) self.button = button self.pot = Pot() self.all_in = [] self.evaluator = evaluator
class Board: """Mancala board. Board layout with pot indexs'. ______________________________________ | [13] (12)(11)(10)( 9)( 8)( 7) [ ] | | [ ] ( 0)( 1)( 2)( 3)( 4)( 5) [ 6] | ______________________________________ """ pots = [Pot(i) for i in range(14)] @property def state(self): return [pot.count for pot in self.pots]
def __init__(self, deck, players, blindtimer, rules, bigblindonly=False): self.__deck = deck self.__players = players self.__blinds = blindtimer.getblinds() self.__blindtimer = blindtimer self.__pots = [Pot(players, 0)] self.__community = Community() self.__rules = rules self.__bigblindonly = bigblindonly self.__currentplayer = None self.__nextplayeractions = () self.__firsttoact = None self.__lasttoact = None self.__winners = [] self.__round = self.NOT_SET
def post_small_blind(self, player): if player.money < 1: return False pot = Pot('Main Pot', player, 1) player.money -= 1 player.bet = 1 player.action = "SMALL BLIND" self.pots.append(pot) self.chat.message('<@{}> posts small blind $1'.format(player.slack_id)) self.current_bet = 1 self.big_blind = None self.small_blind = player return True
def __updatepot(self): # No pots? What are you doing here? if len(self.__pots) == 0: return players = self.__pots[-1].getplayers()[:] # Find everyone who's all in allins = [] for player in players: if player.isallin(): allins.append((player.gettotalbet(), player.name(), player)) allins.sort(key=lambda player: player[0]) # Remove the biggest all-in as they're still in if len(allins) > 1 and allins[-1][0] > allins[-2][0]: allins[-1][2].reducebet(allins[-1][0] - allins[-2][0]) allins[-1][2].notallin() self.__pots[-1].subtractvalue(allins[-1][0] - allins[-2][0]) del (allins[-1]) # fill the current pot and create a new side pot/pots last_allin = 0 allin_count = 0 for allin in allins: allin_count = allin_count + 1 if allin[0] > last_allin: # not already processed this all in bet for player in players: if allin[0] > player.gettotalbet(): self.__pots[-1].addvalue(player.gettotalbet()) player.addedtopot(player.gettotalbet()) else: self.__pots[-1].addvalue(allin[0]) player.addedtopot(allin[0]) last_allin = allin[0] players.remove(allin[2]) self.__pots.append(Pot(players, 0)) # fill the main pot or side pot for remaining players for player in players: if player.gettotalbet() == 0: continue self.__pots[-1].addvalue(player.gettotalbet()) player.addedtopot(player.gettotalbet())
def setUp(self): self.player1 = Player("player1", 100) self.player2 = Player("player2", 100) self.player3 = Player("player3", 100) self.player4 = Player("player4", 100) self.player5 = Player("player5", 100) self.player6 = Player("player6", 100) self.table = Table(6, 1, 2, [50, 100]) self.table.join(1, self.player1, 100) self.table.join(2, self.player2, 100) self.table.join(3, self.player3, 100) self.table.join(4, self.player4, 100) self.table.join(5, self.player5, 100) self.table.join(6, self.player6, 100) # Set player order; happy path for k, v in self.table.seats.items(): self.table.player_order.append(v) pot = Pot(self.table.player_order, 0) self.table.pots.append(pot)
from sets import Set from card import Card from hand import Hand from pot import Pot import itertools def findsubsets(S,m): return set(itertools.combinations(S, m)) class Utils(object): def __init__(self, info): self.info = info @staticmethod def getAllCombi(hand, pot): for card in hand.cards: print card for card in pot.cards: print card if __name__ == "__main__": my_pot = Pot(set([Card("A", "C"),Card("2", "C"),Card("2", "S"), Card("K", "S")])) my_hand = Hand(set([Card("3", "C"), Card("K", "H")])) Utils.getAllCombi(my_hand, my_pot)
class Spin(): def __init__(self, starting_stack, blind_structure, hands_per_level, logging_level=logging.INFO): logging.basicConfig(stream=sys.stderr, level=logging_level) self.starting_stack = starting_stack self.blind_structure = blind_structure self.hands_per_level = hands_per_level self.players = [ mcplayer("Alice", self.starting_stack), randomplayer("Bob", self.starting_stack), randomplayer("Chris", self.starting_stack) ] self.pm = PlayerManager(self.players) self.pot = Pot() self.deck = Deck() self.board = [] self.blind_level = 1 #Uses 1 indexing not 0 indexing self.BIG_BLIND = self.blind_structure[self.blind_level - 1] self.SMALL_BLIND = self.blind_structure[self.blind_level - 1] // 2 self.num_hands = 0 self.game_state = {} self.players_in_hand = 0 def start(self): #Play until there is only one player remaining IE 1 player has all the chips while len(self.players) > 1: #new hand self.__prepare_new_hand() if logging.root.level == logging.DEBUG: for i, player in enumerate(self.players): logging.debug( f"Seat {i+1}: {player.get_name()} ({player.get_stack()} in chips)" ) #PREFLOP self.__deal_new_hand() self.__post_blinds( ) #in a real game players post blinds first, it's a small formality that doesn't matter here self.__betting_round( ) #There's a bug that occurs here for some reason a player is folded and makes a bet (Bug is very unlikely 1/20000 games just to give an idea) #FLOP self.__betting_round() #TURN self.__betting_round() #RIVER self.__betting_round() #SHOWDOWN self.pot.payout(self.players, self.board) self.remove_eliminated_players() return self.players[0] #This is the winning player def remove_eliminated_players(self): self.players[:] = [x for x in self.players if x.get_stack() > 0] ''' This method resets all variables to be able to start a new hand and does housekeeping of the button and blinds ''' def __prepare_new_hand(self): self.game_state["street"] = Street.PREFLOP self.num_hands += 1 self.players_in_hand = len(self.players) self.pm.move_button() logging.debug(f"*** Hand #{self.num_hands} ***") if self.num_hands % self.hands_per_level == 0: self.__increase_blinds() ''' shuffles the deck and deals a starting hand to each remaining player as well as the board even though it's not technically visible to the players yet This method also resets the player variables while we deal them a new hand seems like a weird place to have that but it's kind of like in order for a player to get a new hand they need to reset their hand status (variables) ''' def __deal_new_hand(self): self.deck.shuffle() self.board.clear() self.board = self.deck.draw(5) self.game_state["board"] = self.board #Deals 2 cards to each remaining player for i, player in enumerate(self.players): self.players[i].deal_new_hand(self.deck.draw(2), self.BIG_BLIND) def __post_blinds(self): self.pot.add_to_pot(self.pm.get_small_blind(), self.SMALL_BLIND) logging.debug( f"{self.pm.get_small_blind().get_name()}: posts small blind {self.SMALL_BLIND}" ) self.pot.add_to_pot(self.pm.get_big_blind(), self.BIG_BLIND) logging.debug( f"{self.pm.get_big_blind().get_name()}: posts big blind {self.BIG_BLIND}" ) def __increase_blinds(self): logging.debug(f"Blind level: {self.blind_level}") self.blind_level += 1 self.BIG_BLIND = self.blind_structure[self.blind_level - 1] self.SMALL_BLIND = self.blind_structure[self.blind_level - 1] // 2 logging.debug("Blinds went up") def __betting_round(self): if self.game_state["street"] == Street.PREFLOP: logging.debug(f"*** HOLE CARDS ***") elif self.game_state["street"] == Street.FLOP: logging.debug(f"*** FLOP ***") elif self.game_state["street"] == Street.TURN: logging.debug(f"*** TURN ***") elif self.game_state["street"] == Street.RIVER: logging.debug( f"*** RIVER *** {Card.print_pretty_cards(self.board)}") if logging.root.level == logging.DEBUG: for player in self.players: logging.debug( f"Dealt to {player.get_name()} {Card.print_pretty_cards(player.get_hand())}" ) #keeps track of how many player actions were taken, it's used to ensure #that everyone gets at least 1 chance to act. Otherwise preflop when #everyone just calls the big blind doesn't get option to bet #There has to be more than 1 player that can act to continue can_act = self.__can_players_act() if can_act > 1: actions = 0 acting_player = self.pm.first_to_act(self.game_state["street"]) self.game_state["remain"] = can_act while ( (not self.__is_betting_completed()) or (actions < len(self.players))) and (self.players_in_hand > 1): action, betsize = acting_player.get_action(self.game_state) self.__process_action(acting_player, action, betsize) acting_player = self.pm.next_player() actions += 1 #After the betting round we reset the amount committed by players for player in self.players: player.reset_street_committed() #Once the betting round is over we can increment street self.__next_street() ''' This method checks that all the players for the current betting round are either folded, all in or have matched the required bet ''' def __is_betting_completed(self): complete = True for player in self.players: if not (player.folded or player.allin or (player.street_committed >= Player.street_max_committed)): complete = False return complete ''' This method checks how many of the remaining players can take an action We have to check this because otherwise we enter into betting rounds when we shouldn't ''' def __can_players_act(self): act_count = 0 #Number of players who can still take legal actions in given hand for player in self.players: if (not player.folded) and (not player.allin): act_count += 1 return act_count def __next_street(self): self.game_state["street"] = (Street(self.game_state["street"] + 1)) def __process_action(self, player, action, bet_size=0): #Folded or checked if action == Action.CHECK_FOLD: if player.can_check(): logging.debug(f"Player {player.get_name()} checked.") else: logging.debug(f"Player {player.get_name()} folded.") player.folded = True self.players_in_hand -= 1 elif action == Action.CALL: logging.debug(f"Player {player.get_name()} called ${bet_size}") self.pot.add_to_pot(player, bet_size) elif action == Action.BET_RAISE: logging.debug(f"Player {player.get_name()} bet/raised ${bet_size}") self.pot.add_to_pot(player, bet_size)
cardstr += card.__str__() + " " cardstr = cardstr[0:-1] allhands.append(cardstr) besthand = poker(allhands) return besthand #print besthand else: return "2S 3C 7D 5C 6H" #print "invalid hand " + str(handlist) if __name__ == "__main__": potstr = sys.argv[1] handstr = sys.argv[2] my_pot = Pot(set(potstr.split())) my_hand = Hand(set(handstr.split())) # my_pot = Pot(set(["AC", "2C", "2S", "KS"])) # my_hand = Hand(set(["3C", "KH"])) allotherhand = allCardsExceptHand(my_hand) mybesthand = getBestFromPotAndHand(my_pot, my_hand) results = [] for alist in allotherhand: besthand = getBestFromPotAndHandList(my_pot, alist) results.append(besthand) sortResults = pokerrank(results) counter = 0
def setUp(self): self.player1 = Player("player1", 100) self.player2 = Player("player2", 100) self.player3 = Player("player3", 100) self.player4 = Player("player4", 100) self.player5 = Player("player5", 100) self.player6 = Player("player6", 100) self.player7 = Player("player7", 100) self.player8 = Player("player8", 100) self.player9 = Player("player9", 100) self.table = Table(9, 1, 2, [50, 100]) self.table.join(1, self.player1, 100) self.table.join(2, self.player2, 100) self.table.join(3, self.player3, 100) self.table.join(4, self.player4, 100) self.table.join(5, self.player5, 100) self.table.join(6, self.player6, 100) self.table.join(7, self.player7, 100) self.table.join(8, self.player8, 100) self.table.join(9, self.player9, 100) cards = [ 13, 'h', 14, 'h', 5, 'h', 5, 's', 5, 'h', 10, 'c', 3, 'h', 4, 'h', 13, 's', 14, 's', 14, 's', 5, 's', 12, 's', 11, 's', 14, 's', 2, 'd', 4, 'h', 2, 'h' ] # Set player order; happy path for k, v in self.table.seats.items(): self.table.player_order.append(v) x = 0 for player in self.table.player_order: value = cards[x] x += 1 suit = cards[x] x += 1 card0 = Card("name", value, suit) value = cards[x] x += 1 suit = cards[x] x += 1 card1 = Card("name", value, suit) player.hole_cards.append(card0) player.hole_cards.append(card1) table_cards = [12, 'h', 11, 'h', 10, 'h', 5, 'c', 5, 'd'] x = 0 while len(self.table.community_cards) != 5: value = table_cards[x] x += 1 suit = table_cards[x] x += 1 card = Card("name", value, suit) self.table.community_cards.append(card) pot = Pot(self.table.player_order, 100) self.table.pots.append(pot)
''' @brief: brewd is a flask-based xHTCPCP daemon and server, written in python3 @author: Bader Zaidan @version: 0.01 ''' from flask import Flask, redirect #, url_for from pot import Pot app = Flask(__name__) covfefe = Pot("covfefe") @app.route("/") def reroute(): #test redirect return redirect("pot", code=302) # return "Home" @app.route("/pot", methods=["GET"]) def get(): return covfefe.get() @app.route("/pot", methods=["HEAD"]) def head(): return covfefe.head()
from flask import Flask from pot import Pot app = Flask(__name__) pot = Pot('teapot') @app.route('/teapot', methods=['GET']) def get(): return pot.get() @app.route('/teapot', methods=['BREW', 'POST']) def brew(): return pot.brew() @app.route('/teapot', methods=['WHEN']) def when(): return pot.when() if __name__ == '__main__': app.run(port=8081)
def __init__(self, playerName="", startAmt=0): self.gold = Pot(startAmt) self.name = playerName