Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

Commit 4f93849a authored by Arnaud Chevalier's avatar Arnaud Chevalier

improved nextStep function. One hand can be fully played until river. next :...

improved nextStep function. One hand can be fully played until river. next : manage everyone flods before river
parent f9b97b2a
......@@ -22,15 +22,18 @@ class Player:
def isPlaying(self):
return self.__isPlaying
@property
def getChipsOnTable(self):
return self.__chipsOnTable
@property
def hasHadAction(self):
return self.__hasHadAction
def getHand(self):
return self.__hand
@property
def getRemainingChips(self):
return self.__chips
......@@ -102,9 +105,11 @@ class Player:
return temp
def giveSB(self, smallBlind):
self.__hasHadAction = True
return self.__putMoneyOnTable(smallBlind)
def giveBB(self, bigBlind):
self.__hasHadAction = True
return self.__putMoneyOnTable(bigBlind)
def bet(self, amount):
......
......@@ -73,16 +73,16 @@ class Table(object):
self.players.extend(self.waitingPlayers)
self.waitingPlayers = []
for i in range(len(self.players)):
if self.players[i].getRemainingChips() <= 0:
if self.players[i].getRemainingChips <= 0:
del self.players[i]
assert len(self.players) >= 2
self.nbrPlayersInHand = len(self.players)
self.activePlayer = self.__playerAtPos(3)
self.players[self.__playerAtPos(1)].giveSB(self.smallBlind)
self.players[self.__playerAtPos(2)].giveBB(self.blind)
for p in self.players:
p.receiveCards(self.deck.drawCard(), self.deck.drawCard())
p.newHand()
self.players[self.__playerAtPos(1)].giveSB(self.smallBlind)
self.players[self.__playerAtPos(2)].giveBB(self.blind)
self.handNbr += 1
self.gameState = GameState.PREFLOP
print("Starting hand {0} with {1} players.".format(self.handNbr, len(self.players)))
......@@ -90,7 +90,8 @@ class Table(object):
def __nextStep(self):
for p in self.players:
self.pot += p.yieldBet()
self.pot += p.yieldBet() # after this all players have hasHadAction to False
self.currentBet = 0
if self.gameState == GameState.RIVER:
remainingPlayerIds = []
for i in range(len(self.players)):
......@@ -106,38 +107,44 @@ class Table(object):
self.pot = 0
self.button = self.__playerAtPos(1)
print("Hand ended, player {0} won.".format(winner))
self.startHand()
# self.startHand()
else:
if self.gameState == GameState.PREFLOP:
for i in range(3):
self.boardCards += [self.deck.drawCard()]
self.gameState = GameState.FLOP
print("Flop : {}{}{}".format(self.boardCards[0].cardStr(), self.boardCards[1].cardStr(), self.boardCards[2].cardStr()))
self.activePlayer = self.__playerAtPos(0)
self.__nextPos()
elif self.gameState == GameState.FLOP:
for i in range(1):
self.boardCards += [self.deck.drawCard()]
self.gameState = GameState.TURN
print("Turn : {}".format(self.boardCards[3].cardStr()))
self.activePlayer = self.__playerAtPos(0)
self.__nextPos()
elif self.gameState == GameState.TURN:
for i in range(1):
self.boardCards += [self.deck.drawCard()]
self.gameState = GameState.RIVER
print("River : {}".format(self.boardCards[4].cardStr()))
self.activePlayer = self.__playerAtPos(0)
self.__nextPos()
def isBettingDone(self):
ans = True
for p in self.players:
if p.isPlaying() and (not p.hasHadAction() or p.getChipsOnTable() != self.currentBet):
ans = False
return ans
if p.isPlaying() and (not p.hasHadAction or p.getChipsOnTable != self.currentBet):
return False
return True
def fold(self, playerId):
if self.activePlayer == playerId:
self.nbrPlayersInHand -= 1
self.players[playerId].fold()
self.__nextPos()
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
else:
print("Cant fold, action currently to player {}".format(self.activePlayer))
......@@ -146,9 +153,10 @@ class Table(object):
def check(self, playerId):
if self.activePlayer == playerId:
self.players[playerId].check()
self.__nextPos()
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
else:
print("Cant check, action currently to player {}".format(self.activePlayer))
......@@ -157,9 +165,10 @@ class Table(object):
def call(self, playerId):
if self.activePlayer == playerId:
self.players[playerId].call(self.currentBet)
self.__nextPos()
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
else:
print("Cant call, action currently to player {}".format(self.activePlayer))
......@@ -169,16 +178,17 @@ class Table(object):
if self.activePlayer == playerId:
player = self.players[playerId]
# Test if betting is covering the current table bet
if amount < player.getRemainingChips and amount + player.getChipsOnTable < self.getCurrentBet:
if (amount < player.getRemainingChips) and (amount + player.getChipsOnTable < self.currentBet):
print("Did not bet enough!")
return False
if not player.bet(amount):
return False # Betting could not happen for some reason
if not player.isAllIn: # Player actually raised the current table bet
self.currentBet = player.getChipsOnTable()
self.__nextPos()
self.currentBet = player.getChipsOnTable
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
else:
print("Cant bet, action currently to player {}".format(self.activePlayer))
......
......@@ -21,19 +21,36 @@ t.addPlayer(player5)
t.startHand()
print(t.players[0].hasHadAction)
print(t.players[1].hasHadAction)
print(t.players[2].hasHadAction)
print(len(t.players))
t.call(t.activePlayer)
t.fold(t.activePlayer)
print(t.isBettingDone())
t.call(t.activePlayer)
print(t.isBettingDone())
t.call(t.activePlayer)
print(t.isBettingDone())
t.bet(t.activePlayer, 4)
t.call(t.activePlayer)
t.fold(t.activePlayer)
t.call(t.activePlayer)
print(t.isBettingDone())
t.bet(t.activePlayer, 5)
t.call(t.activePlayer)
print(t.isBettingDone())
t.call(t.activePlayer)
print(t.isBettingDone())
t.bet(t.activePlayer, 5)
t.call(t.activePlayer)
print(t.isBettingDone())
t.call(t.activePlayer)
print(t.isBettingDone())
import table
import player.player
t = table.Table()
player1 = player.player.Player()
print(player1.uniqueId)
player2 = player.player.Player()
print(player2.uniqueId)
player3 = player.player.Player()
print(player3.uniqueId)
player4 = player.player.Player()
print(player4.uniqueId)
player5 = player.player.Player()
print(player5.uniqueId)
t.addPlayer(player1)
t.addPlayer(player2)
t.addPlayer(player3)
t.addPlayer(player4)
t.addPlayer(player5)
t.startHand()
print(t.players[0].hasHadAction)
print(t.players[1].hasHadAction)
print(t.players[2].hasHadAction)
print(len(t.players))
t.fold(t.activePlayer)
t.fold(t.activePlayer)
t.fold(t.activePlayer)
t.fold(t.activePlayer)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment