#include #include #include #include #include using namespace std; string standardDeckDecode(int); int standardDeckEncode(int); void shuffleDeck(int*, int); void createDeck(int*, int); void cutDeck(int*, int); void theJoeyStandardShuffle(int*, int); void underOut(int*, int*, int, int); void overIn(int*, int*, int, int); void moveDeck(int*, int*, int, int); bool compDeck(int*, int*, int, int); int main(int argc, char **argv) { srand(time(0)); //The size of the card deck. const int STANDARD = 1000; //Create three card decks of length STANDARD. int deck[STANDARD]; int deck1[STANDARD]; //Standard deck is only used for comparison with the main deck. int standardDeck[STANDARD]; //Put STANDARD unique cards into two of the decks, ordered from smallest to largest. createDeck(deck, STANDARD); createDeck(standardDeck, STANDARD); long int count; //The number of under-outs performed on the (main) deck. int top; //The number of card decks to test. int bottom = 1; //The number of cards to start with. string fileName; //The name of the file with the number sequence. //string response; //The response to the file question. fstream file; //The number sequence file. string temp; //A temporary string to place the number sequence in. //Ask the user to enter the number of decks to perform the test on. //cout << "Enter a number." << endl; //cin >> top; if (argc > 1) { top = strtoul(argv[1], NULL, 0); } else { cout << "Please put the number of numbers to generate as the first argument." << endl; } /* cout << "Would you like to write to a file? (y/n)" << endl; cin >> response; //If the response was not valid, force the user to input a correct response. while ((response[0] != 'n') && (response[0] != 'y')) { cout << "I do not understand your response. Please type 'y' or 'n'." << endl; cin >> response; } */ //If the answer is "yes", prepare the file. //if (response[0] == 'y') { if (argc > 2) { /* //Get the name of the file from the user. cout << "Enter the name of the file" << endl; cin >> fileName; */ fileName = argv[2]; //Open the file to verify that it exists. file.open(fileName.c_str()); //If the file could not be opened, proceed as if the answer to the question was "no". if (!file) { cout << "Error: could not open file " << fileName << "." << endl; //response[0] = 'n'; //else, set bottom to the amount of numbers of the sequence that are stored in the file + 1. } else { //If the file is empty, set bottom to 1. if (file.peek() == ifstream::traits_type::eof()) { bottom = 1; } else { while (!file.eof()) { getline(file, temp, ','); bottom++; } } //Close the file until it is next used. file.close(); } } //Repeat top times or until the number of cards (i) is greater the the length of the actual deck. for (int i = bottom; (i < (top + bottom)) && (i <= STANDARD); i++) { //Reset the under-out count. count = 0; //Perform the under-out until the deck is in its original state. do { underOut(deck, deck1, i, i); moveDeck(deck1, deck, i, i); //Increment the number of times that under-out has been performed. count++; } while (!compDeck(deck, standardDeck, i, i)); //Output the deck size and the under-out count. if (!(argc > 2)) { cout << i << "\t" << count << "\t" << ((i == count) ? "=" : "") << endl; } //If the response is "yes", write the new data to the file. //if (response[0] == 'y') { if (argc > 2) { file.open(fileName.c_str()); //Read the sequence from the file. getline(file, temp); file.close(); file.open(fileName.c_str()); //Write the sequence back to the file. file << temp; //Add a comma if the new number is not the first one. if (i != 1) { file << ","; } //Write the new number to the end of the sequence. file << count; file.close(); } } return 0; } bool compDeck(int* deckS, int* deckD, int sizeS, int sizeD) { bool equal = true; for (int i = 0; (i < sizeS) && (i < sizeD); i++) { equal = (deckD[i] == deckS[i]) ? equal : false; } return equal; } void moveDeck(int* deckS, int* deckD, int sizeS, int sizeD) { for (int i = 0; (i < sizeS) && (i < sizeD); i++) { deckD[i] = deckS[i]; } } void overIn(int* deckS, int* deckD, int sizeS, int sizeD) { int temp[sizeS]; int Stop = sizeS; int Dtop = 0; for (int i = 0; (i < sizeD) && (i < sizeS); i++) { //In. deckD[Dtop++] = deckS[--Stop]; deckS[Stop] = 0; //Out. for (int j = 0; j < Dtop; j++) { temp[j] = deckD[j]; } for (int j = 0; j < Dtop; j++) { deckD[(j + Dtop - 1) % Dtop] = temp[j]; } } } void underOut(int* deckS, int* deckD, int sizeS, int sizeD) { int temp[sizeS]; int Stop = sizeS; int Dtop = 0; for (int i = 0; (i < sizeD) && (i < sizeS); i++) { //Under. for (int j = 0; j < Stop; j++) { temp[j] = deckS[j]; } for (int j = 0; j < Stop; j++) { deckS[(j + 1) % Stop] = temp[j]; } //Out. deckD[Dtop++] = deckS[--Stop]; deckS[Stop] = 0; } } void theJoeyStandardShuffle(int* deck, int size) { //The Joey standard: Three shuffles and then a cut. for (int i = 0; i < 3; i++) { shuffleDeck(deck, size); } cutDeck(deck, size); } void cutDeck(int* deck, const int size) { int cut = rand() % size; int temp[size]; for (int i = 0; i < size; i++) { temp[i] = deck[i]; } for (int i = 0; i < size; i++) { deck[(i + cut) % size] = temp[i]; } } void shuffleDeck(int* deck, const int size) { int deckr[size]; int deckl[size]; int drptr = 0; int dlptr = 0; int drtop; int dltop; int cut = rand() % size; //Cut the deck. for (int i = 0; i < size; i++) { if (i > cut) { deckr[drptr++] = deck[i]; } else { deckl[dlptr++] = deck[i]; } } //Record the length of the two decks. drtop = drptr; dltop = dlptr; //Reset the deck pointers. drptr = 0; dlptr = 0; //Shuffle the decks together. for (int i = 0; i < size; i++) { if (drptr >= drtop) { deck[i] = deckl[dlptr++]; } else if (dlptr >= dltop) { deck[i] = deckr[drptr++]; } else if ((rand() % 2) == 1) { deck[i] = deckr[drptr++]; } else { deck[i] = deckl[dlptr++]; } } } void createDeck(int* deck, int size) { for (int i = 0; i < size; i++) { deck[i] = i + 1; } } string standardDeckDecode(int card) { string suit; string number; if (card == (4 * 13 + 2)) { return "Black Joker"; } else if (card == (4 * 13 + 1)) { return "Red Joker"; } else if ((card > 0) && (card <= (4 * 13))) { switch ((card - 1) / 13) { case 0: suit = "Hearts"; break; case 1: suit = "Clubs"; break; case 2: suit = "Diamonds"; break; case 3: suit = "Spades"; break; default: return "Suit Error"; } switch (((card - 1) % 13) + 1) { case 1: number = "Ace"; break; case 2: number = "Two"; break; case 3: number = "Three"; break; case 4: number = "Four"; break; case 5: number = "Five"; break; case 6: number = "Six"; break; case 7: number = "Seven"; break; case 8: number = "Eight"; break; case 9: number = "Nine"; break; case 10: number = "Ten"; break; case 11: number = "Jack"; break; case 12: number = "Queen"; break; case 13: number = "King"; break; default: return "Number Error"; } return number + " of " + suit; } else if (card == 0) { return "_"; } else { return "Error: Card out of range."; } }