//samantha petro. created 3 oct 2006. last modified 17 oct 2006. //deck.cpp - defines the deck class #include "deck.h" // -------- constructors ------- // deck::deck(string fname) { filldeck(fname); } // ----------- set ----------- // void deck::filldeck(string fname) { //open file fstream file; file.open(fname.c_str()); //create temp & counter vars int i = 0; string name; int atk, def; card temp; //retrieve cards from file while(!file.eof()) { //read card members from file file >> name; file >> atk; file >> def; //set card temp.setData(name, atk, def); cardlist.push_back(temp); } } // ------ card functions -------- // card deck::draw() { //store next card to draw card temp = cardlist[cardlist.size()-1]; cardlist.pop_back(); //remove the last card from the deck return temp; } void deck::shuffle() { //randomly shuffle deck between 5 - 10 times /*srand(time(NULL)); int index = (rand() % 5) + 5; for (int i = 0; i < index; i++) { random_shuffle(cardlist.begin(), cardlist.end()); }*/ //seed random number generator with the system's time srand(time(NULL)); //create the temporary card used for the swapping algorithm card temp; int firstindex, secondindex; //indices for the swap for (int i = 0; i < 100; i++) { //retrieve the indices firstindex = (rand() % 32); secondindex = (rand() % 32); /*cout << "turn " << i << endl << "1st: " << cardlist[firstindex] << endl << "2nd: " << cardlist[secondindex] << endl;*/ //shuffle temp = cardlist[secondindex]; //store 2nd index //cout << "swap 1: temp: " << temp << endl; cardlist[secondindex] = cardlist[firstindex]; //move 1st to 2nd index //cout << "swap2: 2ndindex: " << cardlist[secondindex] << endl; cardlist[firstindex] = temp; //move 2nd from temp to 1st index //cout << "swap3: 1stindex: " << cardlist[firstindex] << endl; } } bool deck::hasCards() { return !cardlist.empty(); } //----- debug I/O --------- // ostream &operator << (ostream &os, deck &d) { for (int i = 0; i < d.cardlist.size(); i++) { os << d.cardlist[i] << " "; //add newline if not at end of list if (i < d.cardlist.size() - 1) os << endl; } return os; }