Friday, June 27, 2008

C++ War Card Game

While reading Data Structures in C++: Using the Standard Template Library (STL)

The author brought up the War card game. I thought it would be interesting challenge to see how fast I could create the game. So just for fun here is the source in case anyone is interested.

The rules are simple. Each player has half the deck they flip over the top card each time. Whichever card is higher (2 being lowest Ace being the highest) that person gets both cards. On tie each player flips over three cards and the fourth determines who wins everything.



#include <iostream>
#include <vector>
#include <queue>
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()
using namespace std;
struct Card {
Card(int value) {
this->value = value;
}
int getValue() {
return value;
}
private:
int value;
};
struct Player{
queue cards;
};
struct Deck {
vector cards;
Deck() {
for(int i=1;i<14;++i) j="1;j<5;++j)" j="0;j<10;++j)" i="0;i< cards.size();++i)" int="" r="(rand()" card="" temp="cards[i];" struct="" game="" void="" deck="" player="" cout=""><< "dealing" << i="0;i<<> undecided;
int count =0;
while(a.cards.size() >0 && b.cards.size() >0) {
cout << ++count << ": A cards: " << acard =" a.cards.front();" bcard =" b.cards.front();"> bCard.getValue()) {
int size = undecided.size();
for(int i=0;i< size =" undecided.size();" i="0;i< size;++i)" else="" were="" in="" take="" three="" more="" from="" each="" player="" int="" i="0;i<3;++i)" if=""> 0 ) {
undecided.push(a.cards.front());
a.cards.pop();
}
if (b.cards.size() > 0 ) {
undecided.push(b.cards.front());
b.cards.pop();
}
}
}
}
}
};
int main() {
Game g;
g.start();
return 0;
}

No comments: