Repository navigation
Expand file tree
/
Copy pathGame.cpp
More file actions
178 lines (162 loc) · 5.98 KB
/
Copy pathGame.cpp
File metadata and controls
178 lines (162 loc) · 5.98 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
#include "Game.h"
#include <iostream>
#include <limits>
static int readInt() {
int n;
while (!(std::cin >> n)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return n;
}
Game::Game() : current(0), activeColor(Color::Red) {
std::cout << "Player 1 name: ";
std::getline(std::cin, playerNames[0]);
if (playerNames[0].empty()) playerNames[0] = "Player 1";
std::cout << "Player 2 name: ";
std::getline(std::cin, playerNames[1]);
if (playerNames[1].empty()) playerNames[1] = "Player 2";
for (int i = 0; i < 7; ++i) {
hands[0].addCard(deck.draw());
hands[1].addCard(deck.draw());
}
// keep flipping until we get a non-wild start card
Card* startCard;
do {
startCard = deck.draw();
if (startCard->color == Color::Wild) {
deck.buryCard(startCard);
startCard = nullptr;
}
} while (!startCard);
activeColor = startCard->color;
deck.discard(startCard);
}
Game::~Game() {
for (auto& h : hands)
for (Card* c : h.cards)
delete c;
}
Color Game::chooseColor() const {
std::cout << "Pick a color: 0=🔴 1=🟡 2=🟢 3=🔵\n> ";
int ch;
while (!(std::cin >> ch) || ch < 0 || ch > 3) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "> ";
}
Color opts[] = { Color::Red, Color::Yellow, Color::Green, Color::Blue };
return opts[ch];
}
void Game::handleFallout(const TurnEffect& effect) {
int other = 1 - current;
for (int i = 0; i < effect.forceDraw; ++i) {
Card* c = deck.draw();
if (c) hands[other].addCard(c);
}
if (effect.skipNext)
std::cout << playerNames[other] << " is skipped.\n";
}
bool Game::hasPlayable(const Hand& h) const {
const Card* top = deck.peekTop();
for (Card* c : h.cards)
if (c->isPlayable(activeColor, top)) return true;
return false;
}
void Game::run() {
std::cout << "=== UNO -- 2 Player ===\n";
while (true) {
int other = 1 - current;
const Card* top = deck.peekTop();
std::cout << "\n--- " << playerNames[current] << "'s turn ---\n";
std::cout << playerNames[other] << " has " << hands[other].cards.size()
<< " card(s). Draw pile: " << deck.size() << " card(s).\n";
std::cout << "Discard: " << top->display();
if (top->color == Color::Wild)
std::cout << " (active: " << colorName(activeColor) << ")";
std::cout << "\nHand:\n";
hands[current].display(activeColor);
TurnEffect effect;
bool cardPlayed = false;
if (hasPlayable(hands[current])) {
int idx;
while (true) {
std::cout << "Play card [0-" << (int)hands[current].cards.size() - 1 << "]: ";
idx = readInt();
if (idx < 0 || idx >= (int)hands[current].cards.size()) {
std::cout << "Out of range.\n";
continue;
}
if (!hands[current].cards[idx]->isPlayable(activeColor, deck.peekTop())) {
std::cout << "That card doesn't play here.\n";
continue;
}
break;
}
Card* c = hands[current].removeCard(idx);
std::cout << "Played: " << c->display() << "\n";
if (c->color == Color::Wild) {
activeColor = chooseColor();
std::cout << "Active color: " << colorName(activeColor) << "\n";
} else {
activeColor = c->color;
}
effect = c->applyEffect();
deck.discard(c);
cardPlayed = true;
} else {
// draw until a playable card is found
std::cout << "No playable cards. Drawing...\n";
Card* playableDrawn = nullptr;
while (!playableDrawn) {
Card* drawn = deck.draw();
if (!drawn) {
std::cout << "Deck is empty, nothing to draw.\n";
break;
}
std::cout << "Drew: " << drawn->display() << "\n";
if (drawn->isPlayable(activeColor, deck.peekTop())) {
playableDrawn = drawn;
} else {
hands[current].addCard(drawn);
}
}
if (playableDrawn) {
std::cout << "It's playable! Play it? 1=yes 0=keep: ";
if (readInt() == 1) {
std::cout << "Played: " << playableDrawn->display() << "\n";
if (playableDrawn->color == Color::Wild) {
activeColor = chooseColor();
std::cout << "Active color: " << colorName(activeColor) << "\n";
} else {
activeColor = playableDrawn->color;
}
effect = playableDrawn->applyEffect();
deck.discard(playableDrawn);
cardPlayed = true;
} else {
hands[current].addCard(playableDrawn);
}
}
}
// when a player drops to 1 card, opponent can call Uno
if (cardPlayed && hands[current].cards.size() == 1) {
std::cout << playerNames[other] << ", call Uno on " << playerNames[current]
<< "? 1=yes 0=no: ";
if (readInt() == 1) {
std::cout << playerNames[current] << " must draw 2 penalty cards!\n";
for (int i = 0; i < 2; ++i) {
Card* c = deck.draw();
if (c) hands[current].addCard(c);
}
}
}
if (hands[current].isEmpty()) {
std::cout << playerNames[current] << " wins! UNO!\n";
return;
}
handleFallout(effect);
if (!effect.skipNext)
current = 1 - current;
}
}