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

Added much of the simple game engine, to be tested/debugged

parent 13f8120a
Pipeline #5797 failed with stages
......@@ -7,26 +7,29 @@
from enum import Enum
class Color(Enum):
HEARTS = 1
SPADES = 2
DIAMONDS = 3
CLUBS =4
HEARTS = 1
SPADES = 2
DIAMONDS = 3
CLUBS =4
class Card:
def __init__(self, color, value):
assert value >= 2 and value <= 14
self.__color = color
self.__value = value
def getColor(self):
return self.__color
def getValue(self):
return self.__value
def cardStr(self):
return '({0}:{1})'.format(self.__value, self.__color)
def __lt__(self, card):
return self.getValue() < card.getValue()
def __init__(self, color, value):
assert value >= 2 and value <= 14
self.__color = color
self.__value = value
def getColor(self):
return self.__color
def getValue(self):
return self.__value
def cardStr(self):
return '({0}:{1})'.format(self.__value, self.__color)
def __lt__(self, card):
return self.getValue() < card.getValue()
def sameAs(self, card):
return self.__color == card.__color and self.__value == card.value
......@@ -8,10 +8,7 @@ import random
class Deck:
def __init__(self):
self.__cards = []
for c in card.Color:
for i in range(2, 15):
self.__cards.append(card.Card(c, i))
random.shuffle(self.__cards)
self.reset()
def getSize(self):
return len(self.__cards)
......@@ -19,3 +16,10 @@ class Deck:
def drawCard(self):
assert len(self.__cards)
return self.__cards.pop()
def reset(self):
self.__cards = []
for c in card.Color:
for i in range(2, 15):
self.__cards.append(card.Card(c, i))
random.shuffle(self.__cards)
......@@ -16,122 +16,123 @@ from card import Card
class HandName(Enum):
HIGH_CARD = 1
PAIR = 2
TWO_PAIRS = 3
THREE_OF_A_KIND = 4
STRAIGHT = 5
FLUSH = 6
FULL_HOUSE = 7
FOUR_OF_A_KIND = 8
STRAIGHT_FLUSH = 9
HIGH_CARD = 1
PAIR = 2
TWO_PAIRS = 3
THREE_OF_A_KIND = 4
STRAIGHT = 5
FLUSH = 6
FULL_HOUSE = 7
FOUR_OF_A_KIND = 8
STRAIGHT_FLUSH = 9
class Hand:
def __init__(self, cards):
assert len(cards) == 5
self.__cards = cards
self.__handName = HandName.HIGH_CARD
self.__cards.sort()
self.__cards.reverse()
print(self.cardsStr())
self.__readHand()
def __init__(self, cards = []):
self.__cards = cards
self.__handName = HandName.HIGH_CARD
if len(cards) == 5:
self.__cards.sort()
self.__cards.reverse()
print(self.cardsStr())
self.__readHand()
def getHandName(self):
return self.__handName
def getHandName(self):
return self.__handName
def getCards(self):
return self.__cards
def getCards(self):
return self.__cards
def handStr(self):
return ""
def handStr(self):
return ""
def cardsStr(self):
return '{0}{1}{2}{3}{4}'.format(self.__cards[0].cardStr(), self.__cards[1].cardStr(), self.__cards[2].cardStr(), self.__cards[3].cardStr(), self.__cards[4].cardStr())
def cardsStr(self):
return '{0}{1}{2}{3}{4}'.format(self.__cards[0].cardStr(), self.__cards[1].cardStr(), self.__cards[2].cardStr(), self.__cards[3].cardStr(), self.__cards[4].cardStr())
def __sendToBack(self, nbr):
self.__cards.append(self.__cards[nbr])
del self.__cards[nbr]
def __sendToBack(self, nbr):
self.__cards.append(self.__cards[nbr])
del self.__cards[nbr]
def __readHand(self):
counter = []
for i in range(15):
counter.append(0)
for card in self.__cards:
counter[card.getValue()] += 1
fourSame = 0
threeSame = 0
firstPair = 0
secondPair = 0
for i in range(2, 15):
if counter[i] == 4:
fourSame = i
elif counter[i] == 3:
threeSame = i
elif counter[i] == 2:
if firstPair:
secondPair = i
else:
firstPair = i
if fourSame:
self.__handName = HandName.FOUR_OF_A_KIND
if self.__cards[0].getValue() != fourSame:
self.__sendToBack(0)
elif threeSame:
if firstPair:
self.__handName = HandName.FULL_HOUSE
if self.__cards[0].getValue() != threeSame:
self.__sendToBack(0)
self.__sendToBack(0)
else:
self.__handName = HandName.THREE_OF_A_KIND
if self.__cards[1].getValue() != threeSame:
self.__sendToBack(0)
self.__sendToBack(0)
elif self.__cards[0].getValue() != threeSame:
__sendToBack(0)
elif secondPair:
self.__handName = HandName.TWO_PAIRS
if self.__cards[0].getValue() != secondPair:
self.__sendToBack(0)
elif self.__cards[2].getValue() != firstPair :
self.__sendToBack(2)
elif firstPair:
self.__handName = HandName.PAIR
if self.__cards[4].getValue() == firstPair:
self.__sendToBack(0)
self.__sendToBack(0)
self.__sendToBack(0)
elif self.__cards[3].getValue() == firstPair:
self.__sendToBack(0)
self.__sendToBack(0)
self.__sendToBack(2)
elif self.__cards[2].getValue() == firstPair:
self.__sendToBack(0)
else:
isFlush = True
for i in range(4):
if self.__cards[0].getColor() != self.__cards[i+1].getColor():
isFlush = False
if isFlush:
self.__handName = HandName.FLUSH
isStraight = self.__cards[4].getValue() == self.__cards[0].getValue() - 4
if self.__cards[0].getValue() == 14 and self.__cards[1].getValue() == 5:
isStraight = True
self.__sendToBack(0)
if isStraight:
if isFlush:
self.__handName = HandName.STRAIGHT_FLUSH
else:
self.__handName = HandName.STRAIGHT
def __readHand(self):
counter = []
for i in range(15):
counter.append(0)
for card in self.__cards:
counter[card.getValue()] += 1
fourSame = 0
threeSame = 0
firstPair = 0
secondPair = 0
for i in range(2, 15):
if counter[i] == 4:
fourSame = i
elif counter[i] == 3:
threeSame = i
elif counter[i] == 2:
if firstPair:
secondPair = i
else:
firstPair = i
if fourSame:
self.__handName = HandName.FOUR_OF_A_KIND
if self.__cards[0].getValue() != fourSame:
self.__sendToBack(0)
elif threeSame:
if firstPair:
self.__handName = HandName.FULL_HOUSE
if self.__cards[0].getValue() != threeSame:
self.__sendToBack(0)
self.__sendToBack(0)
else:
self.__handName = HandName.THREE_OF_A_KIND
if self.__cards[1].getValue() != threeSame:
self.__sendToBack(0)
self.__sendToBack(0)
elif self.__cards[0].getValue() != threeSame:
__sendToBack(0)
elif secondPair:
self.__handName = HandName.TWO_PAIRS
if self.__cards[0].getValue() != secondPair:
self.__sendToBack(0)
elif self.__cards[2].getValue() != firstPair :
self.__sendToBack(2)
elif firstPair:
self.__handName = HandName.PAIR
if self.__cards[4].getValue() == firstPair:
self.__sendToBack(0)
self.__sendToBack(0)
self.__sendToBack(0)
elif self.__cards[3].getValue() == firstPair:
self.__sendToBack(0)
self.__sendToBack(0)
self.__sendToBack(2)
elif self.__cards[2].getValue() == firstPair:
self.__sendToBack(0)
else:
isFlush = True
for i in range(4):
if self.__cards[0].getColor() != self.__cards[i+1].getColor():
isFlush = False
if isFlush:
self.__handName = HandName.FLUSH
isStraight = self.__cards[4].getValue() == self.__cards[0].getValue() - 4
if self.__cards[0].getValue() == 14 and self.__cards[1].getValue() == 5:
isStraight = True
self.__sendToBack(0)
if isStraight:
if isFlush:
self.__handName = HandName.STRAIGHT_FLUSH
else:
self.__handName = HandName.STRAIGHT
def __lt__(self, hand):
rslt = False
if self.__handName == hand.__handName:
for i in range(5):
if self.__cards[i] < hand.__cards[i]:
rslt = True
else:
if self.__handName < hand.__handName:
rslt = True
return rslt
def __lt__(self, hand):
assert len(cards) == 5
rslt = False
if self.__handName == hand.__handName:
for i in range(5):
if self.__cards[i] < hand.__cards[i]:
rslt = True
else:
if self.__handName < hand.__handName:
rslt = True
return rslt
......@@ -7,22 +7,68 @@ class Player:
# INIT
def __init__(self):
self.__hand = Hand()
self.__chips = float
self.__cards = [Card()]
self.__chipsOnTable = float
self.__position = int
self.__isPlaying = bool
self.__isAllIn = bool
self.__chips = int(0)
self.__cards = []
self.__chipsOnTable = int(0)
self.__position = int(0)
self.__hasHadAction = bool(False)
self.__isPlaying = bool(False)
self.__isAllIn = bool(False)
# GETTERS & SETTERS
def isPlaying(self):
return self.__isPlaying
def getChipsOnTable(self):
return self.__chipsOnTable
def hasHadAction(self):
return self.__hasHadAction
def getHand(self):
return self.__hand
def getRemainingChips(self):
return self.__chips
# PRIVATE METHODS
# PUBLIC METHODS
def newHand(self, pos):
self.__hasHadAction = False
self.__isPlaying = True
self.__isAllIn = False
self.__position = pos
def computeHand(self, boardCards):
cards = boardCards + self.__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
return self.__hand
def endTurn(self):
self.__cards = []
ans = self.__chipsOnTable
self.__chipsOnTable(0)
return ans
def receiveCards(self, card1, card2):
self.__cards = [card1, card2]
def receiveChip(self, chips):
self.__chips += chips
def yieldBet(self):
temp = self.__chipsOnTable
self.__chipsOnTable = 0
return temp
def giveSB(self, smallBlind):
if smallBlind >= self.__chips:
......@@ -45,6 +91,7 @@ class Player:
self.__chipsOnTable += bigBlind
def bet(self, amount):
self.__hasHadAction = True
if self.__isAllIn:
return False
else:
......@@ -59,17 +106,23 @@ class Player:
return False
def check(self):
self.__hasHadAction = True
# TODO
return
def call(self):
self.__hasHadAction = True
# TODO
return
def fold(self):
self.__hasHadAction = True
returnedCards = []
returnedCards.append(self.__cards.pop())
returnedCards.append(self.__cards.pop())
self.__isPlaying = False
return returnedCards
def yieldAction(self):
self.__hasHadAction = True
from cards import Deck
from players import Player
from card import Card
from deck import Deck
from player.player import Player
import json
from enum import Enum
class GameState(Enum):
IDLE = 0
PREFLOP = 1
FLOP = 2
TURN = 3
RIVER = 4
class Table(object):
def __init__(self):
self.gameState = GameState.IDLE
self.players = []
self.deck= Deck()
self.waitingPlayers = []
self.nbrPlayersInHand = 0
self.deck = Deck()
self.pot = 0
self.boardCards = []
self.com = []
self.activePlayer = 1
self.blind = 1
self.buyIn = 200
self.blind = 2
self.smallBlind = 1
self.button = 0
self.currentBet = 0
self.handNbr = 0
def addPlayer(self, p):
p.receiveChip(self.buyIn)
self.waitingPlayers += [p]
# Return the player at required position, 0 is button
def __playerAtPos(self, pos):
return self.button + pos % len(self.players)
def startHand(self):
self.deck.reset()
self.currentBet = self.blind
if len(self.waitingPlayers):
self.players += self.waitingPlayers
for i in range(len(self.players)):
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())
self.handNbr += 1
self.gameState = GameState.PREFLOP
print("Starting hand {0} with {1} players.".format(self.handNbr, len(self.players)))
def __nextStep(self):
for p in self.players:
self.pot += p.yieldBet()
if self.gameState == GameState.RIVER:
remainingPlayerIds = []
for i in self.players:
self.players[i].computeHand(self.boardCards)
if self.players[i].isPlaying():
remainingPlayerIds.append(i)
winner = remainingPlayerIds[0]
for i in remainingPlayerIds:
if self.players[winner].getHand() < self.players[remainingPlayerIds[i]].getHand():
winner = i
#TODO: manage draw
self.players[winner].receiveChip(self.pot)
self.pot = 0
self.StartHand()
else:
if self.gameState == GameState.PREFLOP:
for i in range(3):
self.boardCards += [self.deck.drawCard()]
self.gameState = GameState.FLOP
elif self.gameState == GameState.FLOP:
for i in range(1):
self.boardCards += [self.deck.drawCard()]
self.gameState = GameState.TURN
elif self.gameState == GameState.TURN:
for i in range(1):
self.boardCards += [self.deck.drawCard()]
self.gameState = GameState.RIVER
def isBettingDone(self):
for p in self.players:
if p.isPlaying() and not p.hasHadAction():
return False
return True
def fold(self, playerId):
if self.activePlayer == playerId:
return self.players[playerId].fold()
self.nbrPlayersInHand -= 1
self.players[playerId].fold()
if self.isBettingDone():
self.__nextStep()
return True
else:
print("Cant fold, action currently to player {}".format(self.activePlayer))
return False
def check(self, playerId):
if self.activePlayer == playerId:
return self.players[playerId].check()
self.players[playerId].check()
if self.isBettingDone():
self.__nextStep()
return True
else:
print("Cant check, action currently to player {}".format(self.activePlayer))
return False
def call(self, playerId):
if self.activePlayer == playerId:
return self.players[playerId].call()
self.players[playerId].call()
if self.isBettingDone():
self.__nextStep()
return True
else:
print("Cant call, action currently to player {}".format(self.activePlayer))
return False
def bet(self, playerId, amount):
if self.activePlayer == playerId:
return self.players[playerId].bet(amount)
self.players[playerId].bet(amount)
if self.isBettingDone():
self.__nextStep()
return True
else:
print("Cant bet, action currently to player {}".format(self.activePlayer))
return False
# not ready yet
"""
def state(self):
json.dumps({
"pot": self.pot,
......@@ -49,3 +148,4 @@ class Table(object):
"isActive": player.isActive
} for player in self.players]
})
"""
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