943,850 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 679
  • C++ RSS
Nov 20th, 2008
0

memory game

Expand Post »
Hi all i am working on a memory game and i am having problems inputing the data so it checks if a 3rd number is pulled up to ignore it
thanks in advance
C++ Syntax (Toggle Plain Text)
  1. int card[6][6],total=0,value,test;
  2. srand((unsigned)time(NULL));//randomize timer
  3.  
  4. for (int i=0;i<6;i++)
  5. {
  6. for (int j=0;j<6;j++)
  7. {
  8. value=rand()%18;//random
  9. test=0;
  10. card[i][j]=value;//sets the value
  11. for (int x=0;x<6;x++)
  12. {
  13. for (int y=0;y<6;y++)//starts the check for doubles
  14. {
  15. if(value==card[x][y])
  16. {
  17. test++;
  18. cout<<i<<endl;
  19. }
  20. if (test==3)
  21. {
  22. //need a code to stop for the 3rd one to be put in.
  23. }
  24. }
  25. }
  26. }
  27. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
egolovin is offline Offline
11 posts
since Oct 2008
Nov 20th, 2008
1

Re: memory game

It might help to describe how the game works and how your program logic accomplishes that. Saying that you need some code for the program to ignore a 3rd number that's pulled up means nothing to us.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 20th, 2008
0

Re: memory game

well it is a memory game where you match 2 numbers
and i want random numbers between 0 and 17 on the board i need 2 of the same number on the board
Reputation Points: 10
Solved Threads: 0
Newbie Poster
egolovin is offline Offline
11 posts
since Oct 2008
Nov 20th, 2008
2

Re: memory game

So you want to populate the board with random numbers in the range of [0, 18) and disallow more than two of a kind? I'd say use a frequency table for the range since it's small, and repeat getting the random number until you find one that doesn't go beyond the limit:
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iomanip>
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8.  
  9. int board[6][6];
  10. int freq[18] = {0};
  11.  
  12. for ( int i = 0; i < 6; i++ ) {
  13. for ( int j = 0; j < 6; j++ ) {
  14. int r;
  15.  
  16. do
  17. r = rand() % 18;
  18. while ( freq[r] >= 2 );
  19.  
  20. ++freq[r];
  21. board[i][j] = r;
  22. }
  23. }
  24.  
  25. for ( int i = 0; i < 6; i++ ) {
  26. for ( int j = 0; j < 6; j++ )
  27. cout<< left << setw ( 3 ) << board[i][j];
  28. cout<<'\n';
  29. }
  30. }
The frequency table holds a count of all of the values in your range.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 20th, 2008
0

Re: memory game

oh wow that is a different way thinking about it. thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
egolovin is offline Offline
11 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need homework help
Next Thread in C++ Forum Timeline: Free safe DLL to play with





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC