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 06070c12 authored by Yildius's avatar Yildius

Finished action system

parent 0a4aa6e9
......@@ -55,21 +55,23 @@ class Player:
# PRIVATE METHODS
def __putMoneyOnTable(self, amount):
amountBet = 0
if self.__isAllIn:
print("Player {} is already All In".format(self.__uniqueId))
return False
return 0
if amount >= self.__chips:
print("Player {} is All In".format(self.__uniqueId))
self.__isAllIn = True
amountBet = self.__chips
self.__chipsOnTable += self.__chips
self.__chips = 0
else:
if amount >= self.__chips:
print("Player {} is All In".format(self.__uniqueId))
self.__isAllIn = True
self.__chipsOnTable += self.__chips
self.__chips = 0
else:
print("Player {} bet {}".format(self.__uniqueId, amount))
self.__isAllIn = False
self.__chips -= amount
self.__chipsOnTable += amount
return True
print("Player {} bet {}".format(self.__uniqueId, amount))
self.__isAllIn = False
amountBet = amount
self.__chips -= amount
self.__chipsOnTable += amount
return amountBet
# PUBLIC METHODS
def newHand(self):
......@@ -118,32 +120,31 @@ class Player:
def giveBB(self, bigBlind):
return self.__putMoneyOnTable(bigBlind)
# Actions, called by Table.
# By calling these methodes, Table authorises the transaction.
# amount is the money added
# Returns the amount added
def bet(self, amount):
self.__hasHadAction = True
return self.__putMoneyOnTable(amount)
# Returns True if succeeds
def check(self):
self.__hasHadAction = True
# TODO
print("Player {} checked".format(self.__uniqueId))
return True
# Returns the amount added
def call(self, amountToCall):
self.__hasHadAction = True
# TODO check poker rules
print("Player {} called".format(self.__uniqueId))
deltaCall = amountToCall - self.__chipsOnTable
return self.__putMoneyOnTable(deltaCall)
return self.__putMoneyOnTable(amountToCall - self.__chipsOnTable)
# Returns True if succeeds
def fold(self):
self.__hasHadAction = True
# returnedCards = []
# returnedCards.append(self.__cards.pop())
# returnedCards.append(self.__cards.pop())
print("Player {} fold".format(self.__uniqueId))
self.__isPlaying = False
return True
# Timeout
# Timeout, shouldn't be needed (just check/fold called from Table)
def yieldAction(self):
self.__hasHadAction = True
......@@ -46,6 +46,7 @@ class Table(object):
self.smallBlind = 1
self.button = 0
self.currentBet = 0
self.minRaise = 0
self.handNbr = 0
def addPlayer(self, p):
......@@ -64,12 +65,19 @@ class Table(object):
if self.players[self.activePlayer].isPlaying():
playerFound = True
print("Action to player {}.".format(self.activePlayer))
print("Pot {}, min raise {}".format(self.pot, self.minRaise))
for p in self.players:
if p.isPlaying:
print("Player {} : {} ({} chips)".format(p.uniqueId, p.getChipsOnTable, p.getRemainingChips))
else:
print("Player {} : out ({} chips)".format(p.uniqueId, p.getRemainingChips))
return self.activePlayer
def startHand(self):
self.deck.reset()
self.boardCards = []
self.currentBet = self.blind
self.minRaise = self.blind
if len(self.waitingPlayers):
self.players.extend(self.waitingPlayers)
self.waitingPlayers = []
......@@ -87,12 +95,15 @@ class Table(object):
self.handNbr += 1
self.gameState = GameState.PREFLOP
print("Starting hand {0} with {1} players.".format(self.handNbr, len(self.players)))
for p in self.players:
print("Player {} : {} chips".format(p.uniqueId, p.getRemainingChips))
print("Action to player {}.".format(self.activePlayer))
def __nextStep(self):
for p in self.players:
self.pot += p.yieldBet() # after this all players have hasHadAction to False
self.currentBet = 0
self.minRaise = self.blind
if self.nbrPlayersInHand == 1: # everyone fold except on player who automatically wins
self.__nextPos() # find the winner
winner = self.activePlayer
......@@ -100,6 +111,7 @@ class Table(object):
self.pot = 0
self.button = self.__playerAtPos(1)
print("Hand ended, player {0} won.".format(winner))
self.startHand()
elif self.gameState == GameState.RIVER:
remainingPlayerIds = []
for i in range(len(self.players)):
......@@ -164,61 +176,75 @@ class Table(object):
return True
def fold(self, playerId):
if self.activePlayer == playerId:
self.nbrPlayersInHand -= 1
self.players[playerId].fold()
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
else:
print("Cant fold, action currently to player {}".format(self.activePlayer))
if self.activePlayer != playerId:
print("Player {} cant fold, action currently to player {}".format(playerId, self.activePlayer))
return False
if not self.players[playerId].fold():
print("Player {} cant fold, fold failed (unexpected).".format(playerId))
return False # fold could not happen for some reason
self.nbrPlayersInHand -= 1
print("Player {} folded.".format(playerId))
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
def check(self, playerId):
if self.activePlayer == playerId:
self.players[playerId].check()
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
else:
print("Cant check, action currently to player {}".format(self.activePlayer))
if self.activePlayer != playerId:
print("Player {} cant check, action currently to player {}".format(playerId, self.activePlayer))
return False
if self.players[playerId].getChipsOnTable != self.currentBet:
print("Player {} cant check, needs to call ({}/{})".format(playerId, self.players[playerId].getChipsOnTable, self.currentBet))
return False
if not self.players[playerId].check():
print("Player {} cant check, check failed (unexpected).".format(playerId))
return False # Check could not happen for some reason
print("Player {} checked.".format(playerId))
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
def call(self, playerId):
if self.activePlayer == playerId:
self.players[playerId].call(self.currentBet)
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
else:
print("Cant call, action currently to player {}".format(self.activePlayer))
if self.activePlayer != playerId:
print("Player {} cant call, action currently to player {}".format(playerId, self.activePlayer))
return False
amountCalled = self.players[playerId].call(self.currentBet)
if not amountCalled:
print("Player {} cant call, call failed (unexpected).".format(playerId))
return False # call could not happen for some reason
print("Player {} called {} ({} more).".format(playerId, self.currentBet, amountCalled))
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
def bet(self, playerId, amount): # bet_by
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.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
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
else:
print("Cant bet, action currently to player {}".format(self.activePlayer))
if self.activePlayer != playerId:
print("Player {} cant bet, action currently to player {}".format(playerId, self.activePlayer))
return False
if amount + self.players[playerId].getChipsOnTable < self.currentBet + self.minRaise:
print("Player {} cant bet, not adding enough money (would be {}/{})".format(playerId, amount + self.players[playerId].getChipsOnTable, self.currentBet + self.minRaise))
return False
if amount > self.players[playerId].getRemainingChips:
print("Player {} cant bet, not enough money ({}/{})".format(playerId, self.activePlayer, amount, self.players[playerId].getRemainingChips))
return False
if not self.players[playerId].bet(amount):
print("Player {} cant bet, betting failed (unexpected).".format(playerId))
return False # Betting could not happen for some reason
if not self.currentBet:
self.minRaise = amount
if self.players[playerId].getChipsOnTable > self.currentBet:
self.currentBet = self.players[playerId].getChipsOnTable
print("Player {} added {}, current bet to {}, minraise at {}.".format(playerId, amount, self.currentBet, self.minRaise))
if self.isBettingDone():
self.__nextStep()
else:
self.__nextPos()
return True
# State Table
......
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