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 54e6f8af authored by Yildius's avatar Yildius

Tests and bug fixes, the first hand works fine (second one is wrong)

parent cf4282b0
......@@ -32,4 +32,4 @@ class Card:
return self.getValue() < card.getValue()
def sameAs(self, card):
return self.__color == card.__color and self.__value == card.value
return self.getColor() == card.getColor() and self.getValue() == card.getValue()
......@@ -11,11 +11,11 @@
# h < h2
#
from enum import Enum
import enum
from card import Card
class HandName(Enum):
class HandName(enum.IntEnum):
HIGH_CARD = 1
PAIR = 2
TWO_PAIRS = 3
......@@ -29,19 +29,25 @@ class HandName(Enum):
class Hand:
def __init__(self, cards = []):
assert cards == [] or len(cards) == 5
self.__cards = cards
self.__handName = HandName.HIGH_CARD
if len(cards) == 5:
if len(self.__cards) == 5:
self.__cards.sort()
self.__cards.reverse()
print(self.cardsStr())
# print(self.cardsStr())
self.__readHand()
def getHandName(self):
return self.__handName
def getCards(self):
return self.__cards
def getCard(self, i):
assert self.isLoaded()
assert i >= 0 and i <= 5
return self.__cards[i]
def isLoaded(self):
return len(self.__cards) == 5
def handStr(self):
return ""
......@@ -126,13 +132,14 @@ class Hand:
self.__handName = HandName.STRAIGHT
def __lt__(self, hand):
assert len(cards) == 5
assert self.isLoaded()
assert hand.isLoaded()
rslt = False
if self.__handName == hand.__handName:
if self.getHandName() == hand.getHandName():
for i in range(5):
if self.__cards[i] < hand.__cards[i]:
if self.getCard(i) < hand.getCard(i):
rslt = True
else:
if self.__handName < hand.__handName:
if self.getHandName() < hand.getHandName():
rslt = True
return rslt
......@@ -41,22 +41,25 @@ class Player:
self.__position = pos
def computeHand(self, boardCards):
assert len(boardCards) == 5
cards = boardCards + self.__cards
# print("Hole cards {}, cards {}".format(len(self.__cards), len(cards)))
self.__hand = Hand(boardCards)
for c1 in cards:
for c2 in cards:
if not c1.sameAs(c2):
newHand = cards
newHand.remove(c1)
newHand.remove(c2)
if self.__hand < newHand:
self.__hand = newHand
for i in range(len(cards)):
for j in range(i+1, len(cards)):
newHandCards = []
for k in range(len(cards)):
if k != i and k != j:
newHandCards += [cards[k]]
# print("({0}, {1})/{2} : {3}".format(i, j, len(cards), len(newHandCards)))
newHand = Hand(newHandCards)
if self.__hand < newHand:
self.__hand = newHand
return self.__hand
def endTurn(self):
self.__cards = []
ans = self.__chipsOnTable
self.__chipsOnTable(0)
self.chipsOnTable(0)
return ans
def receiveCards(self, card1, card2):
......@@ -66,6 +69,7 @@ class Player:
self.__chips += chips
def yieldBet(self):
self.__hasHadAction = False
temp = self.__chipsOnTable
self.__chipsOnTable = 0
return temp
......
# Game engine
#
# Tests :
# t = Table()
# t.addPlayer(Player())
# t.addPlayer(Player())
# t.startHand()
# t.call(1)
# t.call(0)
# Then repeat the last 2 at will
#
# TODO: fix bug with wrong player action at the start of the second hand.
from card import Card
from deck import Deck
from player.player import Player
......@@ -17,6 +31,7 @@ class Table(object):
def __init__(self):
self.gameState = GameState.IDLE
self.players = []
self.playerIdsInHand = []
self.waitingPlayers = []
self.nbrPlayersInHand = 0
self.deck = Deck()
......@@ -37,7 +52,17 @@ class Table(object):
# Return the player at required position, 0 is button
def __playerAtPos(self, pos):
return self.button + pos % len(self.players)
return (self.button + pos) % len(self.players)
# Passes action to the next player in the hand
def __nextPos(self):
playerFound = False
while not playerFound:
self.activePlayer = (self.activePlayer + 1) % len(self.players)
if self.players[self.activePlayer].isPlaying():
playerFound = True
print("Action to player {}.".format(self.activePlayer))
return self.activePlayer
def startHand(self):
self.deck.reset()
......@@ -54,6 +79,7 @@ class Table(object):
self.players[self.__playerAtPos(2)].giveBB(self.blind)
for p in self.players:
p.receiveCards(self.deck.drawCard(), self.deck.drawCard())
p.newHand((i - self.button) % len(self.players))
self.handNbr += 1
self.gameState = GameState.PREFLOP
print("Starting hand {0} with {1} players.".format(self.handNbr, len(self.players)))
......@@ -63,7 +89,7 @@ class Table(object):
self.pot += p.yieldBet()
if self.gameState == GameState.RIVER:
remainingPlayerIds = []
for i in self.players:
for i in range(len(self.players)):
self.players[i].computeHand(self.boardCards)
if self.players[i].isPlaying():
remainingPlayerIds.append(i)
......@@ -74,20 +100,25 @@ class Table(object):
#TODO: manage draw
self.players[winner].receiveChip(self.pot)
self.pot = 0
self.StartHand()
self.button = self.__playerAtPos(1)
print("Hand ended, player {0} won.".format(winner))
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()))
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()))
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()))
def isBettingDone(self):
for p in self.players:
......@@ -99,6 +130,7 @@ class Table(object):
if self.activePlayer == playerId:
self.nbrPlayersInHand -= 1
self.players[playerId].fold()
self.__nextPos()
if self.isBettingDone():
self.__nextStep()
return True
......@@ -109,6 +141,7 @@ class Table(object):
def check(self, playerId):
if self.activePlayer == playerId:
self.players[playerId].check()
self.__nextPos()
if self.isBettingDone():
self.__nextStep()
return True
......@@ -119,6 +152,7 @@ class Table(object):
def call(self, playerId):
if self.activePlayer == playerId:
self.players[playerId].call()
self.__nextPos()
if self.isBettingDone():
self.__nextStep()
return True
......@@ -129,6 +163,7 @@ class Table(object):
def bet(self, playerId, amount):
if self.activePlayer == playerId:
self.players[playerId].bet(amount)
self.__nextPos()
if self.isBettingDone():
self.__nextStep()
return True
......
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