-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack.cpp
More file actions
158 lines (141 loc) · 4.15 KB
/
Copy pathblackjack.cpp
File metadata and controls
158 lines (141 loc) · 4.15 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
#include <string>
#include <time.h>
#include <cstdlib>
#include <string>
#include <iostream>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include "blackjack.h"
using namespace std;
//#################### _21POINT_GAME CLASS IMPLENMENTATIONS####################//
void blackjack::blackjack_welcome(){
srand(time(NULL));
static const int nums_rands = 3;
int rand_start = rand() % nums_rands;
string start_prompt[nums_rands] = {"Bribing the dealer...", "Making AI harder...", "Shuffling cards..."};
cout << start_prompt[rand_start] << endl;
Sleep(1000);
string word = "Start!";
cout << word << endl;
Sleep(500);
}
void blackjack::write_score(int decider){
if ( decider == 1 ){
scores[0]++;
} else if ( decider == -1){
scores[1]++;
} else if ( decider == 0){
scores[2]++;
}
return;
}
void blackjack::welcome(){
card_game game;
cout << "This is a simple balck jack game (Twenty-one game), you will be playing as a player against dealer which controlled by computer." << endl;
cout << "For rules, please go to: https://en.wikipedia.org/wiki/Blackjack" << endl;
cout << endl;
string prvent_crush;
cout << "Enter 'start' to start the game: ";
cin >> prvent_crush;
while (prvent_crush != "start")
{
cout << "Enter 'start' to start the game: ";
cin >> prvent_crush;
}
}
void blackjack::refresh_card(int* handcard){
for (int i = 0; i < 5; i++){
handcard[i] = 0;
}
}
//return 1 for blow
bool blackjack::checkif_blow(int* handcard, int count){
card_game A;
int sum = A.get_sum(handcard, count);
if (sum > 21){
return 1;
}
return 0;
}
//check if five, return 1 for five cards have been drawn
bool blackjack::checkif_five(int count){
if (count >= 4){
return 1;
}
return 0;
}
bool blackjack::checkif_21(int* handcard, int count){
card_game A;
if (A.get_sum(handcard,count) == 21){
return true;
}
return false;
}
//check if any Ace exists
//return 1 for yes
bool blackjack::checkif_Ace_exists(int* handcard, int count){
for (int i = 0; i < count; i++){
if (handcard[i] == 1){
return 1;
}
}
return 0;
}
//winner deciding function, this is triggered when comparison is called
//return 1 for player wins, -1 for computer wins and 0 for draw
int blackjack::decide_winner(int player_sum, int computer_sum){
//when both player and computer is under 21 point
if (player_sum <= 21 && computer_sum <= 21){
if (player_sum > computer_sum){
cout << endl;
cout << "You have won this round!" << endl;
cout << endl;
return 1;
} else if (player_sum == computer_sum){
cout << endl;
cout << "This round was a draw"<< endl;
cout << endl;
return 0;
} else if (player_sum < computer_sum){
cout << endl;
cout << "You have lost this round"<< endl;
cout << endl;
return -1;
}
}
//when player blow (over 21 point), and computer is blow 21
if (player_sum > 21 && computer_sum <= 21){
cout << endl;
cout << "You have lost this round"<< endl;
cout << endl;
return -1;
}
//when computer blow and player under 21
if (player_sum <= 21 && computer_sum > 21){
cout << endl;
cout << "You have won this round"<< endl;
cout << endl;
return 1;
}
//when both blow
if (player_sum > 21 && computer_sum > 21){
cout << endl;
cout << "This round was a draw"<< endl;
cout << endl;
return 0;
}
return 0;
}
void blackjack::print_score(){
cout << endl;
cout << endl;
cout << "========================= Scores ========================" << endl;
cout << "Player: " << scores[0] << endl;
cout << "Computer: " << scores[1] << endl;
cout << "Draw: " << scores[2] << endl;
cout << endl;
return;
}