Memory Game not Working

Started by
15 comments, last by Lordimm 15 years, 4 months ago
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]
Advertisement
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.

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

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.
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.

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

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.
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.

 codecodecodecodecodecode 


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

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

But do you have any idea what's wrong with it?
Quote:Original post by Lordimm
But do you have any idea what's wrong with it?


Can you be more specific of what the exact problem is, does it compile?

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

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.
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.

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

This topic is closed to new replies.

Advertisement