Sign in to follow this  
Lordimm

Memory Game not Working

Recommended Posts

Lordimm    122
I just finished my memory game today and for some reason it's not working...and I have no clue. Can someone please help? (Also, can someone tell me how to get those nice little windows full of code where you can scroll through? instead of having a huge page full of code)
#include <iostream>
using namespace std;

// Memory game

int main()
{
	int choice = 0;

	cout << "Welcome to the memory game!\n\n";
	while(choice != 3)
	{
		cout << endl;

		cout << "What would you like to do?\n\n";
		cout << "1: Play\n";
		cout << "2: Instructions\n";
		cout << "3: Quit\n";
		cout << ">";

		cin >> choice;

		switch(choice)
		{
		case 1:
			{
				// Twenty-eight variables for each box
				int box[28] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

				// Twenty-eight variables for each character in the boxes
				char output[28] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
					' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',};

				int win = 0;
				int count = 0;

				while(win == 0)
				{
					int input = 0;
					
					cout << endl;

					if(count == 3)
						count = 1;
				
					cout << "  +-+-+-+-+-+-+-+\n";
					cout << "  I" << output[0] << "I" << output[1] << "I" << output[2] << "I" << output[3] << "I" << output[4] << "I" << output[5] << "I" << output[6] << "I\n";
					cout << "  +-+-+-+-+-+-+-+\n";
					cout << "  I" << output[7] << "I" << output[8] << "I" << output[9] << "I" << output[10] << "I" << output[11] << "I" << output[12] << "I" << output[13] << "I\n";
					cout << "  +-+-+-+-+-+-+-+\n";
					cout << "  I" << output[14] << "I" << output[15] << "I" << output[16] << "I" << output[17] << "I" << output[18] << "I" << output[19] << "I" << output[20] << "I\n";
					cout << "  +-+-+-+-+-+-+-+\n";
					cout << "  I" << output[21] << "I" << output[22] << "I" << output[23] << "I" << output[24] << "I" << output[25] << "I" << output[26] << "I" << output[27] << "I\n";
					cout << "  +-+-+-+-+-+-+-+\n\n";
	
					cout << "Pick a spot.\n";
					cout << ">";
							
					cin >> input;

					switch(input)
					{
					case 1:
						{
							box[0] = 1;
							output[0] = '5';
							break;
						}
					case 2:
						{
							box[1] = 1;
							output[1] = '2';
							break;
						}
					case 3:
						{
							box[2] = 1;
							output[2] = '7';
							break;
						}
					case 4:
						{
							box[3] = 1;
							output[3] = 'A';
							break;
						}
					case 5:
						{
							box[4] = 1;
							output[4] = '3';
							break;
						}
					case 6:
						{
							box[5] = 1;
							output[5] = 'E';
							break;
						}
					case 7:
						{
							box[6] = 1;
							output[6] = 'D';
							break;
						}
					case 8:
						{
							box[7] = 1;
							output[7] = '5';
							break;
						}
					case 9:
						{
							box[8] = 1;
							output[8] = '1';
							break;
						}
					case 10:
						{
							box[9] = 1;
							output[9] = '2';
							break;
						}
					case 11:
						{
							box[10] = 1;
							output[10] = '4';
							break;
						}
					case 12:
						{
							box[11] = 1;
							output[11] = 'C';
							break;
						}
					case 13:
						{
							box[12] = 1;
							output[12] = 'B';
							break;
						}
					case 14:
						{
							box[13] = 1;
							output[13] = '6';
							break;
						}
					case 15:
						{
							box[14] = 1;
							output[14] = 'D';
							break;
						}
					case 16:
						{
							box[15] = 1;
							output[15] = '3';
							break;
						}
					case 17:
						{
							box[16] = 1;
							output[16] = 'A';
							break;
						}
					case 18:
						{
							box[17] = 1;
							output[17] = 'F';
							break;
						}
					case 19:
						{
							box[18] = 1;
							output[18] = 'F';
							break;
						}
					case 20:
						{
							box[19] = 1;
							output[19] = '6';
							break;
						}
					case 21:
						{
							box[20] = 1;
							output[20] = 'G';
							break;
						}
					case 22:
						{
							box[21] = 1;
							output[21] = 'B';
							break;
						}
					case 23:
						{
							box[22] = 1;
							output[22] = '1';
							break;
						}
					case 24:
						{
							box[23] = 1;
							output[23] = 'C';
							break;
						}
					case 25:
						{
							box[24] = 1;
							output[24] = 'E';
							break;
						}
					case 26:
						{	
							box[25] = 1;
							output[25] = 'G';
							break;
						}
					case 27:
						{
							box[26] = 1;
							output[26] = '7';
							break;
						}
					case 28:
						{
							box[27] = 1;
							output[27] = '4';
							break;
						}
					}

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

					count++;

					if(box[0] + box[1] + box[2] + box[3] + box[4] + box[5] + box[6] + box[7] + box[8] + box[9] + box[10] + 
						box[11] + box[12] + box[13] + box[14] + box[15] + box[16] + box[17] + box[18] + box[19] + box[20] + 
						box[21] + box[22] + box[23] + box[24] + box[25] + box[26] + box[27] == 28)
					{
						cout << "  +-+-+-+-+-+-+-+\n";
						cout << "  I" << output[0] << "I" << output[1] << "I" << output[2] << "I" << output[3] << "I" << output[4] << "I" << output[5] << "I" << output[6] << "I\n";
						cout << "  +-+-+-+-+-+-+-+\n";
						cout << "  I" << output[7] << "I" << output[8] << "I" << output[9] << "I" << output[10] << "I" << output[11] << "I" << output[12] << "I" << output[13] << "I\n";
						cout << "  +-+-+-+-+-+-+-+\n";
						cout << "  I" << output[14] << "I" << output[15] << "I" << output[16] << "I" << output[17] << "I" << output[18] << "I" << output[19] << "I" << output[20] << "I\n";
						cout << "  +-+-+-+-+-+-+-+\n";
						cout << "  I" << output[21] << "I" << output[22] << "I" << output[23] << "I" << output[24] << "I" << output[25] << "I" << output[26] << "I" << output[27] << "I\n";
						cout << "  +-+-+-+-+-+-+-+\n\n";

						win = 1;

						cout << "You got them all right!\n";
					}
				}
				break;
			}
		case 2:
			{
				break;
			}
		case 3:
			{
				cout << "Thanks for playing!\n";
				break;
			}
		}
	}


	return 0;
}

What it does is after the third turn, it only takes away one character, instead of both of the ones you put in, and after that, all the characters stay. [Edited by - Lordimm on December 18, 2008 1:14:08 AM]

Share this post


Link to post
Share on other sites
VanillaSnake21    175
Quote:
Original post by Lordimm
Also, can someone tell me how to get those nice little windows full of code where you can scroll through? instead of having a huge page full of code


I'm not sure what you mean by little windows but if you want to break you code up into multiple files, so you don't have one huge file, create a header file (.h extension) and another cpp file (either .cpp or .cc extensions) then place your code into functions, declare the functions in the header file and define them in the cpp file. Then #include your header file with that function into the file where you have int main(). Then you can use all the functions within that file.
Clicky Hope this helps [smile]

As for your game, what exactly is not working? Is it crashing is it giving you a wrong output? Be a little bit more specific.

Share this post


Link to post
Share on other sites
Lordimm    122
Lol, that's not what I meant - on these forums, I've seen windows with a scroller and code in it. And it's not outputting right. The first and second times you input a value, it works correctly, but the third time it is supposed to take away the characters if you get the two guesses wrong. But what it does it take away the first value, and keeps the second, then every output after it never takes it away.

Share this post


Link to post
Share on other sites
VanillaSnake21    175
Quote:
Original post by Lordimm
Lol, that's not what I meant - on these forums, I've seen windows with a scroller and code in it.


I guess that would depend on the environment you're working in. What IDE are you using. Visual Express C++ (free) has scrollers, but I actually don't know any IDEs that don't have that.

Share this post


Link to post
Share on other sites
Lordimm    122
No, I'm not talking about Visual Studio, I'm talking about these forums - I've seen posts that have scrollers with code in it, instead of having huge pages of code on one page on this forum.

Share this post


Link to post
Share on other sites
VanillaSnake21    175
Quote:
Original post by Lordimm
No, I'm not talking about Visual Studio, I'm talking about these forums - I've seen posts that have scrollers with code in it, instead of having huge pages of code on one page on this forum.


Ohhhh, sorry I misunderstood what you were trying to say, just use this

source lang="c" your text here /source

except put [] around source and /source, press edit on my post to see it.

 
code


code


code


code


code


code




Check out this Gamedev FAQ it has a lot of info on how to use different forum tags and smileys Here

Share this post


Link to post
Share on other sites
Lordimm    122
Above I elaborated, you posted right after I edited though, so you probably didn't see it. I'll re-post here: "The first and second times you input a value, it works correctly, but the third time it is supposed to take away the characters if you get the two guesses wrong. But what it does it take away the first value, and keeps the second, then every output after it never takes it away." Actually, it only takes away the first character, the other ones stay.

Share this post


Link to post
Share on other sites
VanillaSnake21    175
Quote:
Original post by Lordimm
Above I elaborated, you posted right after I edited though, so you probably didn't see it. I'll re-post here: "The first and second times you input a value, it works correctly, but the third time it is supposed to take away the characters if you get the two guesses wrong. But what it does it take away the first value, and keeps the second, then every output after it never takes it away." Actually, it only takes away the first character, the other ones stay.


It seems to be because
if(box[0] + box[7] != 2) always evaluates to true (meaning it does not add up to 2) and all your other cases are ignored because they are else ifs, which only execute if the preceding if was false.

Share this post


Link to post
Share on other sites
Lordimm    122
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.

Share this post


Link to post
Share on other sites
VanillaSnake21    175
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 ?

Share this post


Link to post
Share on other sites
rip-off    10979
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.

Share this post


Link to post
Share on other sites
kseh    3840

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.

Share this post


Link to post
Share on other sites
Lordimm    122
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.

Share this post


Link to post
Share on other sites
kseh    3840
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.

Share this post


Link to post
Share on other sites
Lordimm    122
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 =).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this