Memory Game not Working

Started by
15 comments, last by Lordimm 15 years, 4 months ago
Alright, I changed my earlier nested if statements to:

if(count == 2)					{						if(box[0] + box[7] != 2 && check == 0)						{							box[0] = 0;							box[7] = 0;							output[0] = ' ';							output[7] = ' ';							check = 1;						}						if(box[1] + box[9] != 2 && check == 0)						{							box[1] = 0;							box[9] = 0;							output[1] = ' ';							output[9] = ' ';							check = 1;						}						if(box[2] + box[26] != 2 && check == 0)						{							box[2] = 0;							box[26] = 0;							output[2] = ' ';							output[26] = ' ';							check = 1;						}						if(box[3] + box[16] != 2 && check == 0)						{							box[3] = 0;							box[16] = 0;							output[3] = ' ';							output[16] = ' ';							check = 1;						}						if(box[4] + box[15] != 2 && check == 0)						{							box[4] = 0;							box[15] = 0;							output[4] = ' ';							output[15] = ' ';							check = 1;						}						if(box[5] + box[24] != 2 && check == 0)						{							box[5] = 0;							box[24] = 0;							output[5] = ' ';							output[24] = ' ';							check = 1;						}						if(box[6] + box[14] != 2 && check == 0)						{							box[6] = 0;							box[14] = 0;							output[6] = ' ';							output[14] = ' ';							check = 1;						}						if(box[8] + box[22] != 2 && check == 0)						{							box[8] = 0;							box[22] = 0;							output[8] = ' ';							output[22] = ' ';							check = 1;						}						if(box[10] + box[27] != 2 && check == 0)						{							box[10] = 0;							box[27] = 0;							output[10] = ' ';							output[22] = ' ';							check = 1;						}						if(box[11] + box[23] != 2 && check == 0)						{							box[11] = 0;							box[23] = 0;							output[11] = ' ';							output[23] = ' ';							check = 1;						}						if(box[12] + box[21] != 2 && check == 0)						{							box[12] = 0;							box[21] = 0;							output[12] = ' ';							output[21] = ' ';							check = 1;						}						if(box[13] + box[19] != 2 && check == 0)						{							box[13] = 0;							box[19] = 0;							output[13] = ' ';							output[19] = ' ';							check = 1;						}						if(box[17] + box[18] != 2 && check == 0)						{							box[17] = 0;							box[18] = 0;							output[17] = ' ';							output[18] = ' ';							check = 1;						}						if(box[20] + box[25] != 2 && check == 0)						{							box[20] = 0;							box[25] = 0;							output[20] = ' ';							output[25] = ' ';							check = 1;						}					}


With check initialized at the beginning of the main game while loop and set to 0, so it'll be reset right after these ifs...and it's doing the same thing.
Advertisement
Quote:Original post by Lordimm
Alright, I changed my earlier nested if statements to:

*** Source Snippet Removed ***

With check initialized at the beginning of the main game while loop and set to 0, so it'll be reset right after these ifs...and it's doing the same thing.


Its not doing the same thing, after step 3 it deletes both previous numbers like you wanted but after that it appers to malfunction.You have to go over your logic again, what is the point of the game ?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Is the game supposed to be a "match pairs" game?

A lot of your logic could be rewritten to be a lot shorter by using loops. You probably should keep an array of the locations of each of the items too. That way, you could have two arrays, an array of "hidden" items and an array to display.
if(count == 2)					{						if(box[0] + box[7] != 2 && check == 0)						{							box[0] = 0;							box[7] = 0;							output[0] = ' ';							output[7] = ' ';							check = 1;						}						if(box[1] + box[9] != 2 && check == 0)						{							box[1] = 0;							box[9] = 0;							output[1] = ' ';							output[9] = ' ';							check = 1;						}						if(box[2] + box[26] != 2 && check == 0)						{							box[2] = 0;							box[26] = 0;							output[2] = ' ';							output[26] = ' ';							check = 1;						}						if(box[3] + box[16] != 2 && check == 0)						{							box[3] = 0;							box[16] = 0;							output[3] = ' ';							output[16] = ' ';							check = 1;						}						if(box[4] + box[15] != 2 && check == 0)						{							box[4] = 0;							box[15] = 0;							output[4] = ' ';							output[15] = ' ';							check = 1;						}						if(box[5] + box[24] != 2 && check == 0)						{							box[5] = 0;							box[24] = 0;							output[5] = ' ';							output[24] = ' ';							check = 1;						}						if(box[6] + box[14] != 2 && check == 0)						{							box[6] = 0;							box[14] = 0;							output[6] = ' ';							output[14] = ' ';							check = 1;						}						if(box[8] + box[22] != 2 && check == 0)						{							box[8] = 0;							box[22] = 0;							output[8] = ' ';							output[22] = ' ';							check = 1;						}						if(box[10] + box[27] != 2 && check == 0)						{							box[10] = 0;							box[27] = 0;							output[10] = ' ';							output[22] = ' ';							check = 1;						}						if(box[11] + box[23] != 2 && check == 0)						{							box[11] = 0;							box[23] = 0;							output[11] = ' ';							output[23] = ' ';							check = 1;						}						if(box[12] + box[21] != 2 && check == 0)						{							box[12] = 0;							box[21] = 0;							output[12] = ' ';							output[21] = ' ';							check = 1;						}						if(box[13] + box[19] != 2 && check == 0)						{							box[13] = 0;							box[19] = 0;							output[13] = ' ';							output[19] = ' ';							check = 1;						}						if(box[17] + box[18] != 2 && check == 0)						{							box[17] = 0;							box[18] = 0;							output[17] = ' ';							output[18] = ' ';							check = 1;						}						if(box[20] + box[25] != 2 && check == 0)						{							box[20] = 0;							box[25] = 0;							output[20] = ' ';							output[25] = ' ';							check = 1;						}					}


The code within these if blocks is what resets the display. After the program goes into any of these if blocks it will not go into any of the if blocks that follow it because you're setting check=1. I'm guessing you're setting check = 1 because you're thinking that after checking the user's input once you don't need to worry about the remaining if blocks so you want to skip the rest for efficiency. However, the user's input is not likely to be two matching boxes which is what your if statements are looking at. (Sorry if i'm not reading your mind correctly.)

Does Visual Express (or whatever you're using) have a debug mode that lets you trace through your program? If so it'd be worth the time to learn it. You'd be able to follow the path the program actually takes through the code. Also, couldn't hurt to get in the habbit of commenting stuff early on. It helps us other programmers get an idea of what it is you expected your code to do.

My logic was that, it would check to see if the input is any correct number for any of the boxes on the grid, which is what the switch is for. Then, I wanted it to check when there was two numbers or letters or both on the grid to see if they match, and if they don't, reset them back to a space, and then start over again. Yes, it's a match two game.
Quote:Original post by Lordimm
My logic was that, it would check to see if the input is any correct number for any of the boxes on the grid, which is what the switch is for. Then, I wanted it to check when there was two numbers or letters or both on the grid to see if they match, and if they don't, reset them back to a space, and then start over again. Yes, it's a match two game.


I'm pretty sure that if you take out the check variable that it'll work.

You need to be sure that when the user does not select matching boxes that both of the boxes that the user selected are being reset. What the code is currently doing is resetting boxes that are paired with each other if they both haven't been selected which is not the same thing as reseting the boxes that the user selected. Now, that would be fine so long as you're testing each and every pair. But the code is currently testing the first pair, resetting it (assuming it's not what the user enters), then it skips the rest because the check variable is no longer 0. If the check variable wasn't there, it would reset each box that hasn't already been matched.
Alright, I got it working. I removed the check variable, and messed around with the output, and now it's working perfectly. Thanks to everyone who helped =).

This topic is closed to new replies.

Advertisement