-
Notifications
You must be signed in to change notification settings - Fork 18
/
war.cpp
172 lines (150 loc) · 3.77 KB
/
war.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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
enum Result {
PLAYER1,
PLAYER2,
WAR
};
class Game;
class Card {
private:
string value;
string suit;
public:
Card(string v, string s) : value(v), suit(s) {}
Card(const Card& other) : value(other.value), suit(other.suit) {}
~Card() {}
int intValue() const {
if (all_of(value.begin(), value.end(), ::isdigit)) {
return stoi(value);
}
if (value == "J") {
return 11;
} else if (value == "Q") {
return 12;
} else if (value == "K") {
return 13;
} else {
return 14; // A
}
}
Card& operator = (const Card& other) {
if (this != &other) {
value = other.value;
suit = other.suit;
}
return *this;
}
bool operator < (const Card& other) const {
return intValue() < other.intValue();
}
friend class Game;
};
class Game {
private:
vector<Card> deck1;
vector<Card> deck2;
int nbRounds = 0;
public:
void readDeck(vector<Card>& deck) {
int nbCards;
cin >> nbCards; cin.ignore();
for (int i = 0; i < nbCards; i++) {
string valueSuit;
cin >> valueSuit; cin.ignore();
string value = valueSuit.substr(0, valueSuit.size() - 1);
string suit = valueSuit.substr(valueSuit.size() - 1, 1);
Card card(value, suit);
deck.push_back(card);
}
}
void readGameInput() {
readDeck(deck1);
readDeck(deck2);
}
vector<Card> rotate(vector<Card>& deck, int index) {
vector<Card> rotatedDeck;
for (int i = index; i < deck.size(); i++) {
rotatedDeck.push_back(deck[i]);
}
for (int j = 0; j < index; j++) {
rotatedDeck.push_back(deck[j]);
}
return rotatedDeck;
}
void checkGameOver() {
if (deck1.size() == 0) {
cout << 2 << ' ' << nbRounds << endl;
exit(0);
}
if (deck2.size() == 0) {
cout << 1 << ' ' << nbRounds << endl;
exit(0);
}
}
// Returns the result of one game turn
Result playTurn(int index) {
if (index > deck1.size() || index > deck2.size()) {
cout << "PAT" << endl;
exit(0);
}
checkGameOver();
if (deck2[index] < deck1[index]) {
return PLAYER1;
} else if (deck1[index] < deck2[index]) {
return PLAYER2;
} else {
return WAR;
}
}
void play() {
int index = 0;
while (true) {
Result result = playTurn(index);
int rotateIndex = index + 1;
if (result == PLAYER1) {
nbRounds++;
deck1 = rotate(deck1, rotateIndex);
// take other player's cards
for (int i = 0; i < rotateIndex; i++) {
deck1.push_back(deck2[i]);
}
// remove other player's cards
deck2.erase(deck2.begin(), deck2.begin() + rotateIndex);
index = 0;
} else if (result == PLAYER2) {
nbRounds++;
// take other player's cards
for (int i = 0; i < rotateIndex; i++) {
deck2.push_back(deck1[i]);
}
// remove other player's cards
deck1.erase(deck1.begin(), deck1.begin() + rotateIndex);
deck2 = rotate(deck2, rotateIndex);
index = 0;
} else {
index += 4; // WAR
}
}
}
void showDecks() {
cerr << "round #" << nbRounds << endl;
for (auto card : deck1) {
cerr << card.value << card.suit << " ";
}
cerr << endl;
for (auto card : deck2) {
cerr << card.value << card.suit << " ";
}
cerr << endl;
}
};
int main() {
Game game;
game.readGameInput();
game.play();
}