0

Poker game in python:

NEWB here. How do I have the big/small blinds rotate every turn.I call this function in my main. Any help is appreciated.

Code:

poker table function:

def post_blinds(self,smallblind,bigblind):

    self.smallblind = smallblind
    self.bigblind = bigblind

    x=0

    for player in self.players: 

        if player == self.players[x+1]:
            player.stack = player.stack - smallblind


        if player == self.players[x+2]:
            player.stack = player.stack - bigblind

    x += 1

    print(x)

def main():

players = (Player("Player 1",200), Player("Player 2",200),Player("Player 3",200),Player("Player 4",200),Player("Player 5",200),Player("Player 6",200))


table = Table(players)

table.post_blinds(1,2)           #Post Blinds

table.deal_cards()               #Deal 2 cards to each player 
RageAgainstheMachine
  • 901
  • 2
  • 11
  • 28
  • For rotating you can always use the % of number of players on the player list. – UglyCode Dec 07 '15 at 23:29
  • im so new, I dont even know how to do that. – RageAgainstheMachine Dec 07 '15 at 23:53
  • Ok. So imagine you have 5 players in the list of players. You assign player[0] as small blind and player[1] as big blind. Store this index somewhere as small blind position. For every game you can add 1 to the position variable for the new small blind position and add 2 for the big blind position. Now at your 5th game you'll have small blind at player[4] and big blind at player[5] and there is no player[5] so you need to rotate it to player[0] by doing a mod 5 on every time you index a player. Code would be something like self.player[(x+1)%len(players)] – UglyCode Dec 07 '15 at 23:58
  • nice! think I got it but how do I run through multiple hands? How do I make my main function loop for a few times? – RageAgainstheMachine Dec 08 '15 at 00:48
  • You can add a loop to the operation of post_blind and deal_cards – UglyCode Dec 08 '15 at 02:02

2 Answers2

1

You set dealer pos to 0 by x = 0

And I presume you set x = x + 1 for the next hand.

But x being created inside your function will be recreated at every call and will always value 0.

You should use a variable from self.

Like self.dealerPos

And self.dealerPos have to be initialized somewhere else like in a initGame function

If you don't want dealerPos to become invalid,

Instead of

self.dealerPos = self.dealerPos + 1

Use

self.dealerPos = (self.dealerPos + 1)%self.nbPlayer

m_pOatrix
  • 150
  • 2
  • 13
  • thanks for the help. still not sure how to fix it but go habs! what do I do with this, says that x is not defined : for player in self.players: if player == self.players[x+1]: – RageAgainstheMachine Dec 07 '15 at 23:51
  • Use self.dealerPos where you used x. But don't set it to 0 – m_pOatrix Dec 07 '15 at 23:57
  • I made the changes but when I run it, the dealer doesent change... def dealer_button(self,dealer_Pos): self.dealer_Pos = dealer_Pos dealer_Pos = (self.dealer_Pos + 1)% len(self.players) def post_blinds(self,smallblind,bigblind): self.smallblind = smallblind self.bigblind = bigblind for player in self.players: if player == self.players[self.dealer_Pos+1]: player.stack = player.stack - smallblind if player == self.players[self.dealer_Pos+2]: player.stack = player.stack - bigblind – RageAgainstheMachine Dec 08 '15 at 01:35
  • heres the main: def main(): players = (Player("Player 1",200), Player("Player 2",200),Player("Player 3",200),Player("Player 4",200),Player("Player 5",200),Player("Player 6",200)) table = Table(players) #Create table table.dealer_button(1) #Create dealer button table.post_blinds(1,2) #Post Blinds table.deal_cards() #Deal 2 cards to each player – RageAgainstheMachine Dec 08 '15 at 01:36
  • the formating is shitty, any recommendations on how to better show you? – RageAgainstheMachine Dec 08 '15 at 01:36
  • See block explaning how to edit comment just bellow the place where you write a comment. Comments use mini-Markdown formatting ...... – m_pOatrix Dec 08 '15 at 02:00
  • In def dealer_button, replace `dealer_Pos = (self.dealer_Pos + 1)% len(self.players)` by `self.dealer_Pos = (self.dealer_Pos + 1)% len(self.players)` – m_pOatrix Dec 08 '15 at 02:03
1

Following your questions, you want a basic idea of how to manage the game. So here it is.

def init_game(self): 
    self.dealer_Pos = 0 

def update_dealer_button(self):
    self.dealer_Pos = (self.dealer_Pos + 1)% len(self.players)      

def post_blinds(self,smallblind,bigblind):
    self.smallblind = smallblind 
    self.bigblind = bigblind 
    for player in self.players: 
        if player == self.players[(self.dealer_Pos + 1)% len(self.players)]:
            player.stack = player.stack - smallblind
        if player == self.players[(self.dealer_Pos + 2)% len(self.players)]:
            player.stack = player.stack


def main(): 
    players = (Player("Player 1",200), Player("Player 2",200),Player("Player 3",200),Player("Player 4",200),Player("Player 5",200),Player("Player 6",200)) 

    table = Table(players) #Create table 
    table.init_game()      #Init dealer button and other future values
    gameOver = False
    while gameOver == False:
        table.post_blinds(1,2) #Post Blinds 
        table.deal_cards()     #Deal 2 cards to each player
        #all other game steps
        #analysing if game is over. If it is, set gameOver = True
        #if game is not over move the dealer button and play again
        update_dealer_button()
m_pOatrix
  • 150
  • 2
  • 13
  • just tried it, much better. The only thing is that once it runs a few times, I get an error saying the tuple index is out of range in the post_blinds function. How would you fix that, would you make a modification to that function or would you try and change it in the main? How do i make the Big blind go from the last player back to the first player? (Thanks alot btw) – RageAgainstheMachine Dec 08 '15 at 03:20
  • Answer editied.... No more out of range. Should fix also your blind going from last to first player. If you like answer, please vote it up :) – m_pOatrix Dec 08 '15 at 03:30
  • first time on site, dont have enough points to change publicly shown score....but as soon as I do, Ill upvote the hell out of your posts. THANKS! – RageAgainstheMachine Dec 08 '15 at 03:37