-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
185 lines (163 loc) · 5.61 KB
/
main.cpp
File metadata and controls
185 lines (163 loc) · 5.61 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
#include "Player.h"
//Author: luke-chan@qq.com
//GitHub: Luke-Chan
void showresult(Player &player, Deck &controller);
void checkCard(Player &player, Deck &controller);
void DisplayRule();
void main()
{
string playerName = "";
char choice = ' ';
//Game guide.
cout << "\nWelcome to Luke-Chan's BlackJack Game!" <<endl;
cout << "--------------------------------------------" << endl;
cout << "For the rules of this game enter (R/r)" << endl;
cout << "Enter (S/s) to Start Game directly: ";
while (!(cin >> choice) || !(choice == 'R' || choice == 'r'
|| choice == 'S' || choice == 's'))
//Checking the validity of the input, if invalid, input again.
{
cout << "Please input R/r or P/p, try again: ";
cin.clear();
cin.ignore();
}
if (choice == 'R' || choice == 'r')
DisplayRule();
cout << "\nPlease enter your name:";
cin >> playerName;
Deck *Controller = new Deck();
//set up the game controller
Player *player = new Player(playerName, 100, 10);
//money $100, wager $10.
Player *dealer = new Player();
//dealer is also a instance of class player, who has his Hand.
cout << "\nWelcome " << playerName << "!" << endl << endl;
cout << "Now you have $"<< player->money() <<", Game Start." << endl << endl;
system("pause");
srand((unsigned)time(NULL));
//set up the randdom generator
while (!(Controller->IsGameOver()))
//the game start
{
Controller->reset();
//reset the game controller when start a new round.
system("cls");
//clear the screen.
showinfo(*player, *dealer, *Controller);
//show player's money and wager, where player can change wager, deal or quit the game.
if (Controller->IsGameOver())
return;
//player choose to quit the game
while(!(Controller->IsRoundOver() || Controller->IsGameOver()))
//a round
{
system("cls");
gameinfo(*player, *dealer, *Controller);
//show the game information, for example, player and dealer's cards.
gameplay(*player, *dealer, *Controller);
//play a round, where player can Hit or Stand.
checkCard(*player, *Controller);
//Check if all cards are used. game will get to end if all 52 cards are used.
}
if(!(Controller->IsCardOver()))
showresult(*player, *Controller);
//show result after a round, if all cards are used before, no result will be shown, game ended directly.
player->reset(*Controller);
dealer->reset(*Controller);
//reset the card for player and dealer
checkCard(*player, *Controller);
//if the card is not enough to start a new round, game over.
}
system("pause");
}
void checkCard(Player &player, Deck &controller)//check if all 52 cards are used.
{
if (controller.IsCardOver())
{
system("cls");
cout << "\nAll the 52 Cards have been used, Game Over." << endl << endl;
cout << "You have $" << player.money() << " at last." << endl << endl;
controller.GameOver();
}
}
void showresult(Player &player, Deck &controller)
//A Decition Trees to show result, called after finishing a round.
{
if (controller.IsUserWin())
{
player.win();
if (controller.IsUserBJ())
{
cout << "Congratulations! you got a BlackJack and win." << endl << endl;
cout << "You have earned $" << 2 * player.bet() << " in this round." << endl << endl;
}
else{
if (controller.IsDealerBust())
{
cout << "Congratulations! Dealer Bust." << endl << endl;
cout << "You have earned $" << 2 * player.bet() << " in this round." << endl << endl;
}
else {
cout << "Congratulations! you win." << endl << endl;
cout << "You have earned $" << 2 * player.bet() << " in this round." << endl << endl;
}
}
}
if (controller.IsDealerWin())
{
player.lose();
if (controller.IsDealerBJ())
{
cout << "Sorry, dealer got a BlackJack." << endl << endl;
cout << "You have lost your wager in this round." << endl << endl;
}
else {
if (controller.IsUserBust())
{
cout << "Sorry, your card Bust." << endl << endl;
cout << "You have lost your wager in this round." << endl << endl;
}
else {
cout << "Sorry, you lost." << endl << endl;
cout << "You have lost your wager in this round." << endl << endl;
}
}
}
if (controller.IsDraw())
{
if (controller.IsUserBJ() && controller.IsDealerBJ())
{
cout << "Dealer also got a BlackJack, round over." << endl << endl;
}
else{
cout << "You got the same point with dealer, round over." << endl << endl;
}
}
if (player.money() <= 0)
{
controller.GameOver();
cout << "You have lost all your money, Game Over." << endl << endl;
return; //avoid two system pause.
}
system("pause");
}
void DisplayRule()
{
system("cls");
cout << "\nWelcome to Liqi.Chen's BlackJack Game." << endl << endl;
cout << "Key point:" << endl << endl;
cout << "* It is a BlackJack game with only one player, where computer act as a dealer." << endl << endl;
cout << "* There is only one deck of card (52 cards), each card can only be used one time," << endl;
cout << " after all the 52 cards are used, Game Over." << endl << endl;
cout << "* Your property consists of your money and your wager." << endl << endl;
cout << "* Game will over if you loses all your properties." << endl << endl;
cout << "* Dealer will show his hidden card in these cases:" << endl;
cout << " 1. You choose to Stand." << endl;
cout << " 2. Your card Bust." << endl;
cout << " 3. You get a BlackJack." << endl;
cout << " 4. Game Over." << endl << endl;
cout << "Please enjoy the game, have fun!" << endl << endl;
cout << "Game start." << endl << endl;
system("pause");
system("cls");
}