// 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;
}
}
我试图在我的套牌中洗牌。我写了下面的功能Shuffle,我相信它会洗牌一副牌,但我不确定如何实现它。我的“洗牌”牌需要是在2D数组中实现..请帮助!
答案 0 :(得分:1)
我建议实施你的Shuffle例程来使用Knuth Shuffle。 kunth shuffle只需要52张牌就52张牌。
Knuth Shuffle可以推广到多个维度。然而,它更容易随机播放并使用它就像它是一维数组一样,但如果它是一个3D数组则可以访问它。
维基百科有一些非常简单的伪代码可用于实现它:
http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
如果您需要帮助学习如何将一维数组视为3D数组,请在Row-Major排序中查找一些材料: https://courses.engr.illinois.edu/ece390/books/artofasm/CH05/CH05-2.html#HEADING2-105
答案 1 :(得分:1)
如果您:
,您可以让您的生活更轻松0 - 51
之间的整数表示卡片。0 <= N <= 51
,您可以将N/13
视为卡片的套件,将N%13
视为卡片的正面。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;
}