-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21(v1).cpp
201 lines (176 loc) · 5.11 KB
/
21(v1).cpp
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
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
void wash(int allcard[52], int playercard[22], int computercard[22]);
int give_out_card(int allcard[52],int card);
void print_card(int card);
void print_cards(int cards[22]);
bool judge(int sum);
int final_judge(float computer_sum, float player_sum, int computer_number, int player_number);
char input_and_check();
int main()
{
int allcard[52] = {0};
bool turn = 0;
bool flag = 0;
int playercard[22] = {0};
int computercard[22]={0};
int player_card_number = 0,computer_card_number = 0;
float player_sum = 0,computer_sum = 0;
int card;
char whether_card;
for(int i=0;i<52;i++) allcard[i]=i;
while (1)
{
cout<<"want to play the game?(y/n)"; //initialization
turn = input_and_check();
if (turn == 'n') break;
wash(allcard, playercard, computercard);
player_card_number = 0;
computer_card_number = 0;
player_sum = 0;
computer_sum = 0;
cout<<"player turn" << endl; //deal 2 cards out and print it
cout<<"player get the cards: " << endl;
for(int i = 0;i < 2;i++)
{
card = give_out_card(allcard,i);
playercard[i] = card;
if (card % 13 > 10 || card % 13 == 0) player_sum += 0.5;
else player_sum += card % 13;
player_card_number++;
}
print_cards(playercard);
if (judge(player_sum)) //check whether the player busts
{
cout << " you lose" << endl;
continue;
}
while (1)
{
flag = 0;
cout << " another card?(y/n)"; //take an additional card or get a 'hit'
whether_card = input_and_check();
if (whether_card == 'n') break;
card = give_out_card(allcard, player_card_number);
print_card;
playercard[player_card_number] = card;
if (card % 13 > 10 || card % 13 == 0) player_sum += 0.5;
else player_sum += card % 13;
player_card_number++;
print_cards(playercard);
if (judge(player_sum)) //check whether the player busts
{
cout << endl << "you lose" << endl;
flag = 1;
break;
}
}
if (flag)
{
continue;
flag=0;
}
while (computer_sum < player_sum) //dealer's turn
{
card=give_out_card(allcard, computer_card_number + player_card_number);
computercard[computer_card_number] = card; //get an additional card if the number doesn't exceed the player's
if (card%13 > 10 || card % 13 == 0)computer_sum += 0.5;
else computer_sum += card%13;
computer_card_number++;
if (judge(computer_sum))
{
print_cards(computercard);
cout << "you win" << endl; //check whether the dealer busts
flag = 1;
break;
}
}
if (flag)
{
continue;
flag=0;
}
print_cards(computercard);
cout<<"\n";
switch(final_judge(computer_sum, player_sum, computer_card_number, player_card_number))
{
case 0:cout << "equal" << endl;break;
case 1:cout << "you win" << endl;break;
case 2:cout << "you lose" << endl;break; //print the results
}
}
}
char input_and_check() //get the input and check it, return'y' or'n'
{
char a[10];
while (cin.getline(a,2))
{
try
{
if ((a[0]!='y' && a[0]!='n') || (a[1] != '\0' && a[1]!= '\n')) throw 1;
cin.clear();
return a[0];
}
catch(int)
{
cout << "Error occured." << endl;
}
cout << "please input again." << endl;
}
cout << endl;
}
void wash(int allcard[52], int playercard[22], int computercard[22]) //get the cards back and shuffle the cards
{
int i;
srand(time(NULL));
random_shuffle(allcard,allcard+52);
for (i = 0;i < 22 && playercard[i] != 0; i++) playercard[i] = 0;
for (i = 0;i < 22 && computercard[i] != 0; i++) computercard[i] = 0;
}
int give_out_card(int allcard[52], int i) //deal a card
{
return allcard[i] + 1;
}
void print_card(int card) //print a single card
{
switch(card/13)
{
case 0:cout << " spade "; break;
case 1:cout << " hearts "; break;
case 2:cout << " diamond "; break;
case 3:cout << " club "; break;
default:break;
}
switch(card % 13)
{
case 1:cout << "A"; break;
case 11:cout << "J"; break;
case 12:cout << "Q"; break;
case 0:cout << "K"; break;
default:cout << card % 13; break;
}
}
void print_cards(int cards[22]) //print a set of cards
{
for(int i = 0; cards[i] != 0;i++)
{
print_card(cards[i]);
}
cout << endl;
}
bool judge(int sum) //judge whether anyone busts
{
if (sum > 21) return 1;
else return 0;
}
int final_judge(float computer_sum, float player_sum, int computer_number, int player_number)
{
if (computer_sum>player_sum) return 2; //judge the final results
else if (computer_sum<player_sum) return 1;
if (computer_number>player_number) return 2;
else if (computer_number<player_number) return 1;
return 0;
}