Shuffle with Random Generator

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Shuffle with Random Generator

 
0
  #1
Nov 15th, 2007
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::::::::::::

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. void player_input(int card_value[][4], int &choice1x, int &choice1y, int &choice2x, int &choice2y);
  6.  
  7. int card_face[4][4];
  8. int card_value[4][4];
  9. int fill_values();
  10. int card_defacer();
  11. int show_grid(int card_value[][4], bool card[][4]);
  12. //int disarray(int card_value[][4]);
  13. //int locum(int card_value[][4]);
  14. char Continue = 1;
  15.  
  16.  
  17. int main(){
  18. int choice1x, choice1y, choice2x, choice2y;
  19.  
  20. fill_values();
  21. player_input(card_value, choice1x, choice1y, choice2x, choice2y);
  22. //card_defacer();
  23.  
  24. }
  25.  
  26. int fill_values() //Defining Function establishes 16 cards with paired numbers from 1-8
  27. {
  28.  
  29. card_value[0][0]=1;
  30. card_value[0][1]=2;
  31. card_value[0][2]=3;
  32. card_value[0][3]=4;
  33. card_value[1][0]=5;
  34. card_value[1][1]=6;
  35. card_value[1][2]=7;
  36. card_value[1][3]=8;
  37. card_value[2][0]=1;
  38. card_value[2][1]=2;
  39. card_value[2][2]=3;
  40. card_value[2][3]=4;
  41. card_value[3][0]=5;
  42. card_value[3][1]=6;
  43. card_value[3][2]=7;
  44. card_value[3][3]=8;
  45.  
  46. return 0;
  47.  
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. 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 "*******"
  56.  
  57. {
  58.  
  59.  
  60.  
  61. cout<<" " << "1 2 3 4 " << endl; //Header for (y) coordinates
  62. cout <<" " <<"----------------------------------"<< endl;
  63.  
  64.  
  65. cout <<endl;
  66.  
  67.  
  68. for(int x = 0; x < 4; x++)
  69. {
  70. cout <<" "<<x+1<<"|";
  71.  
  72.  
  73.  
  74. for (int y = 0; y < 4; y++){
  75.  
  76. if (card[x][y] == true)
  77.  
  78. cout << " "<< card_value[x][y]<< " ";
  79. else
  80. cout << " "<< "*"<< " ";
  81.  
  82.  
  83. }
  84. cout << endl;
  85. cout << endl;
  86. }
  87. return 0;
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94. void player_input(int card_value[][4], int &choice1x, int &choice1y, int &choice2x, int &choice2y)
  95.  
  96.  
  97. {
  98.  
  99.  
  100.  
  101. do{
  102.  
  103. bool card[4][4];
  104. show_grid(card_value, card);
  105.  
  106.  
  107.  
  108.  
  109. cout << "Player, enter a coordinate choice for your first card: " << endl;
  110. cin >> choice1x >> choice1y;
  111. cout << "Player, enter your second choice: " << endl;
  112. cin >> choice2x >> choice2y;
  113.  
  114.  
  115. choice1x = choice1x - 1;
  116. choice1y = choice1y - 1;
  117. choice2x = choice2x - 1;
  118. choice2y = choice2y - 1;
  119.  
  120.  
  121. if (card_value[choice1x][choice1y] == card_value[choice2x][choice2y]){
  122. card[choice1x][choice1y] = true;
  123. card[choice2x][choice2y] = true;
  124. }
  125. else{
  126.  
  127. card[choice1x][choice1y] = true;
  128. card[choice2x][choice2y] = true;
  129.  
  130. show_grid(card_value, card);
  131.  
  132. system("PAUSE");
  133. system("cls");
  134.  
  135. card[choice1x][choice1y] = false;
  136. card[choice2x][choice2y] = false;
  137.  
  138.  
  139.  
  140.  
  141. }show_grid(card_value, card);
  142.  
  143. cout << "Do you want to continue, enter 1 for yes: "<< endl;
  144. cin >> Continue;
  145.  
  146. }while(Continue == '1');
  147.  
  148.  
  149. return;
  150.  
  151.  
  152.  
  153.  
  154.  
  155. }
  156. //int locum(int card_value[][4])
  157. //{
  158. // int locumizer2, locumizer3, locumizer4, momentarily;
  159.  
  160. //int locumizer1 =(rand()%3 + 1, locumizer2 = (rand()%3 + 1, locumizer3 = (rand()%3 + 1, locumizer4 = (rand()%3 + 1, momentarily[4][4];
  161. //momentarily[locumizer1][locumizer2] = momentarily[locumizer2][locumizer1];
  162. //card_value[locumizer1][locumizer2] = card_value[locumizer3][locumizer4];
  163. //card_value[locumizer3][locumizer4] = momentarily[locumizer1][locumizer2];
  164.  
  165. //return 0;
  166. //}
  167.  
  168. //int disarray(int card_value[][4])
  169.  
  170. //{
  171.  
  172. // locum(card_value);
  173. // locum(card_value);
  174. // locum(card_value);
  175. // locum(card_value);
  176. // locum(card_value);
  177. // locum(card_value);
  178. // locum(card_value);
  179. //return 0;
  180.  
  181. //}
  182. int random = rand()%3;
  183. {
  184. printf("%s", card_value[][4]);
  185. return;
  186. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Shuffle with Random Generator

 
0
  #2
Nov 15th, 2007
Format!!! Format!!! Format!!! Please!!!

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Re: Shuffle with Random Generator

 
0
  #3
Nov 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Shuffle with Random Generator

 
0
  #4
Nov 15th, 2007
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
Originally Posted by guitarrick View Post
And when you say format, are you referring to my code?? Or my messaging...
Did you look at the link? That's why the words were in blue
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Re: Shuffle with Random Generator

 
0
  #5
Nov 15th, 2007
Thanks Walt,
will do and I'll give it all a shot
Rick
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Shuffle with Random Generator

 
0
  #6
Nov 15th, 2007
Depending on restrictions there is a function in the algorithm header file of the standard template library that will "automagically" shuffle a given group of items. However, that would take the fun and learning experience out of implementing the routine on your own.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Re: Shuffle with Random Generator

 
0
  #7
Nov 15th, 2007
Thanks Lerner,
No restrictions I don't think --I was also wondering how difficult it would be to write a function that would shuffle?
Regards,
Rick
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Shuffle with Random Generator

 
0
  #8
Nov 15th, 2007
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Shuffle with Random Generator

 
0
  #9
Nov 15th, 2007
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.
Last edited by Lerner; Nov 15th, 2007 at 5:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Re: Shuffle with Random Generator

 
0
  #10
Nov 15th, 2007
Thanks again to iamthewee and Lerner
I'll be at later this evening
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC