Thread: Simple memory game problem

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    Simple memory game problem

    Hello all, I am new to this board, as well as fairly new to C++. The past year I have taken a class at my high school in C++ which is ending in a final project. I choose a simple memory game(as in, the user picks a "card" from a board, it flips over, user picks another "card" it flips over, the user tries to match the cards). I have a 2d array that represents the board, and is randomly filled with the numbers 97-112(ascii values of a-p which correspond to separate .jpg picture files). I use a waitmouseclick statement to find the spot on the board where the user clicked, then compare the coordinates to the board array to figure out which picture to display. I use a strcat statement to add a ".jpg" to the end of the letter so that it will be in the proper format for outputting...

    pic = board[2][0];
    picname[0] = char(pic);
    strcat (picname,".jpg");
    image testImage(picname, JPEG);
    win.DrawImage(testImage, 210,10);

    "pic" is an integer that represents the randomly generated value in the "board" array. "picname" is the char variable that initially holds the char value of "pic", then gets a ".jpg" added to the end by the strcat. I then pull the image and display it.

    My problem is that this whole part that gets a card is a separate function, which can be called anytime I need the user to click a card. Therefore, every time the function is called, another ".jpg" is added to the end of the "picname" variable, and after the first time through, no picture is outputted and the program ends. It will wait for the user to click, display the right picture, but when the next picture should be displayed the program ends. My question is this: How can I reset the variable(in this case I declare it as "char picname[4]" each time the function is run... or... is there a way to designate where the strcat statement begins adding the ".jpg"?

    Thank you for your time/help

    edit: Sorry, I forgot to mention that I am using codewarrior with the CMU graphics package. My teacher told me to make sure I include this information when posting.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You could just initialize picname to "a.jpg" then only worry about changing the first character, and not use strcat at all

    Code:
    char picname[] = "a.jpg";
    picname[0] = char(pic);
    If you want to use strcat you need to set picname[1] to null before you call strcat.

    Code:
    picname[0] = char(pic);
    picname[1] = '\0';
    strcat (picname,".jpg");
    The null marks the end of the string, and this is where strcat will start appending the ".jpg" string.

    Also your picname length should be [5]... room for the terminating null character.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    2
    Awesome, Thank you very much for the reply! I'm headed into the school right now to use codewarrior.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple game using RNG
    By amirahasanen1 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 11:05 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Help with a simple game
    By jjj93421 in forum Game Programming
    Replies: 1
    Last Post: 03-28-2004, 03:52 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 06:41 PM