gtheler 5 anni fa
parent
commit
a19e0f0144
5 ha cambiato i file con 33 aggiunte e 10 eliminazioni
  1. +2
    -2
      blackjack.conf
  2. +1
    -0
      src/base.h
  3. +11
    -1
      src/blackjack.cpp
  4. +2
    -2
      src/internal.cpp
  5. +17
    -5
      src/tty.cpp

+ 2
- 2
blackjack.conf Vedi File

@@ -1,5 +1,5 @@
flat_bet = 1
no_insurance = true
; delay = 0
arranged_cards = 1,1,10,5
delay = 0
arranged_cards = 2,1,11
; rng_seed = 1

+ 1
- 0
src/base.h Vedi File

@@ -340,5 +340,6 @@ class Dealer {
};

template <typename ... Args> std::string string_format( const std::string& format, Args ... args);
template <typename ... Args> std::string string_format2( const std::string& format, Args ... args);

#endif

+ 11
- 1
src/blackjack.cpp Vedi File

@@ -128,6 +128,7 @@ void Blackjack::deal(void) {
}

info(Libreblackjack::Info::NewHand, n_hand, 1e3*playerStats.bankroll);
// std::cout << "new hand #" << n_hand << std::endl;
if (player->flat_bet) {
@@ -160,15 +161,19 @@ void Blackjack::deal(void) {
// step 3. deal the first card to each player
playerFirstCard = drawCard(&(*playerStats.currentHand));
info(Libreblackjack::Info::CardPlayer, playerFirstCard);
// std::cout << "first card " << card[playerFirstCard].utf8() << std::endl;
// step 4. show dealer's upcard
upCard = drawCard(&hand);
info(Libreblackjack::Info::CardDealer, upCard);
// std::cout << "up card " << card[upCard].utf8() << std::endl;
player->dealerValue = hand.value();

// step 5. deal the second card to each player
playerSecondCard = drawCard(&(*playerStats.currentHand));
info(Libreblackjack::Info::CardPlayer, playerSecondCard);
player->playerValue = playerStats.currentHand->value();
// std::cout << "second card " << card[playerSecondCard].utf8() << std::endl;
// step 6. deal the dealer's hole card
holeCard = drawCard(&hand);
@@ -198,7 +203,6 @@ void Blackjack::deal(void) {
}
// step 7.b. if either the dealer or the player has a chance to have a blackjack, check
player->playerValue = playerStats.currentHand->value();
if ((card[upCard].value == 10 || card[upCard].value == 11) || std::abs(player->playerValue) == 21) {
player->actionRequired = Libreblackjack::PlayerActionRequired::None;
nextAction = Libreblackjack::DealerAction::CheckforBlackjacks;
@@ -217,6 +221,7 @@ void Blackjack::deal(void) {
if (hand.blackjack()) {
info(Libreblackjack::Info::CardDealerRevealsHole, holeCard);
info(Libreblackjack::Info::DealerBlackjack);
// std::cout << "dealer blakjack " << card[holeCard].utf8() << std::endl;
playerStats.blackjacksDealer++;

if (playerStats.currentHand->insured) {
@@ -231,6 +236,7 @@ void Blackjack::deal(void) {

if (playerBlackjack) {
info(Libreblackjack::Info::PlayerBlackjackAlso);
// std::cout << "both blackjack " << card[holeCard].utf8() << std::endl;

// give him his (her her) money back
playerStats.bankroll += playerStats.currentHand->bet;
@@ -290,6 +296,7 @@ void Blackjack::deal(void) {
unsigned int playerCard = drawCard(&(*playerStats.currentHand));
player->playerValue = playerStats.currentHand->value();
info(Libreblackjack::Info::CardPlayer, playerCard, playerStats.currentHand->id);
// std::cout << "card player " << card[playerCard].utf8() << std::endl;

if (std::abs(player->playerValue) == 21) {
player->actionRequired = Libreblackjack::PlayerActionRequired::None;
@@ -313,6 +320,7 @@ void Blackjack::deal(void) {

if (bustedAllHands) {
info(Libreblackjack::Info::CardDealerRevealsHole, holeCard);
// std::cout << "hole " << card[holeCard].utf8() << std::endl;
player->actionRequired = Libreblackjack::PlayerActionRequired::None;
nextAction = Libreblackjack::DealerAction::StartNewHand;
@@ -328,12 +336,14 @@ void Blackjack::deal(void) {
case Libreblackjack::DealerAction::HitDealerHand:
info(Libreblackjack::Info::CardDealerRevealsHole, holeCard);
// std::cout << "hole " << card[holeCard].utf8() << std::endl;

// hit while count is less than 17 (or equal to soft 17 if hit_soft_17 is true)
player->dealerValue = hand.value();
while ((std::abs(player->dealerValue) < 17 || (hit_soft_17 && player->dealerValue == -17)) && hand.busted() == 0) {
unsigned int dealerCard = drawCard(&hand);
info(Libreblackjack::Info::CardDealer, dealerCard);
// std::cout << "dealer " << card[dealerCard].utf8() << std::endl;
player->dealerValue = hand.value();
}

+ 2
- 2
src/internal.cpp Vedi File

@@ -127,7 +127,7 @@ int Internal::play() {
case Libreblackjack::PlayerActionRequired::Play:

std::cout << "player " << playerValue << " dealer " << dealerValue << std::endl;
// std::cout << "player " << playerValue << " dealer " << dealerValue << std::endl;
// TODO: split
@@ -142,7 +142,7 @@ int Internal::play() {
actionTaken = Libreblackjack::PlayerActionTaken::Hit;
}
}
std::cout << (int)(actionTaken) << std::endl;
// std::cout << (int)(actionTaken) << std::endl;
break;

+ 17
- 5
src/tty.cpp Vedi File

@@ -36,6 +36,18 @@
// TODO: make class static
std::vector<std::string> commands;

// https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
template<typename ... Args>
std::string string_format2( const std::string& format, Args ... args)
{
size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1; // Extra space for '\0'
if (size <= 0) {
return std::string("");
}
std::unique_ptr<char[]> buf(new char[size]);
snprintf(buf.get(), size, format.c_str(), args ...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}

Tty::Tty(Configuration &conf) {
@@ -106,7 +118,7 @@ void Tty::info(Libreblackjack::Info msg, int p1, int p2) {

case Libreblackjack::Info::NewHand:
std::cout << std::endl;
s = "Starting new hand #" + std::to_string(p1) + " with bankroll " + string_format("%g", 1e-3*p2, 0.0);
s = "Starting new hand #" + std::to_string(p1) + " with bankroll " + string_format2("%g", 1e-3*p2);
// clear dealer's hand
dealerHand.cards.clear();
@@ -226,12 +238,12 @@ void Tty::info(Libreblackjack::Info msg, int p1, int p2) {
break;
case Libreblackjack::Info::PlayerPushes:
s = "Player pushes " + string_format("%g", 1e-3*p1, 0.0) + ((p2 > 0) ? (" with " + std::to_string(p2)) : "");
s = "Player pushes " + string_format2("%g", 1e-3*p1) + ((p2 > 0) ? (" with " + std::to_string(p2)) : "");
render = true;
break;
case Libreblackjack::Info::PlayerLosses:
s = "Player losses " + string_format("%g", 1e-3*p1, 0.0) + ((p2 > 0) ? (" with " + std::to_string(p2)) : "");
s = "Player losses " + string_format2("%g", 1e-3*p1) + ((p2 > 0) ? (" with " + std::to_string(p2)) : "");
render = true;
break;
case Libreblackjack::Info::PlayerBlackjack:
@@ -239,7 +251,7 @@ void Tty::info(Libreblackjack::Info msg, int p1, int p2) {
render = true;
break;
case Libreblackjack::Info::PlayerWins:
s = "Player wins " + string_format("%g", 1e-3*p1, 0.0) + ((p2 > 0) ? (" with " + std::to_string(p2)) : "");
s = "Player wins " + string_format2("%g", 1e-3*p1) + ((p2 > 0) ? (" with " + std::to_string(p2)) : "");
render = true;
break;
@@ -256,7 +268,7 @@ void Tty::info(Libreblackjack::Info msg, int p1, int p2) {
break;

case Libreblackjack::Info::Bankroll:
std::cout << "Your bankroll is " << string_format("%g", 1e-3*p1, 0.0) << std::endl;
std::cout << "Your bankroll is " << string_format2("%g", 1e-3*p1) << std::endl;
break;

Loading…
Annulla
Salva