-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21( random ).cpp
201 lines (179 loc) · 5.14 KB
/
21( random ).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>
using namespace std;
void wash(bool allcard[4][13], int playercard[22][2], int computercard[22][2]);
int give_out_card(bool allcard[4][13],int card[2]);
void print_card(int card[2]);
void print_cards(int cards[22][2]);
bool judge(int sum);
int final_judge(int computer_sum, int player_sum, int computer_number, int player_number);
char input_and_check();
int main()
{
bool allcard[4][13] = {0};
bool turn = 0;
bool flag = 0;
int playercard[22][2] = {0};
int computercard[22][2];
int player_card_number = 0,computer_card_number = 0;
int player_sum = 0,computer_sum = 0;
int card[2];
char whether_card;
while (1)
{
flag=0; //initialization
cout<<"want to play the game?";
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++)
{
give_out_card(allcard, card);
playercard[i][0] = card[0];
playercard[i][1] = card[1];
if (card[1]>10)player_sum+=0.5;
else player_sum+=card[1];
player_card_number++;
}
print_cards(playercard);
if (judge(player_sum)) //check whether the player busts
{
cout << " you lose" << endl;
continue;
}
while (1)
{
cout << " another card?(y/n)"; //take an additional card or get a 'hit'
whether_card = input_and_check();
if (whether_card == 'n') break;
give_out_card(allcard,card);
print_card;
playercard[player_card_number][0] = card[0];
playercard[player_card_number][1] = card[1];
if (card[1] > 10)player_sum += 0.5;
else player_sum += card[1];
player_card_number++;
print_cards(playercard); //check whether the player busts
if (judge(player_sum))
{
cout << endl << "you lose" << endl;
flag = 1;
break;
}
}
if (flag) continue;
while (computer_sum <= player_sum) //dealer's turn
{
give_out_card(allcard,card); //get an additional card if the number doesn't exceed the player's
computercard[computer_card_number][0] = card[0];
computercard[computer_card_number][1] = card[1];
if (card[1]>10)computer_sum += 0.5;
else computer_sum += card[1];
computer_card_number++;
if (judge(computer_sum))
{
print_cards(computercard);
cout << endl << "you win" << endl; //check whether the dealer busts
flag = 1;
break;
}
}
if (flag) continue;
print_cards(computercard);
switch(final_judge(computer_sum, player_sum, computer_card_number, player_card_number))
{
case 0:cout << "equal" << endl;break; //check the final result
case 1:cout << "you win" << endl;break;
case 2:cout << "you lose" << endl;break;
}
}
}
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(bool allcard[4][13], int playercard[22][2], int computercard[22][2]) //recover the cards to the initial condition
{
int i,j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 13; j++) allcard[i][j] = 0;
}
for (i = 0;i < 21 && playercard[i] != 0; i++) playercard[i][1]=playercard[i][0]=0;
for (i = 0;i < 21 && computercard[i]!=0; i++) computercard[i][1]=computercard[i][0]=0;
}
int give_out_card(bool allcard[4][13],int card[2]) //deal a card
{
int type, number;
srand(time(NULL));
do
{
type=rand()%4;
number=rand()%13;
}
while (allcard[type][number]);
allcard[type][number]=1;
card[0]=type+1;
card[1]=number+1;
}
void print_card(int card[2]) //print a single card
{
switch(card[0])
{
case 1:cout<<" spade ";break;
case 2:cout<<" hearts ";break;
case 3:cout<<" diamond ";break;
case 4:cout<<" club ";break;
default:break;
}
switch(card[1])
{
case 1:cout<<"A";break;
case 11:cout<<"J";break;
case 12:cout<<"Q";break;
case 13:cout<<"K";break;
default:cout<<card[1];break;
}
}
void print_cards(int cards[22][2]) //print a set of cards
{
for(int i=0;cards[i][1]!=0;i++)
{
print_card(cards[i]);
}
}
bool judge(int sum) //judge whether anyone busts
{
if (sum>21) return 1;
else return 0;
}
int final_judge(int computer_sum, int player_sum, int computer_number, int player_number) //judge the final results
{
if (computer_sum>player_sum) return 2;
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;
}