瀏覽代碼

internal

master
gtheler 5 年之前
父節點
當前提交
7f76821559
共有 5 個檔案被更改,包括 146 行新增4 行删除
  1. +1
    -1
      blackjack.conf
  2. +41
    -0
      bs.txt
  3. +2
    -2
      src/conf.cpp
  4. +98
    -1
      src/internal.cpp
  5. +4
    -0
      src/internal.h

+ 1
- 1
blackjack.conf 查看文件

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

+ 41
- 0
bs.txt 查看文件

@@ -0,0 +1,41 @@
# 2 3 4 5 6 7 8 9 T A
h20 s s s s s s s s s s
h19 s s s s s s s s s s
h18 s s s s s s s s s s
h17 s s s s s s s s s s
h16 s s s s s h h h s h
h15 s s s s s h h h h h
h14 s s s s s h h h h h
h13 s s s s s h h h h h
h12 h h s s s h h h h h
h11 d d d d d d d d d h
h10 d d d d d d d d h d
h9 h d d d d h h h h h
h8 h h h h h h h h h h
h7 h h h h h h h h h h
h6 h h h h h h h h h h
h5 h h h h h h h h h h
h4 h h h h h h h h h h

# 2 3 4 5 6 7 8 9 T A
s20 s s s s s s s s s s
s19 s s s s d s s s s s
s18 d d d d d s s h h h
s17 h d d d d h h h h h
s16 h h d d d h h h h h
s15 h h d d d h h h h h
s14 h h h d d h h h h h
s13 h h h h d h h h h h
s12 h h h h d h h h h h

# 2 3 4 5 6 7 8 9 T A
pA y y y y y y y y y y
pT n n n n n n n n n n
p9 y y y y y n y y n n
p8 y y y y y y y y y y
p7 y y y y y y n n n n
p6 y y y y y n n n n n
p5 n n n n n n n n n n
p4 n n n y y n n n n n
p3 y y y y y y n n n n
p2 y y y y y y n n n n

+ 2
- 2
src/conf.cpp 查看文件

@@ -30,8 +30,6 @@
#include <getopt.h>
#include <unistd.h>

// source https://www.walletfox.com/course/parseconfigfile.php

Configuration::Configuration(int argc, char **argv) {
const struct option longopts[] = {
@@ -155,6 +153,8 @@ Configuration::Configuration(int argc, char **argv) {
}

// source https://www.walletfox.com/course/parseconfigfile.php

int Configuration::readConfigFile(std::string filePath, bool mandatory) {
// std::ifstream is RAII, i.e. no need to call close

+ 98
- 1
src/internal.cpp 查看文件

@@ -19,6 +19,9 @@
* along with Libre Blackjack. If not, see <http://www.gnu.org/licenses/>.
*------------------- ------------ ---- -------- -- - - -
*/
#include <iostream>
#include <fstream>
#include <sstream>

#include "conf.h"
#include "blackjack.h"
@@ -26,6 +29,83 @@


Internal::Internal(Configuration &conf) {
hard.resize(21); // 4--20
soft.resize(21); // 12--20
pair.resize(12); // 2--11
for (int value = 0; value < 21; value++) {
hard[value].resize(12);
soft[value].resize(12);
if (value < 12) {
pair[value].resize(12);
}
for (int upcard = 0; upcard < 12; upcard++) {
hard[value][upcard] = Libreblackjack::PlayerActionTaken::None;
soft[value][upcard] = Libreblackjack::PlayerActionTaken::None;
if (value < 12) {
pair[value][upcard] = Libreblackjack::PlayerActionTaken::None;
}
}
}
// TODO: read a default basic strategy
// std::ifstream is RAII, i.e. no need to call close
std::ifstream fileStream("bs.txt");
std::string line;
std::string token;
if (fileStream.is_open()) {
while (getline(fileStream, line)) {
if (line[0] == '#' || line[0] == ';' || line.empty()) {
continue;
}

auto stream = std::istringstream(line);
// TODO: check error
stream >> token;
int value = 0;
std::vector<std::vector<Libreblackjack::PlayerActionTaken>> *strategy = nullptr;
switch (token[0]) {
case 'h':
strategy = &hard;
break;
case 's':
strategy = &soft;
break;
case 'p':
strategy = &pair;
if (token[1] == 'A') {
value = 11;
} else if (token[1] == 'T') {
value = 10;
}
break;
}

if (value == 0) {
value = std::stoi(token.substr(1));
}
for (int upcard = 2; upcard < 12; upcard++) {
// TODO: check error
stream >> token;
if (token == "h") {
(*strategy)[value][upcard] = Libreblackjack::PlayerActionTaken::Hit;
} else if (token == "s") {
(*strategy)[value][upcard] = Libreblackjack::PlayerActionTaken::Stand;
} else if (token == "d") {
(*strategy)[value][upcard] = Libreblackjack::PlayerActionTaken::Double;
} else if (token == "y") {
(*strategy)[value][upcard] = Libreblackjack::PlayerActionTaken::Split;
} else if (token == "n") {
(*strategy)[value][upcard] = Libreblackjack::PlayerActionTaken::None;
}
}
}
}
return;
}

@@ -46,7 +126,24 @@ int Internal::play() {
break;
case Libreblackjack::PlayerActionRequired::Play:
actionTaken = (playerValue < 12) ? Libreblackjack::PlayerActionTaken::Hit : Libreblackjack::PlayerActionTaken::Stand;

std::cout << "player " << playerValue << " dealer " << dealerValue << std::endl;
// TODO: split
// soft
{
std::size_t value = std::abs(playerValue);
std::size_t upcard = std::abs(dealerValue);
actionTaken = (playerValue < 0) ? soft[value][upcard] : hard[value][upcard];
// TODO: double
if (actionTaken == Libreblackjack::PlayerActionTaken::Double) {
actionTaken = Libreblackjack::PlayerActionTaken::Hit;
}
}
std::cout << (int)(actionTaken) << std::endl;
break;
case Libreblackjack::PlayerActionRequired::None:

+ 4
- 0
src/internal.h 查看文件

@@ -33,6 +33,10 @@ class Internal : public Player {
void info(Libreblackjack::Info = Libreblackjack::Info::None, int = 0, int = 0) override;

private:
std::vector<std::vector<Libreblackjack::PlayerActionTaken>> pair;
std::vector<std::vector<Libreblackjack::PlayerActionTaken>> soft;
std::vector<std::vector<Libreblackjack::PlayerActionTaken>> hard;
};


Loading…
取消
儲存