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 2940cbc2 authored by Arnaud Chevalier's avatar Arnaud Chevalier

addes a new playing mode tournoi

parent 7b426e0a
......@@ -25,21 +25,23 @@ class Card:
elif self.__color == Color.CLUBS : self.__json["Suit"] = "Clubs"
self.__json["Rank"] = self.__value
def getColor(self):
@property
def color(self):
return self.__color
def getValue(self):
@property
def value(self):
return self.__value
@property
def json(self):
return self.__json
def cardStr(self):
return '({0}:{1})'.format(self.__value, self.__color)
def __lt__(self, card):
return self.getValue() < card.getValue()
return self.__value < card.value
def sameAs(self, card):
return self.getColor() == card.getColor() and self.getValue() == card.getValue()
def json(self):
return self.__json
\ No newline at end of file
return self.__color == card.color and self.__value == card.value
......@@ -255,6 +255,18 @@ div.name-chips[disabled=disabled] {
cursor: pointer;
}
#big-blind {
position: absolute;
color: black;
border-radius: 1px;
height:30px;
font-size: 12px;
text-align: center;
width:90px;
left:450px;
top:425px;
}
#game-timer {
position: absolute;
color: black;
......@@ -299,12 +311,13 @@ button[disabled=disabled] {
#amount-bet {
left:200px;
top:35px;
width: 100px;
}
#history {
position:absolute;
width:400px;
height:600;
height:600px;
border-color: black;
border-style: solid;
border-width: 1px;
......
......@@ -259,7 +259,8 @@ function catchUp(tableState) {
//targetSeat = (targetSeat + 1) % 6;
moveActive((tableState["activePlayer"]+1));
setActionButtons(tableState);
setTimerOrigin(tableState["remainingTime"].toFixed(0))
setTimerOrigin(tableState["remainingTime"].toFixed(0));
setBB(tableState["BB"]);
}
function setActionButtons(tableState) {
......@@ -287,6 +288,10 @@ function setActionButtons(tableState) {
}
}
function setBB(BB) {
$('#big-blind').text("Big blind : $" + BB);
}
function updateGameState() {
var sidebar = $('#poker_table');
var requestData = {
......@@ -642,7 +647,6 @@ function moveActive(seat) {
active.removeClass();
active.addClass('seat' + seat.toString() + '-active');
var divs = document.getElementsByClassName("name-chips");
console.log(divs);
for (var i = 0; i < divs.length; i++) {
divs[i].setAttribute("disabled", "disabled");
}
......
......@@ -14,7 +14,6 @@
#
import enum
from card import Card
class HandName(enum.IntEnum):
......@@ -66,7 +65,7 @@ class Hand:
for i in range(15):
counter.append(0)
for card in self.__cards:
counter[card.getValue()] += 1
counter[card.value] += 1
fourSame = 0
threeSame = 0
firstPair = 0
......@@ -83,51 +82,51 @@ class Hand:
firstPair = i
if fourSame:
self.__handName = HandName.FOUR_OF_A_KIND
if self.__cards[0].getValue() != fourSame:
if self.__cards[0].value != fourSame:
self.__sendToBack(0)
elif threeSame:
if firstPair:
self.__handName = HandName.FULL_HOUSE
if self.__cards[0].getValue() != threeSame:
if self.__cards[0].value != threeSame:
self.__sendToBack(0)
self.__sendToBack(0)
else:
self.__handName = HandName.THREE_OF_A_KIND
if self.__cards[1].getValue() != threeSame:
if self.__cards[1].value != threeSame:
self.__sendToBack(0)
self.__sendToBack(0)
elif self.__cards[0].getValue() != threeSame:
elif self.__cards[0].value != threeSame:
self.__sendToBack(0)
self.__sendToBack(3)
elif secondPair:
self.__handName = HandName.TWO_PAIRS
if self.__cards[0].getValue() != secondPair:
if self.__cards[0].value != secondPair:
self.__sendToBack(0)
elif self.__cards[2].getValue() != firstPair :
elif self.__cards[2].value != firstPair :
self.__sendToBack(2)
elif firstPair:
self.__handName = HandName.PAIR
if self.__cards[4].getValue() == firstPair:
if self.__cards[4].value == firstPair:
self.__sendToBack(0)
self.__sendToBack(0)
self.__sendToBack(0)
elif self.__cards[3].getValue() == firstPair:
elif self.__cards[3].value == firstPair:
self.__sendToBack(0)
self.__sendToBack(0)
self.__sendToBack(2)
elif self.__cards[2].getValue() == firstPair:
elif self.__cards[2].value == firstPair:
self.__sendToBack(0)
self.__sendToBack(2)
self.__sendToBack(2)
else:
isFlush = True
for i in range(4):
if self.__cards[0].getColor() != self.__cards[i+1].getColor():
if self.__cards[0].color != self.__cards[i + 1].color:
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 = self.__cards[4].value == self.__cards[0].value - 4
if self.__cards[0].value == 14 and self.__cards[1].value == 5:
isStraight = True
self.__sendToBack(0)
if isStraight:
......
import json
from uuid import uuid4
from card import Card
from hand import Hand
......@@ -33,10 +31,12 @@ class Player:
def hasHadAction(self):
return self.__hasHadAction
def getHand(self):
@property
def hand(self):
return self.__hand
def getHoleCards(self):
@property
def holeCards(self):
return self.__cards
@property
......
import time
from uuid import uuid4
import uuid as uuid
......@@ -90,6 +91,7 @@ def game():
if len(currentTable.waitingPlayers) + len(currentTable.players) < currentTable.maxPlayers:
currentTable.addPlayer(p)
if len(currentTable.waitingPlayers) + len(currentTable.players) == currentTable.maxPlayers:
currentTable.startTime = time.time()
currentTable.startHand()
resp = redirect("/game?gameId="+gameId)
resp.set_cookie("username", name)
......@@ -116,7 +118,8 @@ def viewGame():
@app.route("/api/newGame", methods=["POST"])
def generateId():
nbPlayers = int(request.form['nbPlayers'])
table = Table(nbPlayers)
playingMode = int(request.form["playingMode"])
table = Table(nbPlayers, playingMode)
games[str(table.uniqueId)] = table
return str(table.uniqueId)
......
......@@ -10,6 +10,8 @@
# Then repeat the last 2 at will
#
# TODO: fix bug with wrong player action at the start of the second hand.
import time
from math import exp
from uuid import uuid4
import threading
......@@ -33,10 +35,19 @@ class GameState(Enum):
HAND_ENDED_NO_SHOW = 6
class PlayingMode(Enum):
CASHGAME = 0
TOURNOI = 1
class Table(object):
def __init__(self, maxPlayers):
def __init__(self, maxPlayers, playingMode):
self.uniqueId = str(uuid4())
if playingMode == 0:
self.playingMode = PlayingMode.CASHGAME
elif playingMode == 1:
self.playingMode = PlayingMode.TOURNOI
self.maxPlayers = maxPlayers
self.gameState = GameState.IDLE
self.players = []
......@@ -58,6 +69,7 @@ class Table(object):
self.timer = TableTimer(30, print)
self.endOfHandTimer = None
self.mainMutex = threading.Lock()
self.startTime = 0
def addPlayer(self, p):
self.mainMutex.acquire()
......@@ -92,6 +104,9 @@ class Table(object):
def startHand(self):
self.mainMutex.acquire()
print(self.playingMode)
if self.playingMode == PlayingMode.TOURNOI:
self.__calculateBlinds()
self.deck.reset()
self.boardCards = []
self.currentBet = self.blind
......@@ -145,11 +160,11 @@ class Table(object):
remainingPlayerIds.append(i)
winner = remainingPlayerIds[0]
for i in remainingPlayerIds:
if self.players[winner].getHand() < self.players[i].getHand():
if self.players[winner].hand < self.players[i].hand:
winner = i
winnerList = [winner]
for i in remainingPlayerIds:
if i != winner and not self.players[i].getHand() < self.players[winner].getHand():
if i != winner and not self.players[i].hand < self.players[winner].hand:
winnerList.append(i)
for playerId in winnerList:
self.players[playerId].receiveChip(self.pot / len(winnerList))
......@@ -160,16 +175,16 @@ class Table(object):
print("Hand ended, draw between {0} players :".format(len(winnerList)))
print("Board cards : {}".format(Hand(self.boardCards).cardsStr()))
for playerId in winnerList:
print("Player {} : {}{} -> {}".format(playerId, self.players[playerId].getHoleCards()[0].cardStr(),
self.players[playerId].getHoleCards()[1].cardStr(),
self.players[playerId].getHand().cardsStr()))
print("Player {} : {}{} -> {}".format(playerId, self.players[playerId].holeCards[0].cardStr(),
self.players[playerId].holeCards[1].cardStr(),
self.players[playerId].hand.cardsStr()))
else:
# One winner
print("Hand ended, player {0} won.".format(winner))
print("Board cards : {}".format(Hand(self.boardCards).cardsStr()))
print("Player {} : {}{} -> {}".format(winner, self.players[winner].getHoleCards()[0].cardStr(),
self.players[winner].getHoleCards()[1].cardStr(),
self.players[winner].getHand().cardsStr()))
print("Player {} : {}{} -> {}".format(winner, self.players[winner].holeCards[0].cardStr(),
self.players[winner].holeCards[1].cardStr(),
self.players[winner].hand.cardsStr()))
self.gameState = GameState.HAND_ENDED
self.endOfHandTimer = threading.Timer(8, self.startHand)
self.endOfHandTimer.start()
......@@ -316,7 +331,7 @@ class Table(object):
"BB": self.blind,
"SB": self.smallBlind,
"button": self.button,
"boardCards": [card.json() for card in self.boardCards],
"boardCards": [card.json for card in self.boardCards],
"currentBet": self.currentBet,
"handNbr": self.handNbr,
"gameState": self.gameState.name,
......@@ -351,13 +366,13 @@ class Table(object):
p = self.players[pId]
if p.isPlaying:
rtn.append({
'cards': [card.json() for card in p.getHoleCards()],
'cards': [card.json for card in p.holeCards],
'seatNumber': pId+1
})
else:
player = self.players[userIndex]
rtn = [{
'cards': [card.json() for card in player.getHoleCards()],
'cards': [card.json for card in player.holeCards],
'seatNumber': userIndex+1
}]
return rtn
......@@ -399,3 +414,13 @@ class Table(object):
self.check(self.activePlayer)
elif "fold" in self._availableActions(activePlayerId):
self.call(self.activePlayer)
def __calculateBlinds(self):
now = time.time()
elapsed = now - self.startTime
val = int(exp(elapsed/60/7)+1)
print("blind update " + str(val))
if val % 2 != 0:
val += 1
self.blind = val
self.smallBlind = val/2
......@@ -11,8 +11,8 @@
<h2>Start new game?</h2>
<div id="newGame">
<article>Choose a playing mode</article>
<input type="radio" name="mode" value="cashGame" checked> Cash Game<br>
<input type="radio" name="mode" value="tournoi"> Tournoi<br>
<input type="radio" name="mode" value="0" checked> Cash Game<br>
<input type="radio" name="mode" value="1"> Tournoi<br>
<article>Choose number of players</article>
<select title="nbPlayers" name="nbPlayers" id="nbPlayers">
<option value="2">2</option>
......@@ -48,13 +48,19 @@ function newgame() {
xhttp.onreadystatechange = function () {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var gameId = xhttp.responseText;
console.log(gameId)
document.getElementById("copyButton").removeAttribute("disabled")
console.log(gameId);
document.getElementById("copyButton").removeAttribute("disabled");
document.getElementById("gameId").value = gameId;
}
}
var nbPlayers = document.getElementById("nbPlayers");
xhttp.send("nbPlayers="+ nbPlayers.options[nbPlayers.selectedIndex].value);
var mode = document.getElementsByName("mode");
for (var i = 0; i < mode.length; i++) {
if (mode[i].checked) {
var playingMode = mode[i].value;
}
}
xhttp.send("nbPlayers="+ nbPlayers.options[nbPlayers.selectedIndex].value+"&playingMode="+playingMode);
}
function copyId() {
......
......@@ -98,6 +98,7 @@
<div id="button" class="seat6-button"></div>
<div id="active" class="seat6-active"></div>
<div id="game-timer" class="timer-normal">29 seconds remaining.</div>
<div id="big-blind" class="timer-normal">Big blind : $2</div>
<div id="history"></div>
<div id="action-options">
<button onclick='checkClicked()' id="check-button" disabled="disabled" class="action-button">Check</button>
......
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