Added some transitions.
This commit is contained in:
@@ -14,6 +14,7 @@ bool pokerPlayerIsAlive(pokerplayer_t *player) {
|
||||
void pokerPlayerReset(pokerplayer_t *player) {
|
||||
player->cardCount = 0;
|
||||
player->currentBet = 0;
|
||||
player->timesRaised = 0;
|
||||
|
||||
// Invert then bitwise AND to turn off.
|
||||
player->state &= ~(
|
||||
|
||||
@@ -53,6 +53,9 @@ typedef struct {
|
||||
|
||||
/** Current bet in current round player has placed */
|
||||
int32_t currentBet;
|
||||
|
||||
/** How many times for the current flop that the player has raised */
|
||||
int32_t timesRaised;
|
||||
} pokerplayer_t;
|
||||
|
||||
/**
|
||||
|
||||
+58
-30
@@ -9,12 +9,13 @@
|
||||
|
||||
pokerturn_t pokerTurnGet(poker_t *poker, uint8_t playerIndex) {
|
||||
int32_t random, maxBet, bluffBet, callBet;
|
||||
float winProbability, expectedGain, confidence, potOdds;
|
||||
float confidence, expectedGain, potOdds;
|
||||
bool isBluff;
|
||||
int32_t amount;
|
||||
pokerplayer_t *player;
|
||||
pokerplayerwinning_t winning;
|
||||
uint8_t i, cardNumber0, cardNumber1, suitNumber0, suitNumber1;
|
||||
pokerturn_t turn;
|
||||
|
||||
player = poker->players + playerIndex;
|
||||
|
||||
@@ -23,42 +24,53 @@ pokerturn_t pokerTurnGet(poker_t *poker, uint8_t playerIndex) {
|
||||
return pokerTurnOut(poker, playerIndex);
|
||||
}
|
||||
|
||||
// Now we basically need to determine the AI's action here. The AI really only
|
||||
// needs to do one thing, decide whether or not they want to Bet or Not.
|
||||
// In future I may make checking optional, but for now there's really no
|
||||
// reason not to.
|
||||
pokerWinnerPlayerGet(&poker->dealer, player, &winning);
|
||||
// The following logic is heavily inspired by;
|
||||
// https://github.com/gorel/C-Poker-AI/blob/master/src/common/pokerai.c
|
||||
// But with some changes and smarts added by me. The original source code will
|
||||
// essentially just run a crap tun of simulated games and get the times that
|
||||
// they are expected to win from those games, but I'm just going to use the
|
||||
// odds of the winning hand.
|
||||
|
||||
// Get the current winning hand.
|
||||
|
||||
// Is this preflop?
|
||||
if(poker->dealer.cardsFacing == 0 && player->cardCount >= 2) {
|
||||
// Get the hand weight
|
||||
cardNumber0 = cardGetNumber(player->cards[0]);
|
||||
cardNumber1 = cardGetNumber(player->cards[1]);
|
||||
suitNumber0 = cardGetSuit(player->cards[0]);
|
||||
suitNumber1 = cardGetSuit(player->cards[1]);
|
||||
|
||||
// Get delta between cards
|
||||
i = mathAbs(cardNumber0 - cardNumber1);
|
||||
|
||||
// Get card weight
|
||||
confidence = (float)cardNumber0 + (float)cardNumber1;
|
||||
if(cardNumber0 == cardNumber1) {
|
||||
if(cardNumber0 == cardNumber1) {// Pairs
|
||||
confidence += 6;
|
||||
} else if(suitNumber0 == suitNumber1) {
|
||||
} else if(suitNumber0 == suitNumber1) {// Same suit
|
||||
confidence += 4;
|
||||
}
|
||||
|
||||
// Get difference from cards for guessing flush
|
||||
if(i > 4) {
|
||||
confidence -= 4;
|
||||
} else if(i > 2) {
|
||||
confidence -= i;
|
||||
}
|
||||
|
||||
// Get the confidence delta 0-1
|
||||
confidence = confidence / 30.0f;
|
||||
} else {
|
||||
// Simulate my hand being the winning hand, use that as the confidence
|
||||
pokerWinnerPlayerGet(&poker->dealer, player, &winning);
|
||||
confidence = pokerWinnerGetTypeConfidence(winning.type);
|
||||
}
|
||||
|
||||
// Now we know how confident the AI is, let's put a chip value to that weight
|
||||
// How many chips to call?
|
||||
callBet = poker->bet.currentBet - player->currentBet;
|
||||
winProbability = confidence;
|
||||
|
||||
// Do they need chips to call, or is it possible to check?
|
||||
if(callBet > 0) {
|
||||
potOdds = (float)callBet / ((float)callBet + (float)poker->bet.pot);
|
||||
} else {
|
||||
@@ -66,26 +78,36 @@ pokerturn_t pokerTurnGet(poker_t *poker, uint8_t playerIndex) {
|
||||
1.0f / (float)pokerBetGetRemainingPlayerCount(&poker->bet, poker->players)
|
||||
);
|
||||
}
|
||||
expectedGain = winProbability / potOdds;
|
||||
|
||||
// Now determine the expected ROI
|
||||
expectedGain = confidence / potOdds;
|
||||
|
||||
// Now get a random 0-100
|
||||
random = randInt32() % 100;
|
||||
maxBet = (int32_t)(poker->bet.currentBet / 1.75f) - (random / 2);//?
|
||||
maxBet -= callBet;//?
|
||||
|
||||
// Determine the max bet that the AI is willing to make
|
||||
maxBet = (int32_t)(poker->bet.currentBet / 1.75f) - (random / 2) - callBet;
|
||||
|
||||
// Determine what's a good bluff bet.
|
||||
bluffBet = random * maxBet / 100 / 2;
|
||||
|
||||
// Now prep the output
|
||||
isBluff = false;
|
||||
amount = 0;
|
||||
|
||||
if(expectedGain < 0.8 && winProbability < 0.8) {
|
||||
// Now the actual AI can happen. This is basically a weight to confidence
|
||||
// ratio. The higher the gains and the confidence then the more likely the AI
|
||||
// is to betting. There are also bluff chances within here.
|
||||
if(expectedGain < 0.8 && confidence < 0.8) {
|
||||
if(random < 95) {
|
||||
amount = 0;//FOLD
|
||||
amount = 0;
|
||||
} else {
|
||||
amount = bluffBet;
|
||||
isBluff = true;
|
||||
}
|
||||
} else if ((expectedGain < 1.0 && winProbability < 0.85) || winProbability < 0.1) {
|
||||
} else if ((expectedGain < 1.0 && confidence < 0.85) || confidence < 0.1) {
|
||||
if (random < 80) {
|
||||
amount = 0;//FOLD
|
||||
amount = 0;
|
||||
} else if(random < 5) {
|
||||
amount = callBet;
|
||||
isBluff = true;
|
||||
@@ -93,13 +115,13 @@ pokerturn_t pokerTurnGet(poker_t *poker, uint8_t playerIndex) {
|
||||
amount = bluffBet;
|
||||
isBluff = true;
|
||||
}
|
||||
} else if ((expectedGain < 1.3 && winProbability < 0.9) || winProbability < 0.5) {
|
||||
if (random < 60 || winProbability < 0.5) {
|
||||
} else if ((expectedGain < 1.3 && confidence < 0.9) || confidence < 0.5) {
|
||||
if (random < 60 || confidence < 0.5) {
|
||||
amount = callBet;
|
||||
} else {
|
||||
amount = maxBet;
|
||||
}
|
||||
} else if (winProbability < 0.95 || poker->dealer.cardsFacing < 0x04) {
|
||||
} else if (confidence < 0.95 || poker->dealer.cardsFacing < 0x04) {
|
||||
if(random < 30) {
|
||||
amount = callBet;
|
||||
} else {
|
||||
@@ -109,21 +131,24 @@ pokerturn_t pokerTurnGet(poker_t *poker, uint8_t playerIndex) {
|
||||
amount = (poker->bet.currentBet - callBet) * 9 / 10;
|
||||
}
|
||||
|
||||
// Let's not get caught in a raising loop with AI.
|
||||
if(player->timesRaised >= POKER_TURN_MAX_RAISES) amount = callBet;
|
||||
|
||||
printf("Raw Amount %i\n", amount);
|
||||
|
||||
// TODO: Make sure we don't get stuck in a raise loop.
|
||||
// Did we actually bet?
|
||||
if(amount > 0) {
|
||||
amount = mathMin(amount, callBet);
|
||||
return pokerTurnRaise(poker, playerIndex, amount);
|
||||
}
|
||||
|
||||
if(pokerTurnCanPlayerCheck(poker, playerIndex)) {
|
||||
return pokerTurnCheck(poker, playerIndex);
|
||||
turn = pokerTurnRaise(poker, playerIndex, amount);
|
||||
turn.confidence = confidence;
|
||||
} else if(pokerTurnCanPlayerCheck(poker, playerIndex)) {
|
||||
turn = pokerTurnCheck(poker, playerIndex);
|
||||
turn.confidence = 1;
|
||||
} else {
|
||||
turn = pokerTurnFold(poker, playerIndex);
|
||||
turn.confidence = 1 - confidence;
|
||||
}
|
||||
|
||||
// Nothing worth doing, so let's just fold here.
|
||||
return pokerTurnFold(poker, playerIndex);
|
||||
|
||||
return turn;
|
||||
}
|
||||
|
||||
void pokerTurnAction(poker_t *poker, pokerplayer_t *player, pokerturn_t *turn) {
|
||||
@@ -132,14 +157,17 @@ void pokerTurnAction(poker_t *poker, pokerplayer_t *player, pokerturn_t *turn) {
|
||||
case POKER_TURN_TYPE_CALL:
|
||||
case POKER_TURN_TYPE_ALL_IN:
|
||||
pokerBetPlayer(&poker->bet, player, turn->chips);
|
||||
player->timesRaised++;
|
||||
break;
|
||||
|
||||
case POKER_TURN_TYPE_CHECK:
|
||||
pokerBetPlayer(&poker->bet, player, 0);
|
||||
player->timesRaised = 0;
|
||||
break;
|
||||
|
||||
case POKER_TURN_TYPE_FOLD:
|
||||
player->state |= POKER_PLAYER_STATE_FOLDED;
|
||||
player->timesRaised = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
#define POKER_TURN_TYPE_CALL_ALL_IN 0x04
|
||||
#define POKER_TURN_TYPE_CHECK 0x05
|
||||
|
||||
#define POKER_TURN_MAX_RAISES 0x02
|
||||
|
||||
/** The turn that a player/the AI decided to do for its turn */
|
||||
typedef struct {
|
||||
/** What type of action the turn is */
|
||||
|
||||
Reference in New Issue
Block a user