-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·208 lines (180 loc) · 4.84 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·208 lines (180 loc) · 4.84 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "engine.hpp"
#include <ncurses.h>
#include <vector>
#include <iostream>
namespace {
WindowConfig config;
}
void setup() {
initscr();
clear();
noecho();
raw();
start_color();
curs_set(0);
refresh();
int rows{};
int columns{};
getmaxyx(stdscr, rows, columns);
config.BoardwindowPositionY = rows / 2 - BOARD_HEIGHT / 2;
config.BoardwindowPositionX = columns / 2 - BOARD_WIDTH / 2;
config.verticallyCentered = config.BoardwindowPositionY + (BOARD_HEIGHT / 2);
}
void drawText(int y, int width, const std::string_view& text) {
mvaddstr(
y,
config.BoardwindowPositionX + (width - text.length()) / 2,
text.data()
);
refresh();
}
void setupDrawUI() {
drawText(
config.BoardwindowPositionY - 1,
BOARD_WIDTH,
UI::StaticText::dealer
);
drawText(
config.BoardwindowPositionY + BOARD_HEIGHT,
BOARD_WIDTH,
UI::StaticText::player
);
drawText(
config.BoardwindowPositionY + BOARD_HEIGHT - 2,
BOARD_WIDTH,
UI::EventMessages::yourTurn
);
mvaddstr(
config.BoardwindowPositionY + BOARD_HEIGHT + 1,
config.BoardwindowPositionX,
UI::controlPrompts::hit.data()
);
mvaddstr(
config.BoardwindowPositionY + BOARD_HEIGHT + 1,
config.BoardwindowPositionX + BOARD_WIDTH - UI::controlPrompts::stand.length(),
UI::controlPrompts::stand.data()
);
refresh();
}
void drawWindowUI(WINDOW *window, const std::vector<int>& hand, bool isDealer, bool hide = false) {
int y{isDealer ? 0 : BOARD_HEIGHT - 1};
std::string_view score{UI::displayScore(hand, hide)};
mvwaddstr(
window,
y,
(BOARD_WIDTH - UI::StaticText::score.length()) / 2 - 1, // The '-1' centers the static score string with the dynamic score.
score.data()
);
wrefresh(window);
}
void displayHand(const std::vector<int>& hand, bool isDealer, bool hideFirstCard) {
// Draw the virtual cards on the board when I have the willpower to implement this
}
bool evaluateTurn(WINDOW *window, std::vector<int>& hand) {
while (true) {
switch (getch()) {
case 'H':
[[fallthrough]];
case 'h':
hand.push_back(drawCard());
break;
case 'S':
[[fallthrough]];
case 's':
return false;
break;
default:
break;
}
drawWindowUI(window, hand, false);
if (calculateScore(hand) > 21) {
return true;
}
}
}
void gameOverEvents(const std::vector<int>& playerHand, const std::vector<int>& dealerHand, bool playerBusted, WINDOW *window) {
if (playerBusted) {
drawWindowUI(window, dealerHand, true);
drawText(config.verticallyCentered - 1, BOARD_WIDTH, UI::EventMessages::bust);
drawText(config.verticallyCentered, BOARD_WIDTH, UI::EventMessages::lose);
}
if (calculateScore(dealerHand) > 21) {
drawText(config.verticallyCentered - 1, BOARD_WIDTH, UI::EventMessages::dealerBust);
drawText(config.verticallyCentered, BOARD_WIDTH, UI::EventMessages::win);
} else if (calculateScore(playerHand) > calculateScore(dealerHand) && !playerBusted) {
drawText(config.verticallyCentered, BOARD_WIDTH, UI::EventMessages::win);
} else if (calculateScore(playerHand) == calculateScore(dealerHand) && !playerBusted) {
drawText(config.verticallyCentered, BOARD_WIDTH, UI::EventMessages::push);
} else {
drawText(config.verticallyCentered, BOARD_WIDTH, UI::EventMessages::lose);
}
}
bool replayPrompt() {
drawText(
config.verticallyCentered + 1,
BOARD_WIDTH,
UI::EventMessages::rematchPrompt
);
mvaddstr(
config.verticallyCentered + 2,
config.BoardwindowPositionX + 3,
UI::controlPrompts::yes.data()
);
mvaddstr(
config.verticallyCentered + 2,
config.BoardwindowPositionX + BOARD_WIDTH - UI::controlPrompts::no.length() - 3,
UI::controlPrompts::no.data()
);
switch(getch()) {
case 'Y':
[[fallthrough]];
case 'y':
return true;
break;
case 'N':
[[fallthrough]];
case 'n':
return false;
break;
default:
return false;
break;
}
}
int main() {
setup();
bool playAgain{true};
// Game Loop
while (playAgain) {
WINDOW *boardWin = newwin(BOARD_HEIGHT, BOARD_WIDTH, config.BoardwindowPositionY , config.BoardwindowPositionX);
box(boardWin, 0, 0);
wrefresh(boardWin);
setupDrawUI();
std::vector<int> playerHand;
std::vector<int> dealerHand;
// Initial Draw
playerHand.push_back(drawCard());
playerHand.push_back(drawCard());
dealerHand.push_back(drawCard());
dealerHand.push_back(drawCard());
drawWindowUI(boardWin, playerHand, false);
drawWindowUI(boardWin, dealerHand, true, true);
bool playerBusted{evaluateTurn(boardWin, playerHand)};
// Dealer's turn
if (!playerBusted) {
if (calculateScore(dealerHand) >= 17) {
drawText(config.BoardwindowPositionY + 1, BOARD_WIDTH, UI::EventMessages::dealerStands);
}
while (calculateScore(dealerHand) < 17) {
dealerHand.push_back(drawCard());
}
drawWindowUI(boardWin, dealerHand, true);
}
gameOverEvents(playerHand, dealerHand, playerBusted, boardWin);
playAgain = replayPrompt();
delwin(boardWin);
}
endwin();
std::cout << "Game Over!" << std::endl;
return 0;
}