Automatic Shuffle Master

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2005
Posts: 10
Reputation: pokerponcho is an unknown quantity at this point 
Solved Threads: 0
pokerponcho's Avatar
pokerponcho pokerponcho is offline Offline
Newbie Poster

Automatic Shuffle Master

 
0
  #1
Nov 23rd, 2005
You created a bunch of random numbers, but say you don't want a bunch of random numbers exactly. Say you have ten members of a team and you want to give them a random position represented by 0 to 9. So you create your array and you fill it with a random number after checking and seeing if the random number is taken.

This program places each person into a random place. It makes more sence because you know you are going to put Larry, Curly, Moe to work... it's just a question of where!

So I used pointers and I came up with this which has many applications. Hopefully someone will find it useful.

  1.  
  2. void shuffle(int *array_ptr, int len, int def, int min) {
  3. int i = 0;
  4. int temp;
  5. for (i = min; i < len; ++i) {
  6. *(array_ptr + i) = def; // 'zero' the array
  7. }
  8. srand((unsigned)time(0)); // set an srand
  9. while (i < len) { // while not done shuffling
  10. temp = rand() % len + min; // get a random integer between min and len
  11. if (*(array_ptr + temp) == def) { // if nothing in the element yet
  12. // put i into element
  13. *(array_ptr + temp) = i;
  14. ++i; // incrament i
  15. }
  16. }
  17. }

BUT, say instead of shuffling integers I REALLY DO want to place Larry, Curly, and Moe in their places? Instead of an array of integers, I use an array of people. This is where I'm stuck.

  1.  
  2. void shuffle(teamMates *array_ptr, int len, int def, int min) {
  3. int i = 0;
  4. int temp;
  5. for (i = min; i < len; ++i) {
  6. *(array_ptr + i) = def; // 'zero' the array
  7. }
  8. srand((unsigned)time(0)); // set an srand
  9. while (i < len) { // while not done shuffling
  10. temp = rand() % len + min; // get a random integer between min and len
  11. if (*(array_ptr + temp) == def) { // if nothing in the element yet
  12. // put i into element
  13. *(array_ptr + temp) = i;
  14. ++i; // incrament i
  15. }
  16. }
  17. }

Generates Errors:

error C2676: binary '==' : 'struct teamMates' does not define this operator or a conversion to a type acceptable to the predefined operator
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

Does anyone know how to convert this stuff or what is the next step? Do I need to overload operators?
pokerponcho
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16
Reputation: HyperEngineer is an unknown quantity at this point 
Solved Threads: 0
HyperEngineer HyperEngineer is offline Offline
Newbie Poster

Re: Automatic Shuffle Master

 
0
  #2
Nov 23rd, 2005
What is the structure of teammates?
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 10
Reputation: pokerponcho is an unknown quantity at this point 
Solved Threads: 0
pokerponcho's Avatar
pokerponcho pokerponcho is offline Offline
Newbie Poster

Re: Automatic Shuffle Master

 
0
  #3
Nov 23rd, 2005
The teammates was just an illustration. I'm actually making a game that has eight different kind of spaces represented by an unsigned integer.

  1.  
  2. struct space {
  3. unsigned int piece:3;
  4. /* Three bits for eight different marks
  5. * 000 | 0 | V - Vacuum
  6. * 001 | 1 | S - Start
  7. * 010 | 2 | D - Death
  8. * 011 | 3 | R - Shifting Right
  9. * 100 | 4 | L - Shifting Left
  10. * 101 | 5 | B - Back to Start
  11. * 110 | 6 | T - Transport
  12. * 111 | 7 | F - Finish
  13. */
  14. };
pokerponcho
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16
Reputation: HyperEngineer is an unknown quantity at this point 
Solved Threads: 0
HyperEngineer HyperEngineer is offline Offline
Newbie Poster

Re: Automatic Shuffle Master

 
0
  #4
Nov 23rd, 2005
I don't think you can use the *(array_ptr + k) syntax. I think you will have to use array_ptr[k]->piece syntax instead.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 10
Reputation: pokerponcho is an unknown quantity at this point 
Solved Threads: 0
pokerponcho's Avatar
pokerponcho pokerponcho is offline Offline
Newbie Poster

Re: Automatic Shuffle Master

 
0
  #5
Nov 23rd, 2005
Originally Posted by HyperEngineer
I don't think you can use the *(array_ptr + k) syntax. I think you will have to use array_ptr[k]->piece syntax instead.
That makes much more sense. Thank you.

Bye the way, I edited and apparently added a couple of bugs with my first algorithm. Sorry. This version should work:

  1.  
  2. // If this doesn't work, then shoot me.
  3. // I commented out the stuff I used for debugging.
  4. // Could come in handy though so it's still there.
  5.  
  6. void shuffle(int *array_ptr, int min, int max, int def) {
  7. int i = min;
  8. int temp;
  9. int len = max - min + 1;
  10. srand((unsigned)time(0)); // set an srand
  11. //std::cout << "\nRANDOMIZING\n";
  12. while (i <= max) { // while not done shuffling
  13. temp = rand() % len; // get a random integer between 0 and length
  14. //std::cout << "Random Position: " << temp << '\n';
  15. if (*(array_ptr + temp) == def) { // if nothing in the element yet
  16. // put i into element
  17. //std::cout << "Empty!\n";
  18. *(array_ptr + temp) = i;
  19. ++i; // incrament i
  20. }/* else {
  21. std::cout << "Occupied.\n";
  22. }*/
  23. //I need it to sleep apparently so I don't get repeated values.
  24. sleep(rand() % 20 + 90);
  25. }
  26. /*std::cout << "\nDONE!\n";
  27. for (i = 0; i < len; ++i) {
  28. std::cout << "Element " << i << ": " << *(array_ptr + i) << '\n';
  29. }*/
  30. }

I am using the wrong syntax for pointers. I totally forgot about the -> operator. Thanks!
pokerponcho
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 10
Reputation: pokerponcho is an unknown quantity at this point 
Solved Threads: 0
pokerponcho's Avatar
pokerponcho pokerponcho is offline Offline
Newbie Poster

Re: Automatic Shuffle Master

 
0
  #6
Nov 23rd, 2005
pokerponcho
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum


Views: 2515 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC