Dawn/src/dawn/poker/PokerGame.cpp

197 lines
4.4 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "PokerGame.hpp"
using namespace Dawn;
PokerGame::PokerGame(SceneItem *item) : SceneItemComponent(item) {
}
void PokerGame::onStart() {
SceneItemComponent::onStart();
this->players = this->getScene()->findComponents<PokerPlayer>();
assertTrue(this->players.size() > 0);
this->newGame();
}
void PokerGame::newGame() {
this->newRound();
auto it = this->players.begin();
while(it != this->players.end()) {
auto player = *it;
player->chips = POKER_PLAYER_CHIPS_DEFAULT;
player->isOut = false;
++it;
}
this->setDealer(0x00);
}
void PokerGame::newRound() {
this->deck.clear();
Card::fillDeck(&this->deck);
this->smallBlind = POKER_BLIND_SMALL_DEFAULT;
this->bigBlind = POKER_BLIND_BIG_DEFAULT;
this->grave.clear();
this->community.clear();
this->pots.clear();
this->pots.push_back(PokerPot());
auto it = this->players.begin();
while(it != this->players.end()) {
auto player = *it;
player->hand.clear();
player->currentBet = 0;
player->isFolded = false;
player->isShowingHand = false;
player->hasBetThisRound = false;
player->timesRaised = 0;
player->currentBet = 0;
++it;
}
this->setDealer(this->dealerIndex + 0x01);
}
void PokerGame::newBettingRound() {
auto it = this->players.begin();
while(it != this->players.end()) {
auto player = *it;
player->hasBetThisRound = false;
player->timesRaised = 0;
++it;
}
this->betterIndex = this->bigBlindIndex;
this->betterIndex = this->getNextBetterIndex();
}
uint8_t PokerGame::getNextBetterIndex() {
uint8_t j, i;
for(i = 0; i < this->players.size(); i++) {
j = (i + this->betterIndex) % this->players.size();
auto player = this->players[j];
if(player->needsToBetThisRound()) return j;
}
return 0xFF;
}
void PokerGame::takeBlinds() {
auto playerSmallBlind = this->players[this->smallBlindIndex];
auto playerBigBlind = this->players[this->bigBlindIndex];
playerSmallBlind->bet(this->smallBlind);
playerBigBlind->bet(this->bigBlind);
playerSmallBlind->hasBetThisRound = false;
playerBigBlind->hasBetThisRound = false;
}
void PokerGame::setDealer(uint8_t dealer) {
uint8_t i, k;
PokerPlayer *player;
bool_t foundDealer;
bool_t foundSmall;
foundDealer = false;
foundSmall = false;
this->dealerIndex = dealer;
for(i = 0; i < this->players.size(); i++) {
k = (dealer + i) % this->players.size();
player = this->players[k];
if(player->isOut) ;
if(!foundDealer) {
this->dealerIndex = k;
foundDealer = true;
} else if(!foundSmall) {
this->smallBlindIndex = k;
foundSmall = true;
} else {
this->bigBlindIndex = k;
break;
}
}
}
uint8_t PokerGame::getRemainingBettersCount() {
uint8_t count = 0;
auto it = this->players.begin();
while(it != this->players.end()) {
if((*it)->needsToBetThisRound()) count++;
++it;
}
return count;
}
uint8_t PokerGame::getRemainingPlayersCount() {
uint8_t count = 0;
auto it = this->players.begin();
while(it != this->players.end()) {
auto player = *it;
if(!player->isFolded && !player->isOut) count++;
++it;
}
return count;
}
int32_t PokerGame::getCurrentCallValue() {
assertTrue(this->pots.size() > 0);
return this->pots.back().call;
}
void PokerGame::burnCard() {
assertTrue(this->deck.size() > 0);
auto card = this->deck.back();
this->deck.pop_back();
this->grave.push_back(card);
}
void PokerGame::dealCard(PokerPlayer *player) {
assertTrue(this->deck.size() > 0);
auto card = this->deck.back();
this->deck.pop_back();
player->hand.push_back(card);
}
void PokerGame::dealToEveryone(uint8_t count) {
for(uint8_t i = 0; i < count; i++) {
auto it = this->players.begin();
while(it != this->players.end()) {
this->dealCard(*it);
++it;
}
}
}
void PokerGame::turn(uint8_t count) {
assertTrue(this->deck.size() >= count);
for(uint8_t i = 0; i < count; i++) {
auto card = this->deck.back();
this->deck.pop_back();
this->community.push_back(card);
}
}
uint8_t PokerGame::getCountOfCardsToTurn() {
switch(this->community.size()) {
case 0x00:
return POKER_FLOP_CARD_COUNT;
case 0x03:
return POKER_TURN_CARD_COUNT;
case 0x04:
return POKER_RIVER_CARD_COUNT;
default:
return 0xFF;
}
}