| | |
Shuffle with Random Generator
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 60
Reputation:
Solved Threads: 0
Hello everyone. Thank you again for all the help... At this point, my eyes are bleeding on this project.....My code runs and does everything this card memory matching game is supposed to do, except I've tried to no avail to "shuffle" the cards between iterations (i suspect). I have read many posts, tutorials etc. on rand functions, and even had an example from someone(which is not working for me- ALL COMMENTED OUT) and still don't quite get it.... My text offers several random generators, a random(n), which will ostensibly return a "pseudorandom integer >=0 & <= n-1. Then the int rand(), and a void srand(unsigned int?)
Anyway, I'm lost again, thanks for any help on this last leg of my project::::::::::::
Anyway, I'm lost again, thanks for any help on this last leg of my project::::::::::::
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> using namespace std; void player_input(int card_value[][4], int &choice1x, int &choice1y, int &choice2x, int &choice2y); int card_face[4][4]; int card_value[4][4]; int fill_values(); int card_defacer(); int show_grid(int card_value[][4], bool card[][4]); //int disarray(int card_value[][4]); //int locum(int card_value[][4]); char Continue = 1; int main(){ int choice1x, choice1y, choice2x, choice2y; fill_values(); player_input(card_value, choice1x, choice1y, choice2x, choice2y); //card_defacer(); } int fill_values() //Defining Function establishes 16 cards with paired numbers from 1-8 { card_value[0][0]=1; card_value[0][1]=2; card_value[0][2]=3; card_value[0][3]=4; card_value[1][0]=5; card_value[1][1]=6; card_value[1][2]=7; card_value[1][3]=8; card_value[2][0]=1; card_value[2][1]=2; card_value[2][2]=3; card_value[2][3]=4; card_value[3][0]=5; card_value[3][1]=6; card_value[3][2]=7; card_value[3][3]=8; return 0; } int show_grid(int card_value[][4], bool card[][4]) // Function that will show the "playing table" and output values of only "pairs" other values will be "*******" { cout<<" " << "1 2 3 4 " << endl; //Header for (y) coordinates cout <<" " <<"----------------------------------"<< endl; cout <<endl; for(int x = 0; x < 4; x++) { cout <<" "<<x+1<<"|"; for (int y = 0; y < 4; y++){ if (card[x][y] == true) cout << " "<< card_value[x][y]<< " "; else cout << " "<< "*"<< " "; } cout << endl; cout << endl; } return 0; } void player_input(int card_value[][4], int &choice1x, int &choice1y, int &choice2x, int &choice2y) { do{ bool card[4][4]; show_grid(card_value, card); cout << "Player, enter a coordinate choice for your first card: " << endl; cin >> choice1x >> choice1y; cout << "Player, enter your second choice: " << endl; cin >> choice2x >> choice2y; choice1x = choice1x - 1; choice1y = choice1y - 1; choice2x = choice2x - 1; choice2y = choice2y - 1; if (card_value[choice1x][choice1y] == card_value[choice2x][choice2y]){ card[choice1x][choice1y] = true; card[choice2x][choice2y] = true; } else{ card[choice1x][choice1y] = true; card[choice2x][choice2y] = true; show_grid(card_value, card); system("PAUSE"); system("cls"); card[choice1x][choice1y] = false; card[choice2x][choice2y] = false; }show_grid(card_value, card); cout << "Do you want to continue, enter 1 for yes: "<< endl; cin >> Continue; }while(Continue == '1'); return; } //int locum(int card_value[][4]) //{ // int locumizer2, locumizer3, locumizer4, momentarily; //int locumizer1 =(rand()%3 + 1, locumizer2 = (rand()%3 + 1, locumizer3 = (rand()%3 + 1, locumizer4 = (rand()%3 + 1, momentarily[4][4]; //momentarily[locumizer1][locumizer2] = momentarily[locumizer2][locumizer1]; //card_value[locumizer1][locumizer2] = card_value[locumizer3][locumizer4]; //card_value[locumizer3][locumizer4] = momentarily[locumizer1][locumizer2]; //return 0; //} //int disarray(int card_value[][4]) //{ // locum(card_value); // locum(card_value); // locum(card_value); // locum(card_value); // locum(card_value); // locum(card_value); // locum(card_value); //return 0; //} int random = rand()%3; { printf("%s", card_value[][4]); return; }
Format!!! Format!!! Format!!! Please!!!
You need
And where is your shuffle routine?
You need
srand() to initialize the random generator (use once at the program's start) and rand() to get a random value.And where is your shuffle routine?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2007
Posts: 60
Reputation:
Solved Threads: 0
You need srand() to initialize the random generator (use once at the program's start) and:
Walt, thanks, but I'm not sure what or where to write the shuffle routine....it was called disarray(int card_value[][4]), but I commented it all out.....I thought there may be an easier way of going about this.....And when you say format, are you referring to my code?? Or my messaging - I do realize probably both are a bit of a mess. Sorry, I will try to do better
Walt, thanks, but I'm not sure what or where to write the shuffle routine....it was called disarray(int card_value[][4]), but I commented it all out.....I thought there may be an easier way of going about this.....And when you say format, are you referring to my code?? Or my messaging - I do realize probably both are a bit of a mess. Sorry, I will try to do better
Well, disarray() is meaningless, isn't it? Call it shuffle() 
Make the function to shuffle the cards in a new program. When you get the shuffle function working, simply add it to you program.
Pass what you want shuffled (the source array) into the function.
Use rand() to move that array into a temporary array as the shuffling mechanism.
Move the temp array back into the source array.
And
Did you look at the link? That's why the words were in blue
Make the function to shuffle the cards in a new program. When you get the shuffle function working, simply add it to you program.
Pass what you want shuffled (the source array) into the function.
Use rand() to move that array into a temporary array as the shuffling mechanism.
Move the temp array back into the source array.
And
•
•
•
•
And when you say format, are you referring to my code?? Or my messaging...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Random shuffling can achieved by...
Populating an array with bunch of numbers going up sequentially.
Choose two random numbers where each number is less that the maximum number of things in your array.
Use those two number to swap the indexes of your array.
Keep doing this until you are satisfied.
Populating an array with bunch of numbers going up sequentially.
Choose two random numbers where each number is less that the maximum number of things in your array.
Use those two number to swap the indexes of your array.
Keep doing this until you are satisfied.
*Voted best profile in the world*
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
Not that difficult.
One option would be to declare a shuffled array inside the function. If you're passing in a 2 dimensional array then declare two int variables that would range from 0 to whatever is appropriate and would act as indexes into the passed in array. Assign a random value to each int variable that is within desired range. Do this within a nested loop. Place an asterix or other default value at the random indexes in the original array after assigning the original value into the current spot in the shuffled array. If the cell is already transfered to the shuffled array, then go to the next unstransfered value. Then copy the shuffled array back into the passed in array so the new values are visible back in the calling function.
Alternatively, you could declare four ints in the function. One pair would randomly determine which cell was to move and the other would determine where it would go. Switch the two cells. Repeat 10000 times or so and it should be pretty random.
Edit: This appears to be a variant of iamthwees suggestion.
One option would be to declare a shuffled array inside the function. If you're passing in a 2 dimensional array then declare two int variables that would range from 0 to whatever is appropriate and would act as indexes into the passed in array. Assign a random value to each int variable that is within desired range. Do this within a nested loop. Place an asterix or other default value at the random indexes in the original array after assigning the original value into the current spot in the shuffled array. If the cell is already transfered to the shuffled array, then go to the next unstransfered value. Then copy the shuffled array back into the passed in array so the new values are visible back in the calling function.
Alternatively, you could declare four ints in the function. One pair would randomly determine which cell was to move and the other would determine where it would go. Switch the two cells. Repeat 10000 times or so and it should be pretty random.
Edit: This appears to be a variant of iamthwees suggestion.
Last edited by Lerner; Nov 15th, 2007 at 5:50 pm.
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Help needed filling array with unique random numbers (C++)
- C++ Random # Generator (C++)
- Card Games Help!!!! (Java)
- Implementing Random Images (Java)
- Random number generator (Java)
Other Threads in the C++ Forum
- Previous Thread: Palindrome problem
- Next Thread: Help picking numbers out of an array/string
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






