2016-02-02 15 views
0

我被分配了一個項目來重新創建Recontre遊戲,其中涉及生成兩個分開洗牌的52張卡片組。該程序沒有隨機化一副​​牌的問題,但是當我在遊戲課中給單獨的玩家打電話時,我認爲它應該創建兩個獨立的Deck結構。我不知道爲什麼它沒有,我不知道如何做到這一點,所以任何幫助表示讚賞。我只提供整個源文件,因爲主函數有代碼來確認是否調試了這個問題。我在生成兩個分開的隨機卡片組時遇到了問題

#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

種子只有一次('函數srand(時間(nullptr));') – Jarod42

+0

澄清:你應該叫'srand'在整個程序中只有一次(除非你真的需要相同的序列)。它通常在'main'開始時完成。 – molbdnilo

+3

您是否知道,標準庫中有[shuffle](http://en.cppreference.com/w/cpp/algorithm/random_shuffle)算法? – MikeMB

回答

0

srand()函數應該只調用一次(在程序開始時)初始化隨機函數。然後撥打rand()以獲得一個隨機數(0到RAND_MAX之間的整數(在<cstdlib>中定義))。

要簡化你的生活,你可以使用標準庫提供的random_shuffle algotithm:http://en.cppreference.com/w/cpp/algorithm/random_shuffle