### Is this a good way to do it

I wrote a code to shuffle any number of poker decks. Is this a good way to do it.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115`` ``````#include #include #include #include #include using namespace std; int main() { ifstream in; ofstream out; srand(time(0)); out.open("deck.txt"); out << ""; out.close(); string type[4] = { "leaf", "heart", "diamond", "spades" }; string cards[13] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; int record = 0; int x, y; string a, b; while (record < 52) { int z = 0; x = rand() % 4; y = rand() % 13; out.open("current.txt"); out << cards[y] << " of " << type[x]; out.close(); in.open("current.txt"); getline(in, a); in.close(); in.open("deck.txt"); for (int i = 0; i < 52; i++) { getline(in, b); if (a == b) { z = 1; } else { if (z != 1) { z = 0; } } } in.close(); out.open("deck.txt", ios::app); if (z == 0) { out << a << endl; record = record + 1; } out.close(); } cout << "Enter the number of decks you want to shuffle"; int number; cin >> number; string list[52]; in.open("deck.txt"); for (int i = 0; i < 52; i++) { getline(in, list[i]); } in.close(); out.open("stack.txt"); out << ""; out.close(); int d,e; int f = 52; record = 0; while (record < 52 * number) { d = 0; record = record + 1; e = rand() % f; out.open("stack.txt", ios::app); out << list[e] << endl; out.close(); cout << record << "\t" << list[e] << endl; in.open("stack.txt"); for (int i = 0; i < record; i++) { getline(in, a); if (list[e] == a) { d = d + 1; } } in.close(); if (d == number) { list[e] == ""; out.open("deck.txt"); for (int i = 0; i < f; i++) { if (i!=e) { out << list[i] << endl; } } out.close(); f = f - 1; in.open("deck.txt"); for (int i = 0; i < f;i++) { getline(in, list[i]); } in.close(); } } system("pause"); }``````

I wrote a shorter code too but it worked too slow if the number of decks was really big like 100. This is the shorter code.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include #include #include #include using namespace std; int main() { ifstream in; ofstream out; srand(time(0)); out.open("deck.txt"); out << ""; out.close(); string type[4] = { "leaf", "heart", "diamond", "spades" }; string cards[13] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; int record=0; int x, y; int number; cout << "Enter the number of decks you want to shuffle"; cin >> number; string a, b; while (record < 52*number) { int z = 0; x = rand() % 4; y = rand() % 13; out.open("current.txt"); out << cards[y] << " of " << type[x]; out.close(); in.open("current.txt"); getline(in, a); in.close(); in.open("deck.txt"); for (int i = 0; i < 52*number; i++) { getline(in, b); if (a == b) { z = z + 1; } else { if (z == 0) { z = 0; } } } in.close(); out.open("deck.txt",ios::app); if (z < number) { out << a<
Last edited on
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include #include #include #include #include int main() { std::srand( std::time(nullptr) ) ; const std::string type[4] = { "leaf", "heart", "diamond", "spades" }; const std::string cards[13] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; const std::size_t NCARDS = 52 ; const char* const file_name = "deck.txt" ; // create the deck of cards. deck should contain 52 distinct cards // 13 distinct cards of each type { std::ofstream file(file_name) ; // http://www.stroustrup.com/C++11FAQ.html#for for( const std::string& t : type ) // fpr each type for( const std::string& c : cards ) // for each card file << c << " of " << t << '\n' ; } // shuffle the deck of carfs { std::string deck[NCARDS] ; // array to hold the cards in the deck // read the cards from the file into the array { std::ifstream file(file_name) ; for( std::size_t i = 0 ; i < NCARDS ; ++i ) std::getline( file, deck[i] ) } // shuffle the deck (library) // http://www.cplusplus.com/reference/algorithm/random_shuffle/ std::random_shuffle( deck, deck+NCARDS ) ; // or: // shuffle the deck (hand-coded fisher-yates shuffle) // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle for( auto i = NCARDS-1 ; i > 0 ; --i ) { auto rand_pos = std::rand() % (i+1) ; // pich a random position <= i std::swap( deck[i], deck[rand_pos] ) ; // swap with the card in that position } // print out the shuffled deck for( const std::string& c : deck ) std::cout << c << '\n' ; } }``````

http://coliru.stacked-crooked.com/a/a89cf2d6c442be18
Topic archived. No new replies allowed.