我无法生成两个单独随机的卡片组

时间:2016-02-02 08:45:34

标签: c++

我被分配了一个重建Recontre游戏的项目,该游戏涉及生成两张由52张牌组成的单独洗牌的牌组。该程序没有问题随机化一副​​牌,但当我在游戏类中调用单独的玩家时,我认为它应该创建两个单独的Deck结构。我不知道它为什么没有,我不知道如何实现这一点,所以任何帮助都表示赞赏。我只提供整个源文件,因为main函数有代码来确认你是否调试了这个问题。

#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;
}

1 个答案:

答案 0 :(得分:0)

srand()函数应该只调用一次(在程序开头)来初始化随机数发生器。然后调用rand()获取一个随机数(0到RAND_MAX之间的整数(在<cstdlib>中定义))。

为了简化您的生活,您可以使用标准库提供的random_shuffle算法:http://en.cppreference.com/w/cpp/algorithm/random_shuffle