-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab9.cpp
More file actions
206 lines (166 loc) · 4.91 KB
/
lab9.cpp
File metadata and controls
206 lines (166 loc) · 4.91 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
//Name: Autumn Henderson
//Date: October 31st, 2018
//Title: Lab 9
//Description: Program simulates a Blackjack game where you and a dealer are
//randomly dealt two cards and play Blackjack until one of you wins.
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
//Functions created in program
int GetRandom (int minimum, int maximum);
string DealCard (int cards[], int player);
int ScoreHand (int cards[], int player);
void InitializeCards (int cards[]);
int main() {
//Seeds rand
srand(time(NULL));
//Variables for game
int cards[52];
string answer;
bool decided = false;
//Player Variables
int playerNum = 1;
string playerCardType;
int playerScore;
//Dealer Variables
int dealerNum = 2;
string dealerCardType;
int dealerScore;
//Initializes array of cards to 0
InitializeCards(cards);
//Starts the game for the player and dealer (ONLY RUNS ONCE!)
dealerCardType = DealCard(cards, dealerNum) + " " + DealCard(cards, dealerNum);
dealerScore = ScoreHand(cards, dealerNum);
cout << "Dealer has cards: " << left << setw(20) << dealerCardType << "(" << dealerScore
<< ")" << "\n";
playerCardType = DealCard(cards, playerNum) + " " + DealCard(cards, playerNum);
playerScore = ScoreHand(cards, playerNum);
cout << "You have cards : " << left << setw(20) << playerCardType << "(" << playerScore
<< ")" << "\n";
//Player Loop
do {
//Will ask player to hit or stand until valid response given
do {
cout << "Hit or stand ? ";
cin >> answer;
} while (!(answer == "hit" || answer == "stand"));
cout << "\n";
//If statement allows player to either keep playing or adds score
if (answer == "stand") {
break;
}
//Else statement adds scores and determines whether to ask hit or stand again.
else {
playerCardType += " " + DealCard(cards, playerNum);
playerScore = ScoreHand(cards, playerNum);
cout << "Dealer has cards: " << left << setw(20) << dealerCardType << "(" << dealerScore
<< ")" << "\n";
cout << "You have cards : " << left << setw(20) << playerCardType << "(" << playerScore
<< ")" << "\n";
//Player has automatically lost
if (playerScore > 21) {
cout << "Player busts, dealer wins! \n";
decided = true;
break;
}
//Player has automatically won
else if (playerScore == 21) {
cout << "Player wins!\n";
decided = true;
break;
}
}
} while(true);
//Dealer loop
if ((answer == "stand") && (decided == false)) {
do {
//Dealer will hit
if (dealerScore < 18) {
dealerCardType += " " + DealCard(cards, dealerNum);
dealerScore = ScoreHand(cards, dealerNum);
cout << "Dealer hits : " << left << setw(20) << dealerCardType << "(" <<
dealerScore << ")\n";
}
//Dealer will stand
else if ((dealerScore > 17) && (dealerScore < 21)) {
cout << "Dealer stands : " << left << setw(20) << dealerCardType << "(" <<
dealerScore << ")\n";
break;
}
//Dealer has automatically won
else if (dealerScore == 21) {
cout << "Dealer wins!\n";
decided = true;
break;
}
//Dealer has automatically lost
else {
cout << "Dealer busts, player wins!\n";
decided = true;
break;
}
} while(true);
}
//Decide winner
if (decided == false) {
if (dealerScore == playerScore) {
cout << "Player and dealer draw.\n";
}
else if (dealerScore > playerScore) {
cout << "Dealer wins!\n";
}
else {
cout << "Player wins!\n";
}
}
return 0;
}
//Initializes array to 0
void InitializeCards (int cards[]) {
for (unsigned int i = 0; i < 52; ++i)
cards[i] = 0;
}
//GetRandom generates a number between 0 and 51
int GetRandom (int minimum, int maximum) {
return rand() % ((maximum - minimum) + 1);
}
//DealCard takes an array of 52 cards and whichever player the program is on
//and deals two cards to the player.
string DealCard (int cards[], int player) {
int randomCard;
int minimum = 0;
int maximum = 51;
const string cardValue[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
const string cardSuit[4] = {"H", "D", "S", "C"};
//Checks to see if card has been dealt
do {
randomCard = GetRandom(minimum, maximum);
if (cards[randomCard] == 0)
break;
} while (true);
//Assigns player to an index
cards[randomCard] = player;
//Converts index into string of card types
return cardValue[(randomCard + 13) % 13] + cardSuit[randomCard / 13];
}
//ScoreHand takes an array of 52 cards and whichever player the program
//is on and adds the player's cards together to generate a score.
int ScoreHand (int cards[], int player) {
int score = 0;
//For loop runs through the array to find where the player number is.
for (unsigned int i = 0; i < 52; ++i) {
if (cards[i] == player) {
//If the card is 10, J, Q, or K, gets the value 10.
if ((((i+13) % 13) +1) > 9) {
score += 10;
}
//Else the value is 1 - 9
else {
score += (((i + 13) % 13) + 1);
}
}
}
return score;
}