0

I was assigned a project to recreate the game of Recontre, which involves generating two separately shuffled decks of 52 cards. The program has no issue randomizing a deck of cards, but when I call separate players in the game class, I thought it should create two separate Deck structures. I'm not sure why it doesn't, and I can't find out how to make that happen, so any help is appreciated. I'll just provide the entire source file since the main function has code to confirm if you debugged the issue.

#include <iostream>
#include <vector>
#include <time.h>
#include <fstream>
using namespace std;

/**
*Creates a deck of cards 1-52 and has a method to randomly shuffle the cards
*/
struct Deck {
    Deck() {}
    /**
    *creates deck of 52 cards in order from 1 to 52
    */
    vector<int> deckofcards() {
        vector<int>newdeck;
        for (int i = 0; i < 52; i++) newdeck.push_back(i + 1);
        return newdeck;
    }
    /**
    *shuffles the deck using a loop and random seed to swap newdeck[i] with a random number in the newdeck vector
    */
    vector<int> shuffle() {
        vector<int>newdeck= deckofcards();
        srand(time(nullptr));
        for (int i = 0; i < 52; i++) {
            swap(newdeck[i], newdeck[rand() % 51 + 1]);
        }
        return newdeck;
    }
};

/**
*Each player has a deck d, and that deck is shuffled.
*/
struct Player {
    Deck d;
    vector<int>deck = d.shuffle();
};

/**
*Uses two players and contains one method to play the game.
*/
class Game {
    Player a, b;
    int numberGames;
public:
    Game(int numberGames) : numberGames(numberGames){}
    /**
    *plays the game
    *uses a loop to determine see how many of the cards in the deck match positions.
    *returns the number of matches
    */
    int match() {
        int matches = 0;
        for (int i = 0; i < 52; i++) {
            if (a.deck[i] == b.deck[i]) matches++;
        }
        return matches;
    }
    /**
    *plays number of games specified by the variable nmatches
    *returns number of card matches for each game in a vector
    */
    vector<int>play(int nmatches) {
        vector<int>results;
        for (int i = 0; i < nmatches; i++) results.push_back(match());
        return results;
    }
    /**
    *sorts the results using a linear sort for easier readability and to make it easier to create statistics of the results
    */
    vector<int>sort() {
        vector<int>tosort = play(numberGames);
        int temp;
        for (int i = 0; i < numberGames; i++) {
            for (int j = i; j < numberGames; j++) {
                if (tosort[i] > tosort[j]) {
                    temp = tosort[i];
                    tosort[i] = tosort[j];
                    tosort[j] = temp;
                }
            }
        }
        return tosort;
    }
    /**
    *writes the sorted results vector to the file recontre.txt with each result separated by a space
    */
    void writeToFile() {
        vector<int>sortedResults = sort();
        ofstream out;
        out.open("recontre.txt");
        for (int i = 0; i < numberGames; i++) out << sortedResults[i] << ' ';
    }
};

int main() {
    int numberGames;
    cout << "Input the number of games you want to play: ";
    cin >> numberGames;
    Game game(numberGames);
    Player a;
    Player b;
    for (int i = 0; i < 52; i++) cout << a.deck[i] << "\t" << b.deck[i] << endl;
    //game.writeToFile();

    return 0;
}
4
  • 4
    seed only once (srand(time(nullptr));)
    – Jarod42
    Feb 2, 2016 at 8:47
  • To clarify: you should call srand only once in the entire program (unless you actually want identical sequences). It's usually done at the beginning of main.
    – molbdnilo
    Feb 2, 2016 at 8:55
  • 3
    Do you know, there is a shuffle algorithm in the standard library?
    – MikeMB
    Feb 2, 2016 at 9:43
  • The class Deck is pointless. Maybe make it a namespace, since it just holds two function with no shared state. Or make it a real class that holds a deck of cards. Feb 2, 2016 at 15:11

1 Answer 1

0

The srand() function should be called just once (at the beginning of your program) to initialize the randomizer. Then call rand() to get a random number (integer between 0 and RAND_MAX (defined in <cstdlib>)).

To simplify your life, you can use the random_shuffle algotithm provided by the standard library: http://en.cppreference.com/w/cpp/algorithm/random_shuffle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.