Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <iostream>
#include <list>
#include <sstream>
using namespace std;
int to_val(char face) {
switch (face) {
case 'A': return 14;
case 'K': return 13;
case 'Q': return 12;
case 'J': return 11;
default: return 0;
}
}
int main () {
list<int> deck1, deck2, pool1, pool2;
string line;
int val;
char face, suit;
bool numeric;
getline(cin, line);
istringstream ls(line);
while ((numeric = (ls >> val)) || (ls.clear(), ls >> face)) {
if (numeric)
deck1.push_back(val);
else
deck1.push_back(to_val(face));
ls >> suit;
}
getline(cin, line);
istringstream ls2(line);
while ((numeric = (ls2 >> val)) || (ls2.clear(), ls2 >> face)) {
if (numeric)
deck2.push_back(val);
else
deck2.push_back(to_val(face));
ls2 >> suit;
}
while (deck1.size() && deck2.size()) {
//cout << "1: ";
//for (list<int>::iterator itr = deck1.begin(); itr != deck1.end(); itr++)
//cout << *itr << " ";
//cout << endl;
//cout << "2: ";
//for (list<int>::iterator itr = deck2.begin(); itr != deck2.end(); itr++)
//cout << *itr << " ";
//cout << endl;
int a = deck1.front(), b = deck2.front(); deck1.pop_front(); deck2.pop_front();
pool1.push_back(a); pool2.push_back(b);
if (a > b) {
while (pool1.size()) {
deck1.push_back(pool1.front()); pool1.pop_front();
deck1.push_back(pool2.front()); pool2.pop_front();
}
}
else if (b > a) {
while (pool1.size()) {
deck2.push_back(pool2.front()); pool2.pop_front();
deck2.push_back(pool1.front()); pool1.pop_front();
}
}
else {
if (!deck1.size() || !deck2.size()) break;
pool1.push_back(deck1.front()); deck1.pop_front();
pool2.push_back(deck2.front()); deck2.pop_front();
if (!deck1.size() || !deck2.size()) break;
pool1.push_back(deck1.front()); deck1.pop_front();
pool2.push_back(deck2.front()); deck2.pop_front();
if (!deck1.size() || !deck2.size()) break;
pool1.push_back(deck1.front()); deck1.pop_front();
pool2.push_back(deck2.front()); deck2.pop_front();
}
}
if (deck1.size())
cout << "I win!\n";
else
cout << "I lose...\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment