rendered paste body// Assignment 2 - WAR!// TODO: Fix memory leaks!// * write a operator= so that every time cards is copied, memory isn't// leaking...// * write a copy constructor#include <string>#include <sstream>#include <iostream>#include <ctime>#include <cstdlib>using namespace std;// Defines a SINGLE card!//// Card has 3 vars... Value, suit, and face// it has mainly 1 function to print the value of the cardclass card { string card_center(string); public: static unsigned int mode; int value; string face; // s=spade, h=heart, d=diamond, c=club, '\0'=no suit char suit; card(int value=0, string face="", char suit='\0'); // functions string display_card(int mode=-1);};unsigned int card::mode=1;// either outputs the card value or a graphic of the card!string card::display_card(int mode) { if (mode==-1) { mode=card::mode; } stringstream out; if (mode==1) { out << this->face << "-" << this->suit; } else { out << ".-------.\n"; if (this->suit == 's') out << "| * |\n" << "| * * |\n" << "|* *|\n" << "| *I* |\n"; else if (this->suit=='d') out << "| / \\ |\n" << "| / \\ |\n" << "| \\ / |\n" << "| \\ / |\n"; else if (this->suit=='c') out << "| _ |\n" << "| _( )_ |\n" << "|(_ . _)|\n" << "| I |\n"; else if (this->suit=='h') out << "| ** ** |\n" << "|* * *|\n" << "| * * |\n" << "| * |\n"; else out << "| |\n" << "| |\n" << "| |\n" << "| |\n"; out << card_center(face); out << "'-------'"; } return out.str(); }// Centers the card face on the cardstring card::card_center(string face) { if (face.length() > 7) face=face.substr(0,7); if (face.length() == 4) face=face.substr(0,1)+" "+face.substr(1,1)+" "+face.substr(2,1)+" "+face.substr(3); else if (face.length() % 2 == 0) face=face.substr(0,face.length()/2)+" "+face.substr(face.length()/2); string space; for (unsigned int i=0;i<(7-face.length())/2;i++) { space+=' '; } face="|"+space+face+space+"|\n"; return face;}card::card(int value, string face, char suit) { this->value=value; this->face=face; this->suit=suit;}class deck { card *cards; unsigned short int _size; public: static bool use_jokers; static unsigned short int maxsize; deck(); ~deck(); string name; int size() { return _size; }; // // FUNCTION/METHOD PROTOTYPES OR WHATEVER YOU CALL THEM! // // prints entire content of deck void debug(); void newDeck(); void shuffle(); card deal(); void receive(card); void reset();};// Static Variablesbool deck::use_jokers=false;unsigned short int deck::maxsize=52;//// ACTUAL FUNCTIONS/METHODS HERE//void deck::reset() { _size=0; for(int i=0;i<deck::maxsize;i++) { cards[i].value=0; cards[i].face=string(); cards[i].suit=' '; } cout << "Resetted array: ";debug(); cout << endl;}void deck::receive(card input) { if (_size > deck::maxsize) cout << "DEBUG: OVERFLOW OF CARDS! WE HAVE MORE CARDS IN THE DECK THAN SPACES!" << endl; cards[_size]=input; _size++;}// takes one card out of the deck and returns it.card deck::deal() { if (_size==0) return card(); card tmp; tmp=cards[_size-1]; cards[_size-1]= card(); _size--; return tmp;}deck::deck() { cards=new card[deck::maxsize]; _size=0;}deck::~deck() { delete cards;}void deck::shuffle() { int r; card tmp; srand(time(0)); for(int i=0; i < _size-1;i++) { r=i + (rand() % (_size-i)); tmp=cards[i];cards[i]=cards[r];cards[r]=tmp; }}// prints out array for debuggingvoid deck::debug() { cout << " { "<< cards[0].value <<":"<<cards[0].face<<":"<<cards[0].suit; for (int i=1;i<_size;i++) cout << " , " << cards[i].value << ":"<<cards[i].face<<":"<<cards[i].suit; cout << " } ";}// deals cardsvoid deck::newDeck() { stringstream tmp; char tmpsuit=' '; int values=0; for (int j=0;j<4;j++) { for (int i=0;i<13;i++) { switch (j) { case 0: tmpsuit='d'; break; case 1: tmpsuit='c'; break; case 2: tmpsuit='h'; break; case 3: tmpsuit='s'; break; } cards[values].value=(i*4)+j+1; if (i < 9) { tmp.str(""); tmp << i+2; cards[values].face=tmp.str(); } else if (i==9) cards[values].face="Jack"; else if (i==10) cards[values].face="Queen"; else if (i==11) cards[values].face="King"; else if (i==12) cards[values].face="Ace"; cards[values].suit=tmpsuit; values++; _size++; } }}// MAIN function prototypescard draw(deck &, deck &, bool display=true);void war(card, card,deck &, deck &, deck &, deck&);int main () { card::mode=3; int round=0; deck dealer, player1, player2, player1discard,player2discard; player1.name="Player 1"; player2.name="Player 2"; player1discard.name="Player 1"; player2discard.name="Player 2"; card dealcard, p1, p2; bool p1won=false,p2won=false; cout << "===PREGAME DEBUG===\nDealer array: "; dealer.debug(); //cout << endl << "NewDeck Array: "; dealer.newDeck(); //dealer.debug(); //cout << endl; dealer.shuffle(); //cout << "Shuffled deck: "; //dealer.debug(); //cout << endl << "===END DEBUG===" << endl;*/ cout << endl << endl << "Let's play the game of war!" << endl << endl; // deal cards to players dealcard=dealer.deal(); while(dealcard.value != 0) { player1.receive(dealcard); dealcard=dealer.deal(); player2.receive(dealcard); dealcard=dealer.deal(); } // let's kill the dealer :-P // Umm... how do you unset/delete a declared class? /*cout << "===INIT PLAYER DEBUG===" << endl; cout << "Player 1's cards: "; player1.debug(); cout << endl << "Player 2's cards: "; player2.debug(); cout << endl << "===END INIT DEBUG===" << endl << endl << endl;*/ p1=draw(player1,player2discard); p2=draw(player2,player2discard); while (p1won == false && p2won== false ) { round++; if ((p1.value-1)/4 > (p2.value-1)/4) { cout << "=== Player 1 Won round "<<round <<"! ===" << endl << endl; player1discard.receive(p2); player1discard.receive(p1); } else if ((p2.value-1)/4 > (p1.value-1)/4) { cout << "=== Player 2 Won round "<<round <<"! ===" << endl << endl; player2discard.receive(p1); player2discard.receive(p2); } else { // WAR!! cout << "=== The two cards are the same - WAR in round "<<round <<"! ===" << endl << endl; war(p1, p2, player1, player1discard, player2, player2discard); } cout << "=== DEBUG: TOTAL AMOUNT OF CARDS: " << player1.size()+player2.size()+player1discard.size()+player2discard.size() << " ===" <<endl; p1=draw(player1,player1discard); p2=draw(player2,player2discard); if (p1.value == 0) { p2won=true; } if (p2.value == 0) { p1won=true; } } if (p1won) { cout << "Player 1 won!" << endl; } else if (p2won) { cout << "Player 2 won!" << endl; } else { cout << "something went wrong with the code!" << endl; }}// takes 1 card and moves cards from discard pile to playing pile if// playing pile is emptycard draw (deck &inplay, deck &discard, bool display) { card tmp; deck tmpdeck; tmp=inplay.deal(); if(tmp.value == 0){ tmpdeck=inplay; inplay=discard; discard=tmpdeck; discard.reset(); inplay.shuffle(); tmp=inplay.deal(); } if (tmp.value != 0 && display) { cout << inplay.name << " has " << inplay.size()+discard.size() << " cards left:\n"; cout << tmp.display_card(); cout << endl; } return tmp;}// o stands for original card, b stands for battle cards// c stands for card, d for discard, p for play//// takes care of warvoid war (card o1, card o2, deck &p1, deck &d1, deck &p2, deck &d2) { card c1[3],c2[3]; card b1,b2; for (int i=0;i<3;i++) { c1[i]=card(); c2[i]=card(); } //collect 3 cards for(int i=0;i<3;i++) { c1[i]=draw(p1,d1, false); if (c1[i].value==0 && i==0) { b1=o1; c1[i]=card(); break; } else if (c1[i].value==0 && i !=0) { b1=c1[i-1]; c1[i]=card(); break; } } for(int i=0;i<3;i++) { c2[i]=draw(p2,d2, false); if (c2[i].value==0 && i==0) { b2=o2; c2[i]=card(); break; } else if (c2[i].value==0 && i!=0) { b2=c2[i-1]; c2[i]=card(); break; } } cout << endl<<"the battle ensues... "<< b1.value<< endl; // if battle card is not set, draw another one... if (b1.value==0) { b1=draw(p1,d1, false); if (b1.value==0) { b1=c1[2]; c1[2]=card(); } } if (b2.value==0) { b2=draw(p2,d2, false); if (b2.value==0) { b2=c2[2]; c2[2]=card(); } } cout << "Player 1:" << endl << b1.display_card() << endl; cout << "Player 2:" << endl << b2.display_card() << endl; if ((b1.value-1)/4 > (b2.value-1)/4) { cout << "=== Player 1 Won the war round! ===" << endl << endl; d1.receive(b1); d1.receive(b2); d1.receive(o1); d1.receive(o2); cout << "Spoils of war won:" << endl; for(int i=0;i<3;i++) { if (c1[i].value != 0) { cout << c1[i].display_card()<< endl; d1.receive(c1[i]); } if(c2[i].value !=0) { cout << c1[i].display_card()<< endl; d1.receive(c2[i]); } } cout << "====================" << endl; } else if ((b2.value-1)/4 > (b1.value-1)/4) { cout << "=== Player 2 Won the war round! ===" << endl << endl; d2.receive(b1); d2.receive(b2); d2.receive(o1); d2.receive(o2); cout << "Spoils of war won:" << endl; for(int i=0;i<3;i++) { if (c1[i].value != 0) { cout << c1[i].display_card()<< endl; d2.receive(c1[i]); } if(c2[i].value !=0) { cout << c1[i].display_card()<< endl; d2.receive(c2[i]); } } cout << "====================" << endl; } else { cout << "DEBUG RECURSIVEWAR: " << b1.face << endl; war(b1, b2, p1, d1, p2, d2); }}