-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
273 lines (252 loc) · 8.62 KB
/
game.cpp
File metadata and controls
273 lines (252 loc) · 8.62 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* File: game.cpp
* Author: James Fregeau
* Created December 11th, 2025, 4:24 PM
* Actaul game logic class
*/
//Libraries
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
//Headers
#include "saveToFile.h"
#include "game.h"
//Global Constants
//Default file name
const string fileName = "out.txt";
//Value the dealer will hit until it hand value is greater
const int DEALERSTANDVAL = 17;
//Seeds
const int NORMAL = time(0);//Random
const int TESTSPLIT = 14; //Starts can split
const int TESTBLACKJACK= 17; //Starts with blackJack. Can test the payout and ace compression
//Constructor
Game::Game(Player *player){
//Sets the seed
srand(NORMAL);
//Store player in the game object for easy access
this->player = player;
//Create deck
deck = new Deck();
//Can add a load dealer later to have multiple dealers
dealer = new Dealer(DEALERSTANDVAL, "Logan");
dealer->introduce();
//Aggregation, deck can exist without the same player or dealer. This is for multiplier if added.
//Sets the current deck for each hand for easy access
player->hand->setCurrDeck(deck);
player->splitHand->setCurrDeck(deck);
dealer->hand->setCurrDeck(deck);
resetAllGameVars();
}
//deconstructor
Game :: ~Game(){
delete dealer;
delete deck;
}
//A group function for the begining of the game //idk if these two funcs are necassary but condense stuff and group it in a understanable name
bool Game::startGame(){
player->placeBet();
//Game logic here
gameLoop();
return true;
}
//A group function
void Game :: gameLoop(){
//Dish out cards+
starterCards();
playerTurn();
if(!player->hasLost){
dealerTurn();
}else printHands();
payOut();
//Saves the round and prints it to file
Round rnd = Round(player, dealer);
SaveToFile::save(fileName, player, rnd);
//Resets vars for next game
resetAllGameVars();
}
//Prints all player options based on flags. ONLY PRINTS DOESNT HANDLE THE LOGIC
void Game :: printPlayerDecisionTree(){
printHands();
if(!player->hasLost){
if(!player->hasSplit){
cout<<"What will you do? "<<endl;
}else if(player->currHand == 1){
cout<<"What will you do for the first hand"<<endl;
}else{
cout<<"What will you do for the second hand"<<endl;
}
cout<<"Hit/stand";
if(!player->hasHit){
//Can only double if player has enough money and they havent hit yet
if(player->balance >=player->bet*2){
cout<<"/double";
player->canDouble = true;
}//Split
if(player->hand->getCard(0)->getValue()==player->hand->getCard(1)->getValue()){
cout<<"/split";
player->canSplit = true;
}else{
player->canSplit = false;
}
player->hasHit = true;
}else player->canDouble = false;
}else{
cout<<"You have busted. "<<endl;
}
}
//Handles all the players turn logic and loop
void Game :: playerTurn(){
string userInp;
do{
//Prints what the player can do based on the players flags
printPlayerDecisionTree();
cout<<endl;
bool acceptedInp = false;
//Actual player decison logic loop
do{
cin>>userInp;
if(player->canDouble && (userInp =="Double"||userInp=="double")){
//Double logic
acceptedInp = true;
player->hitDouble();
}else if(player->canSplit == true &&(userInp =="Split"||userInp=="split")){
//Split logic
acceptedInp = true;
player->split();
}else if(userInp=="hit"||userInp=="Hit"){
//Hit logic sets flags for options that are no longer possible
acceptedInp = true;
player->hasHit = true;
player->canDouble = false;
player->canSplit = false;
player->nat21 = false;
//Makes sure the right hand is hit if the player has split
if(player->currHand==1) player->hand->hit();
else player->splitHand->hit();
}else if(userInp=="Stand"||userInp=="stand"){
//Stand logic
acceptedInp = true;
//If stand while split go next hand else start the endRound or move next hand
player->stand();
}
}while(!acceptedInp);
checkPlayerStatus();
}while(!player->endRound);
}
//Handles all the dealers turn logic and loop if player hasn't lost
void Game :: dealerTurn(){
//Holds players best hand val that is still playable
int bestPlrHandVal;
dealer->hidden =false;
if(player->nat21){
bestPlrHandVal = 21;
}else if(player->hasSplit){
//Compares the split hands and assigns the best one
if(player->hand->getHandValue()>player->splitHand->getHandValue() && player->hand->getBusted()==false){
bestPlrHandVal = player->hand->getHandValue();
}else{
bestPlrHandVal = player->splitHand->getHandValue();
}
}else{
//Regualr assignment
bestPlrHandVal = player->hand->getHandValue();
}
//Dealer plays until over the stand val or if the players value is higher than dealers
printHands();
while(dealer->hand->getHandValue()<dealer->standVal || bestPlrHandVal >dealer->hand->getHandValue()){
dealer->hand->hit();
dealer->hand->checkBusted();
printHands();
}
//Logic for player winnings/lossings
if(bestPlrHandVal==dealer->hand->getHandValue())player->push =true;
else if(dealer->hand->getBusted()) player->hasLost = false;
else if(bestPlrHandVal <dealer->hand->getHandValue())player->hasLost =true;
}
//Prints both players hands and also handles split hand output
void Game :: printHands(){
cout <<"=================================="<< endl;
cout <<"Dealer's Hand:"<<endl;
//If the dealers turn hasnt started
if(dealer->hidden){
dealer->hand->printFirstCard();
}else{
dealer->hand->printHand();
}
cout <<"----------------------------------"<< endl;
//Prints players hands for split and regular
if(player->hasSplit){
cout<<"Player's hands"<<endl;
player->printSplitHand();
}else{
cout<<"Player's hand"<<endl;
player->hand->printHand();
}
cout <<"=================================="<< endl;
}
//Resets the variables so eveything works correctly
void Game :: resetAllGameVars(){
player->resetPlayerVars();
dealer->resetDealerVars();
}
//Decides amount to pay player based on if they won or lost or got a blackjack
void Game :: payOut(){
float multi, temp;
++player->sTotal;
//Assigns the correct multi for payout
if(player->push){
cout<<"Push. Your money is returned. Total returned: $"<<player->bet<<endl;
cout<<"Balance: $"<<player->balance<<endl;
return;
}else if(!player->hasLost){
if(player->nat21){
multi = 1.5;
++player->sBlackJacks;
}else multi =1;
++player->sWins;
temp = player->bet;
cout<<"Congratulations YOU WON! Total Winnings: $";
}else{
++player->sLoses;
multi =-1;
cout<<"You lose. Losings: $";
temp = 0;
}
player->payOut+= player->bet * multi;
//actually adds/subtracts payout.
if(!player->push)player->balance+=player->payOut;
cout<<player->payOut+temp<<endl<<"Balance: $"<<player->balance<<endl;
}
//Hands out the two set of starter cards
void Game :: starterCards(){
player->hand->hit();
player->hand->hit();
dealer->hand->hit();
dealer->hand->hit();
//Sets the flag for incresed payout if player hit blackjack
if(player->hand->getHandValue()==21)player->nat21 =true;
}
//Sets game flags for currhand and has lost
void Game :: checkPlayerStatus(){
//If player hasnt split and busted
if(!player->hasSplit && player->hand->getBusted()){
player->hasLost =true;
player->endRound = true;
}else{
//If player has split and lost both hands
if (player->hand->getBusted() && player->splitHand->getBusted()){
player->hasLost ==true;
//If players first hand busts move them to second hand
}else if(player->hand->getBusted()&&player->currHand==1)player->currHand =2;
//if the second hand busts end the round
else if(player->splitHand->getBusted()){
player->endRound = true;
}
}
}