// 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 #include #include #include #include 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 card class 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 card string 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 Variables bool 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) 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 debugging void deck::debug() { cout << " { "<< cards[0].value <<":"< (p2.value-1)/4) { cout << "=== Player 1 Won round "< (p1.value-1)/4) { cout << "=== Player 2 Won round "< (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); } }