2014-09-04 182 views
2
// deck of cards 
// below are initializations 
#include <iostream> 
#include <fstream> 
#include <ctime> 

using namespace std; 

int main() 
{ 
ofstream myfile; //setup for copy to text file 
const char usdeck[4][13][14] = //create 3d array of 52 cards 
{ 
{"heart two", "heart three", "heart four", 
"heart five", "heart six", "heart seven", 
"heart eight","heart nine", "heart ten", 
"heart jack","heart queen", "heart king", 
"heart ace"}, 
{"diamond two", "diamond three", "diamond four", 
"diamond five", "diamond six", "diamond seven", 
"diamond eight", "diamond nine", "diamond ten", 
"diamond jack", "diamond queen", "diamond king", 
"diamond ace"}, 
{"club two", "club three", "club four", "club five", 
"club six", "club seven", "club eight", "club nine", 
"club ten", "club jack", "club queen", "club king", 
"club ace"}, 
{"spade two", "spade three", "spade four", 
"spade five", "spade six", "spade seven", 
"spade eight", "spade nine", "spade ten", 
"spade jack", "spade queen", "spade king", 
"spade ace"} 
}; 

for(int row=0;row<4; row++) 
{ 
for(int column=0;column<13;column++) 
    { 
    for(int element=0;element<14;element++) 
     { 
    cout << usdeck[row][column][element] << " "; 
     } 
    cout <<endl; 
} 
} 

myfile.open("UnshuffledDeck.txt");//creates a text file to place unshuffled deck into 
for(int row=0;row<4; row++) 
{ 
for(int column=0;column<13;column++) 
    { 
    for(int element=0;element<14;element++) 
     { 
    myfile << usdeck[row][column][element] << " "; 
//this creates the unshuffled deck text file 
     } 
    myfile <<endl; 
} 
} 
myfile.close(); //closes unshuffled deck text file 




return 0; 

} 

void Shuffle() 
{ 
int temp; 
char theDeck[4][13]; 
srand(time(0)); 

for (int i=0; i<=51; i++) 
{ 

int j = 1 + rand()%52; 
int k = 1 + rand()%52; 

temp = theDeck[j]; 
theDeck[j]=theDeck[k]; 
theDeck[k]=temp; 
} 
} 

我想洗牌在我的甲板。我寫了下面的函數隨機我相信洗牌一副牌,但我不知道如何實施它..我的「洗牌」甲板需要在2D陣列實施..請幫助!隨機化(隨機播放)多維數組到另一個二維數組

+0

ewwwww indent the code :) – 2014-09-04 03:38:45

+0

它最有可能不足以做52次卡片掉期洗牌。您最有效的洗牌將是清空卡組,然後隨機將卡重新放入。 – jackarms 2014-09-04 03:39:11

+0

爲什麼不是一張一卡的卡?這很容易:'std :: string myCards [52] = {「heart two」,「heart three」,等等}。然後,你需要的只是'std :: random_shuffle'而不是所有的代碼。因爲沒有人只是在俱樂部,心臟,黑桃和鑽石中洗牌,所以你在洗牌的第二套牌的要求在現實世界中是沒有意義的。你洗牌整個甲板。 – PaulMcKenzie 2014-09-04 04:13:03

回答

1

我會建議您採用隨機日常使用克努特洗牌。第ku shuffle只需要52張牌換52張牌。

的Knuth的洗牌可以推廣到多個維度。然而,將它洗牌並使用它就好像它是一維數組一樣容易,但如果它是3D數組,則可以訪問它。

維基百科,你可以用它來實現它的一些非常簡單的僞代碼:

http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm

如果您需要幫助學習如何處理一維數組,就好像它是一個三維陣列,查找一些資料對行優先順序: https://courses.engr.illinois.edu/ece390/books/artofasm/CH05/CH05-2.html#HEADING2-105

1

你可以讓你的生活變得更加簡單,如果你:

  1. 使用代表卡的0 - 51之間的整數。
  2. 鑑於0 <= N <= 51,您可以將N/13作爲該卡的套件,將N%13作爲該卡的面。
  3. 可以代表套房的字符串版本,並使用靜態數組的面孔和得到套件的名稱和索引到這些靜態數組的一個卡面的名稱。
  4. 您可以開始了與被訂購一臺,然後洗牌他們多次如你所願使用std::shuffle

這裏有一個程序,做到這一點。

#include <cassert> 
#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <random> 
#include <chrono> 

// Represent cards by numbers 0 - 51. 
// Given 0 <= N <= 51, 
// suite = N/13 
// face = N%13. 

std::string const& getSuiteName(unsigned int index) 
{ 
    static std::string suites[4] = {"Club", "Diamond", "Heart", "Spade"}; 
    assert(index < 4); 
    return suites[index]; 
} 

std::string const& getFaceName(unsigned int index) 
{ 
    static std::string cards[13] = 
    { 
     "Two", "Three", "Four", "Five", 
     "Six", "Seven", "Eight", "Nine", 
     "Ten", "Jack", "Queen", "King", "Ace" 
    }; 
    assert(index < 13); 
    return cards[index]; 
} 

unsigned int getSuiteIndex(unsigned int card) 
{ 
    return card/13; 
} 

unsigned int getFaceIndex(unsigned int card) 
{ 
    return card%13; 
} 

void printCards(std::vector<unsigned int> const& cards) 
{ 
    std::cout << "---------------\n"; 
    for(auto card : cards) 
    { 
     std::cout 
     << getSuiteName(getSuiteIndex(card)) << " " 
     << getFaceName(getFaceIndex(card)) << std::endl; 
    } 
    std::cout << "---------------\n\n"; 
} 

void shuffleCards(std::vector<unsigned int>& cards) 
{ 
    // obtain a time-based seed: 
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); 

    // Shuffle the deck. 
    std::shuffle(cards.begin(), cards.end(), std::default_random_engine(seed)); 
} 

void testShuffle() 
{ 
    std::vector<unsigned int> cards; 
    for (unsigned int i = 0; i < 52; ++i) 
    { 
     cards.push_back(i); 
    } 

    printCards(cards); 

    shuffleCards(cards); 
    printCards(cards); 

    shuffleCards(cards); 
    printCards(cards); 
} 

int main() 
{ 
    testShuffle(); 
    return 0; 
} 
+0

二維數組,這將是一個偉大的方式來進行此事,但我不允許使用任何字符串,並且必須使用3D/2D字符數組 – matt 2014-09-04 06:16:28

+0

你也是預計寫你自己的洗牌算法? – 2014-09-04 06:37:53

+0

是的,我必須寫我自己的洗牌算法,即時通訊不知道如果我去正確的方式 – matt 2014-09-04 17:40:19