-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
executable file
·57 lines (50 loc) · 1.08 KB
/
Copy pathengine.cpp
File metadata and controls
executable file
·57 lines (50 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "engine.hpp"
#include <random>
#include <sstream>
#include <iomanip>
// Seeding of random number generator
namespace {
std::mt19937 mt{};
bool seeded{false};
void initRandom() {
if (seeded)
return;
mt.seed(std::random_device{}());
seeded = true;
}
}
namespace UI {
std::string displayScore(const std::vector<int>& hand, bool hidden) {
int score{::calculateScore(hand)};
std::ostringstream oss;
oss << StaticText::score;
if (hidden) {
oss << "??";
} else {
oss << std::setw(2) << std::setfill('0') << score;
}
return oss.str();
}
}
int drawCard() {
initRandom();
std::uniform_int_distribution<> drawnCard{0, 12};
int index{drawnCard(mt) % 13};
return cards[index].cardValue;
}
int calculateScore(const std::vector<int>& hand) {
int score{};
int aceCount{};
for (int card : hand) {
score += card;
if (card == 11)
aceCount++;
}
// If 2 Aces are drawn --- Known Bug: Score can go down when you hit.
// Plan to address it if I get to splitting and move away from ncurses
while (score > 21 && aceCount > 0) {
score -= 10;
aceCount--;
}
return score;
}