Files
pokerth/src/engine/local_engine/localboard.cpp
T

379 lines
11 KiB
C++
Raw Normal View History

2011-07-03 22:27:36 +00:00
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* Copyright (C) 2006-2011 Felix Hammer, Florian Thauer, Lothar May *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
2007-01-13 02:40:33 +00:00
#include "localboard.h"
#include "handinterface.h"
#include <game_defs.h>
2007-10-23 21:26:07 +00:00
#include <core/loghelper.h>
2007-09-29 00:08:19 +00:00
#include "localexception.h"
2007-10-14 11:01:44 +00:00
#include "engine_msg.h"
2007-01-13 02:40:33 +00:00
LocalBoard::LocalBoard(unsigned dp) : BoardInterface(), pot(0), sets(0), dealerPosition(dp), allInCondition(false), lastActionPlayerID(0)
2007-01-13 02:40:33 +00:00
{
2010-10-28 08:04:36 +00:00
myCards[0] = myCards[1] = myCards[2] = myCards[3] = myCards[4] = 0;
2007-01-13 02:40:33 +00:00
}
LocalBoard::~LocalBoard()
{
}
void LocalBoard::setPlayerLists(PlayerList sl, PlayerList apl, PlayerList rpl)
{
seatsList = sl;
2007-09-23 21:30:51 +00:00
activePlayerList = apl;
runningPlayerList = rpl;
}
2007-01-13 02:40:33 +00:00
void LocalBoard::collectSets()
{
2007-01-13 02:40:33 +00:00
sets = 0;
PlayerListConstIterator it_c;
for(it_c=seatsList->begin(); it_c!=seatsList->end(); ++it_c) {
sets += (*it_c)->getMySet();
}
2007-01-13 02:40:33 +00:00
}
void LocalBoard::collectPot()
{
pot += sets;
2007-01-13 02:40:33 +00:00
sets = 0;
PlayerListIterator it;
for(it=seatsList->begin(); it!=seatsList->end(); ++it) {
(*it)->setMySetNull();
}
2007-01-13 02:40:33 +00:00
}
void LocalBoard::distributePot()
{
2007-08-09 18:02:58 +00:00
2007-10-17 14:46:20 +00:00
winners.clear();
2007-08-10 18:44:41 +00:00
size_t i,j,k,l;
PlayerListIterator it;
PlayerListConstIterator it_c;
2007-08-10 00:17:46 +00:00
// filling player sets vector
std::vector<unsigned> playerSets;
for(it=seatsList->begin(); it!=seatsList->end(); ++it) {
2007-10-29 22:26:50 +00:00
if((*it)->getMyActiveStatus()) {
playerSets.push_back( ( ((*it)->getMyRoundStartCash()) - ((*it)->getMyCash()) ) );
2007-08-10 00:15:51 +00:00
} else {
playerSets.push_back(0);
}
2007-10-29 22:26:50 +00:00
(*it)->setLastMoneyWon(0);
2007-08-10 00:15:51 +00:00
}
// sort player sets asc
std::vector<unsigned> playerSetsSort = playerSets;
2007-08-10 00:15:51 +00:00
sort(playerSetsSort.begin(), playerSetsSort.end());
// potLevel[0] = amount, potLevel[1] = sum, potLevel[2..n] = winner
std::vector<unsigned> potLevel;
2007-08-10 00:15:51 +00:00
// temp var
int highestCardsValue;
size_t winnerCount;
2012-12-09 19:41:39 +01:00
bool finalPot;
int potCarryOver = 0;
2007-08-10 00:15:51 +00:00
size_t mod;
bool winnerHit;
// level loop
for(i=0; i<playerSetsSort.size(); i++) {
// restart levelHighestCardsValue
highestCardsValue = 0;
// level detection
if(playerSetsSort[i] > 0) {
// level amount
potLevel.push_back(playerSetsSort[i]);
// level sum
2012-12-09 19:41:39 +01:00
potLevel.push_back((playerSetsSort.size()-i)*potLevel[0] + potCarryOver);
2007-08-10 00:15:51 +00:00
// determine level highestCardsValue
for(it_c=seatsList->begin(), j=0; it_c!=seatsList->end(); ++it_c,j++) {
if((*it_c)->getMyActiveStatus() && (*it_c)->getMyCardsValueInt() > highestCardsValue && (*it_c)->getMyAction() != PLAYER_ACTION_FOLD && playerSets[j] >= potLevel[0]) {
highestCardsValue = (*it_c)->getMyCardsValueInt();
2007-08-10 00:15:51 +00:00
}
}
// level winners
for(it_c=seatsList->begin(), j=0; it_c!=seatsList->end(); ++it_c,j++) {
if((*it_c)->getMyActiveStatus() && highestCardsValue == (*it_c)->getMyCardsValueInt() && (*it_c)->getMyAction() != PLAYER_ACTION_FOLD && playerSets[j] >= potLevel[0]) {
potLevel.push_back((*it_c)->getMyUniqueID());
2007-08-10 00:15:51 +00:00
}
}
// determine the number of level winners
winnerCount = potLevel.size()-2;
2007-10-14 11:01:44 +00:00
if (!winnerCount) {
throw LocalException(__FILE__, __LINE__, ERR_NO_WINNER);
2007-10-14 11:01:44 +00:00
}
2007-08-10 00:15:51 +00:00
2012-12-09 19:41:39 +01:00
// check if this is the final pot level for at least one winner
finalPot = false;
for(j=2; j<potLevel.size(); j++) {
// find seat with potLevel[j]-ID
for(it=seatsList->begin(), k=0; it!=seatsList->end(); ++it, k++) {
if((*it)->getMyUniqueID() == potLevel[j] && potLevel[0] == playerSets[k]) {
finalPot = true;
break;
}
}
if(finalPot) break;
}
2012-12-09 19:41:39 +01:00
if(finalPot) {
// distribute the pot level sum to level winners
mod = (potLevel[1])%winnerCount;
// pot level sum divisible by winnerCount
if(mod == 0) {
for(j=2; j<potLevel.size(); j++) {
// find seat with potLevel[j]-ID
for(it=seatsList->begin(); it!=seatsList->end(); ++it) {
if((*it)->getMyUniqueID() == potLevel[j]) {
break;
}
}
if(it == seatsList->end()) {
throw LocalException(__FILE__, __LINE__, ERR_SEAT_NOT_FOUND);
}
(*it)->setMyCash( (*it)->getMyCash() + ((potLevel[1])/winnerCount));
// filling winners vector
winners.push_back((*it)->getMyUniqueID());
(*it)->setLastMoneyWon( (*it)->getLastMoneyWon() + (potLevel[1])/winnerCount );
}
}
// pot level sum not divisible by winnerCount
// --> distribution after smallBlind
else {
// find Seat with dealerPosition
for(it=seatsList->begin(); it!=seatsList->end(); ++it) {
2012-12-09 19:41:39 +01:00
if((*it)->getMyUniqueID() == dealerPosition) {
break;
}
}
2007-10-14 11:01:44 +00:00
if(it == seatsList->end()) {
throw LocalException(__FILE__, __LINE__, ERR_SEAT_NOT_FOUND);
2007-10-14 11:01:44 +00:00
}
2007-10-17 14:46:20 +00:00
2012-12-09 19:41:39 +01:00
for(j=0; j<winnerCount; j++) {
2007-08-10 00:15:51 +00:00
2012-12-09 19:41:39 +01:00
winnerHit = false;
2007-08-10 00:15:51 +00:00
2012-12-09 19:41:39 +01:00
for(k=0; k<MAX_NUMBER_OF_PLAYERS && !winnerHit; k++) {
2007-08-10 00:15:51 +00:00
2012-12-09 19:41:39 +01:00
++it;
if(it == seatsList->end())
it = seatsList->begin();
2007-08-10 00:15:51 +00:00
2012-12-09 19:41:39 +01:00
for(l=2; l<potLevel.size(); l++) {
if((*it)->getMyUniqueID() == potLevel[l]) winnerHit = true;
}
2007-08-10 18:44:41 +00:00
2007-08-10 00:15:51 +00:00
}
2012-12-09 19:41:39 +01:00
if(j<mod) {
(*it)->setMyCash( (*it)->getMyCash() + (int)((potLevel[1])/winnerCount) + 1);
// filling winners vector
winners.push_back((*it)->getMyUniqueID());
(*it)->setLastMoneyWon( (*it)->getLastMoneyWon() + ((potLevel[1])/winnerCount) + 1 );
} else {
(*it)->setMyCash( (*it)->getMyCash() + (int)((potLevel[1])/winnerCount));
// filling winners vector
winners.push_back((*it)->getMyUniqueID());
(*it)->setLastMoneyWon( (*it)->getLastMoneyWon() + (potLevel[1])/winnerCount );
}
2007-08-10 00:15:51 +00:00
}
}
2012-12-09 19:41:39 +01:00
potCarryOver = 0;
// pot refresh
pot -= potLevel[1];
} else {
potCarryOver = potLevel[1];
2007-08-10 00:15:51 +00:00
}
// reevaluate the player sets
for(j=0; j<playerSets.size(); j++) {
if(playerSets[j]>0) {
playerSets[j] -= potLevel[0];
}
}
// sort player sets asc
playerSetsSort = playerSets;
sort(playerSetsSort.begin(), playerSetsSort.end());
// clear potLevel
potLevel.clear();
}
}
// winners sort and unique
2007-10-17 14:46:20 +00:00
winners.sort();
winners.unique();
2007-08-10 00:15:51 +00:00
// ERROR-Outputs
2007-10-23 21:26:07 +00:00
if(pot!=0) LOG_ERROR(__FILE__ << " (" << __LINE__ << "): distributePot-ERROR: Pot = " << pot);
2007-08-10 00:15:51 +00:00
2010-10-28 08:04:36 +00:00
/*int sum = 0;
2011-11-13 11:55:18 +00:00
for(it_c=activePlayerList->begin(); it_c!=activePlayerList->end(); ++it_c) {
sum += (*it_c)->getMyCash();
2010-10-28 08:04:36 +00:00
}*/
2007-08-09 18:02:58 +00:00
}
void LocalBoard::determinePlayerNeedToShowCards()
{
playerNeedToShowCards.clear();
// in All In Condition everybody have to show the cards
if(allInCondition) {
PlayerListConstIterator it_c;
for(it_c = activePlayerList->begin(); it_c != activePlayerList->end(); ++it_c) {
if((*it_c)->getMyAction() != PLAYER_ACTION_FOLD) {
playerNeedToShowCards.push_back((*it_c)->getMyUniqueID());
}
}
}
else {
// all winners have to show their cards
2011-03-06 15:22:13 +00:00
std::list<std::pair<int,int> > level;
PlayerListConstIterator lastActionPlayerIt;
PlayerListConstIterator it_c;
// search lastActionPlayer
for(it_c = activePlayerList->begin(); it_c != activePlayerList->end(); ++it_c) {
2011-10-26 22:29:47 +00:00
if((*it_c)->getMyUniqueID() == lastActionPlayerID && (*it_c)->getMyAction() != PLAYER_ACTION_FOLD) {
lastActionPlayerIt = it_c;
break;
}
}
if(it_c == activePlayerList->end()) {
for(it_c = activePlayerList->begin(); it_c != activePlayerList->end(); ++it_c) {
if((*it_c)->getMyAction() != PLAYER_ACTION_FOLD) {
lastActionPlayerIt = it_c;
break;
}
}
}
// the player who has done the last action has to show his cards first
playerNeedToShowCards.push_back((*lastActionPlayerIt)->getMyUniqueID());
2011-03-06 15:22:13 +00:00
std::pair<int,int> level_tmp;
// get position und cardsValue of the player who show his cards first
2011-03-06 15:22:13 +00:00
level_tmp.first = (*lastActionPlayerIt)->getMyCardsValueInt();
level_tmp.second = (*lastActionPlayerIt)->getMyRoundStartCash()-(*lastActionPlayerIt)->getMyCash();
level.push_back(level_tmp);
2011-03-06 15:22:13 +00:00
std::list<std::pair<int,int> >::iterator level_it;
std::list<std::pair<int,int> >::iterator next_level_it;
it_c = lastActionPlayerIt;
++it_c;
for(unsigned i = 0; i < activePlayerList->size(); i++) {
if(it_c == activePlayerList->end()) it_c = activePlayerList->begin();
if((*it_c)->getMyAction() != PLAYER_ACTION_FOLD) {
2011-02-28 18:29:57 +00:00
for(level_it = level.begin(); level_it != level.end(); ++level_it) {
2011-03-06 15:22:13 +00:00
if((*it_c)->getMyCardsValueInt() > (*level_it).first) {
next_level_it = level_it;
2011-02-28 18:29:57 +00:00
++next_level_it;
if(next_level_it == level.end()) {
playerNeedToShowCards.push_back((*it_c)->getMyUniqueID());
2011-03-06 15:22:13 +00:00
level_tmp.first = (*it_c)->getMyCardsValueInt();
level_tmp.second = (*it_c)->getMyRoundStartCash()-(*it_c)->getMyCash();
level.push_back(level_tmp);
break;
}
} else {
2011-03-06 15:22:13 +00:00
if((*it_c)->getMyCardsValueInt() == (*level_it).first) {
next_level_it = level_it;
2011-02-28 18:29:57 +00:00
++next_level_it;
2011-03-06 15:22:13 +00:00
if(next_level_it == level.end() || (*it_c)->getMyRoundStartCash()-(*it_c)->getMyCash() > (*next_level_it).second) {
playerNeedToShowCards.push_back((*it_c)->getMyUniqueID());
2011-03-06 15:22:13 +00:00
if((*it_c)->getMyRoundStartCash()-(*it_c)->getMyCash() > (*level_it).second) {
(*level_it).second = (*it_c)->getMyRoundStartCash()-(*it_c)->getMyCash();
}
}
break;
} else {
2011-03-06 15:22:13 +00:00
if((*it_c)->getMyRoundStartCash()-(*it_c)->getMyCash() > (*level_it).second) {
playerNeedToShowCards.push_back((*it_c)->getMyUniqueID());
2011-03-06 15:22:13 +00:00
level_tmp.first = (*it_c)->getMyCardsValueInt();
level_tmp.second = (*it_c)->getMyRoundStartCash()-(*it_c)->getMyCash();
level.insert(level_it,level_tmp);
break;
}
}
}
}
}
++it_c;
}
2011-03-01 09:07:39 +00:00
level.clear();
}
// sort and unique the list
playerNeedToShowCards.sort();
playerNeedToShowCards.unique();
}