-
Notifications
You must be signed in to change notification settings - Fork 1
/
UNO.cpp
269 lines (263 loc) · 5.44 KB
/
UNO.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include"stdafx.h"
#include"UNO.h"
using namespace std;
bool Card::CheckCurrent(Card& curr)
{
if (curr.number == Card::number || curr.type == this->type || this->type == 5)
return true;
return false;
}
Card::Card()
{
type = 0;
number = 0;
}
Card::Card(Card& card)
{
this->type = card.type;
this->number = card.number;
}
Card& Card::operator=(const Card& card)
{
this->number = card.number;
this->type = card.type;
return *this;
}
void Card::show()
{
switch (type) {
case 5:
cout << "Wildcard" << " "; break;
case 4:
cout << "黃色"; break;
case 3:
cout << "紅色"; break;
case 2:
cout << "綠色"; break;
case 1:
cout << "藍色"; break;
}
if (type != 5 && number != 100) cout << number << " ";
}
CardStack::CardStack(CardStack& cardStack)
{
this->num_card = cardStack.num_card;
this->stack = new Card[num_card];
for (int i = 0; i<num_card; i++)
{
this->stack[i] = cardStack.stack[i];
}
}
CardStack::CardStack()
{
num_card = 80;
this->stack = new Card[this->num_card];
}
CardStack::~CardStack()
{
delete[] stack;
}
Card CardStack::Pop()
{
return stack[--num_card];
}
void CardStack::Shuffle(Card& temp)
{
/*
/// may causes memery leaks cause it doesn't
/// seems to release the 'stack' that CardStack originally hold.
Card* tempstack = new Card[num_card];
/// i shold be ranged (0, num_card - 2)
for(int i = 0;i<num_card;i++)
{
tempstack[i] = stack[i];
}
tempstack[num_card-1] = temp;
int numb1 = 0;
srand(static_cast<unsigned int>(time(NULL)));
numb1 = rand()%num_card;
Card tempc(stack[numb1]);
stack[numb1] = stack[num_card-1];
stack[num_card-1] = tempc;
stack = tempstack;
*/
// the cards are limited to 80,
// so it might needn't to allocate new memery space
num_card++;
srand((unsigned int)time(NULL));
int index = rand() % num_card;
stack[num_card - 1] = stack[index];
stack[index] = temp;
}
void Player::Display()
{
for (int i = 0; i < myCard.num_card; i++)
{
cout << "<" << i + 1 << ">";
myCard.stack[i].show();
}
cout << endl;
}
void Player::Play(Card& curr, CardStack& stack1)
{
int checkcard1 = 0;
pull = 0;
for (int i = 0; i<myCard.num_card; i++)
{
if (myCard.stack[i].CheckCurrent(curr))
{
checkcard1++;
}
}
if (checkcard1 == 0)
{
cout << "沒牌可出,抽一張牌" << endl;
myCard.Shuffle(stack1.Pop());
// why shuffle ? know what you mean, but...why not another method XD
}
else
{
cout << "可出的牌有:";
for (int i = 0; i<myCard.num_card; i++)
{
if (myCard.stack[i].CheckCurrent(curr))
{
cout << "<" << i + 1 << ">";
myCard.stack[i].show();
}
}
cout << "選一張牌出";
cin >> pull;
if (myCard.stack[pull - 1].CheckCurrent(curr))
{
Card tempcard = myCard.stack[pull - 1];
myCard.stack[pull - 1] = myCard.stack[myCard.num_card - 1];
myCard.stack[myCard.num_card - 1] = tempcard;
}
else
{
// not familar with game rules, but is this your own rule ?
cout << "選的牌不能出,抽一張牌" << endl;
myCard.Shuffle(stack1.Pop());
pull = 0;
}
}
}
void Player::ChangeColor(Card& curr)
{
int color = 0;
if (curr.type == 5)
{
cout << "選一個顏色:" << " <1>藍色 " << " <2>綠色 " << " <3>紅色 " << " <4>黃色 ";
cin >> color;
curr.type = color;
curr.number = 100;
}
}
UNO::UNO()
{
UNO(2);
}
UNO::UNO(int num_player)
{
num = num_player;
list = new Player[num];
for (int type = 1; type <= 5; ++type) {
int num = (type - 1) * 19;
if (type == 5) {
for (int i = 0; i <= 4; ++i)
tableCard.stack[num + i].number = 10,
tableCard.stack[num + i].type = type;
break;
}
tableCard.stack[num].number = 0;
tableCard.stack[num].type = type;
for (int i = 1; i <= 9; ++i) {
tableCard.stack[num + i].number = tableCard.stack[num + i + 9].number = i;
tableCard.stack[num + i].type = tableCard.stack[num + i + 9].type = type;
}
}
}
void UNO::shuffle()
{
CardStack temp(tableCard);
int numb = 0;
srand(static_cast<unsigned int>(time(NULL)));
for (int j = 0; j<tableCard.num_card; j++)
{
numb = rand() % tableCard.num_card;
while (temp.stack[numb].number == 100)
{
numb = rand() % tableCard.num_card;
}
tableCard.stack[j] = temp.stack[numb];
temp.stack[numb].number = 100;
}
}
void UNO::deal()
{
for (int i = 0; i<num; i++)
{
list[i].myCard.stack = new Card[80];
}
for (int k = 0; k<num; k++)
{
for (int j = 0; j<3; j++)
{
Card temp1(tableCard.Pop());
list[k].myCard.num_card = 3;
list[k].myCard.stack[j] = temp1;
}
}
Card tempcard2(tableCard.Pop());
currentCard = tempcard2;
}
bool UNO::checkUNO(int playernum)
{
bool iswin = false;
if (list[playernum].myCard.num_card == 1)
{
cout << "UNO!!!!" << endl;
}
if (list[playernum].myCard.num_card == 0)
{
cout << endl << "玩家" << list[playernum].name << "獲勝!!!";
iswin = true;
}
return iswin;
}
void UNO::Game()
{
pFunc func = &Player::Play;
bool win = false;
do
{
for (int i = 0; i<num; i++)
{
cout << endl << "現在的牌為:";
currentCard.show();
cout << endl;
cout << "現在輪到玩家:" << list[i].name << endl;
cout << "他的牌有:";
list[i].Display();
(list[i].*func)(currentCard, tableCard);
if (list[i].pull != 0)
{
if (currentCard.number == 100)
{
currentCard.type = 5;
}
tableCard.Shuffle(currentCard);
Card tempcard1(list[i].myCard.Pop());
currentCard = tempcard1;
list[i].ChangeColor(currentCard);
}
if (checkUNO(i))
{
win = true;
break;
}
}
} while (!win);
cout << endl << "遊戲結束" << endl;
}