DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   memory game (http://www.daniweb.com/forums/thread158646.html)

egolovin Nov 20th, 2008 5:21 pm
memory game
 
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
int card[6][6],total=0,value,test; 
        srand((unsigned)time(NULL));//randomize timer

        for (int i=0;i<6;i++)
        {
                for (int j=0;j<6;j++)
                {
                        value=rand()%18;//random
                        test=0;
                        card[i][j]=value;//sets the value
                        for (int x=0;x<6;x++)
                        {
                                for (int y=0;y<6;y++)//starts the check for doubles
                                {
                                        if(value==card[x][y])
                                        {
                                                test++;
                                                cout<<i<<endl;
                                        }
                                        if (test==3)
                                        {
                                                //need a code to stop for the 3rd one to be put in.
                                        }
                                }
                        }
                }
        }

Narue Nov 20th, 2008 5:23 pm
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.

egolovin Nov 20th, 2008 5:27 pm
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

Narue Nov 20th, 2008 5:42 pm
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:
#include <cstdlib>
#include <iomanip>
#include <iostream>

int main()
{
  using namespace std;

  int board[6][6];
  int freq[18] = {0};

  for ( int i = 0; i < 6; i++ ) {
    for ( int j = 0; j < 6; j++ ) {
      int r;

      do
        r = rand() % 18;
      while ( freq[r] >= 2 );

      ++freq[r];
      board[i][j] = r;
    }
  }

  for ( int i = 0; i < 6; i++ ) {
    for ( int j = 0; j < 6; j++ )
      cout<< left << setw ( 3 ) << board[i][j];
    cout<<'\n';
  }
}
The frequency table holds a count of all of the values in your range.

egolovin Nov 20th, 2008 5:58 pm
Re: memory game
 
oh wow that is a different way thinking about it. thanks


All times are GMT -4. The time now is 5:45 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC