Thread: I need help with this memory game

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    Question I need help with this memory game

    I'm currently doing with this memory game and i have a problem on how to make it using C languae. Please help me on how to implement it. I used colors that will serves as the hidden image inside the tile ( it is 4 x 7) and I had a trouble with it. It is our project for this semester. Please I need your help with this project.

    My implementation in setting the colors:

    int boxcolor[4][7];

    void setcolor();
    int exist[14]={0}, okey=0,i,j;
    randomize();
    for(i=0;i<4;i++)
    for(j=0;j<7;j++){
    do{
    boxcolor[i][j]=random(13)+1;
    if(exist[boxcolor[i][j]-1] > 1) okey=1;
    else{
    exist[boxcolor[i][j]-1]++;
    okey=0;
    }
    }while(okey==0);
    }
    }

    Is this correct? if not please I need your assistance.
    And i have also a problem on how to track the rest of the boxes that has been revealed and the rest of the functions. Please you help is much appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Assign the colors in pairs. This way you're sure each card/square will have a matching card/square. You may want something like:
    Code:
    /**
    *** TS = total squares
    *** x = counter for total number of squares being used
    *** y = counter for square color being placed
    *** MC = max colors
    *** color = current color
    *** s = spot to place color
    *** fs = free spot
    *** uc = used count
    **/
    
    for( x = 0; x < TS; )
    {
       color = (rand()%MC)+1;
       for(y=0;y<2;y++,x++)
       {
          /** Place 1 color. **/
          s = rand()%(TS-x);
          for( fs=0, uc=0; fs+uc < TS; )
          {
             if( squares[fs+uc] == 0 )
             { if( fs==s )
                { squares[fs+uc] = color; break; }
                else
                   fs++;
             }
             else
                uc++;
          }
       }
    }
    Ain't that puuuurrdy?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    Thanks! but can a variable be used without having to declare it? i'm a little bit confused with the line:

    if( squares[fs+uc] == 0 )
    //squares is not declared.

    and also in memory game the colors must be in random everytime it will runs. Thanks for this, but can you explain it clearer the line:

    color = (rand()%MC)+1; and s = rand()%(TS-x); Thanks!

    Another is that, how can I trace the status of the square, weither it is currently open or not and back again if it is not the pair of the opened one.

    Thanks for your help!!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, if you look, none of the variables are declared. I just put in my comments what they are. 'squares' in this case is your "board" or whatever you want to call it.

    Additionally, if you seed rand(), the colors will be different every time it is run.

    You'll likely want to write your own, otherwise when your instructor asks you to tell what this code is doing, you won't have a clue. If you did, you wouldn't be asking me these questions. (Not that I mind, and not that I care if you claim credit for my example, but you still may want to know what your code is doing before you use it.)

    If you can understand what my example does, tracking what square is open is trivial.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. A snake game - Memory problem
    By gavra in forum C Programming
    Replies: 29
    Last Post: 11-23-2008, 12:58 PM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM