so i guess there must be some sort of an error in my logic but on line 119 i started a counter that only goes up when a pair is made in the game. Since there are a total of 8 pairs, i set that when the counter is 8 the bool nullexists turns false. when it is false it should skip the while loop and output that you have won.

Any ideas on what is actually wrong here? I did check and the counter is going up at the correct times

here is the code

//*******************************************************************************************************************
// Homework 5
// 
//     
//     
//
// This program is a matching game.  The user uses a cordinant system to select
// cards on a 4x4 board.
// Input:			the user inputs the cordinants of the cards they want to flip
// Processing: 		the program randomly generates pairs of cards in random locations
//					on the 4x4 board.  it also adjusts the user input so that the
//					cordinants match with the matrix counting system.
// Output: 			the game board, the cards fliped, if they match, and the next board piece
//*******************************************************************************************************************


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    char comma;
    int row1, row1a, column1, column1a, row2, row2a, column2, column2a, cards[4][4], cards_unflipped[4][4] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} , counter(0), g(0);
	bool nullexists = true;
    srand((unsigned)time(NULL));
   
    //fill board with pairs
    bool makeNext = false;
    memset(cards, 0, sizeof(cards));
    srand(time(NULL));

        for( int i = 1; i < 9; i++ )
            {
                makeNext = false;
                while(!makeNext)
                    {
                        int x1, y1, x2, y2;
                        x1 = rand()%4;
                        y1 = rand()%4;
                        x2 = rand()%4;
                        y2 = rand()%4;
                        if( (x1 != x2 || y1 != y2) && (cards[x1][y1] == 0 && cards[x2][y2] == 0) )
                        {
                            cards[x1][y1] = i;
                            cards[x2][y2] = i;
                            makeNext = true;;
                        }
                    }
            }
       
                   

        // initial display board
        cout<<"    1 2 3 4\n";
        cout<<"  ";
        for (int i=0; i<=8; i++)
        {
            cout<<"-";
        }

        cout<<endl;
  
        for (int r=0; r<4; r++)
        {
			cout<<r+1<<" | ";
            for (int c=0; c<4; c++)
            {
				cout<<cards_unflipped[r][c]<<" ";
            }
			cout<<endl;
        }



   //Game Loop --------------------------------------------------------------------------------
   while (nullexists = true)

   {
     //selection of cards
    cout << "Please select the first card row and column seperated by a comma.\n";
    cin >> row1a >> comma >> column1a;

    cout<<"Please select the second card row and column seperated by a comma.\n";
    cin>>row2a>>comma>>column2a;
       
        // adjusting for matrix cordinants
        row1 = row1a - 1;
        row2 = row2a - 1;
        column1 = column1a - 1;
        column2 = column2a - 1;

   
        // make sure that the cordinants are on the board
        if (row1a > 4 || row2a > 4 || column1a > 4 || column2a > 4 || row1a < 1 || row2a < 1 || column1a < 1 || column2a < 1)
        {
            cout << "please pick cordinants on the board." << endl <<endl;
       
            cout << "Please select the first card row and column seperated by a comma.\n";
            cin >> row1a >>comma >> column1a;

            cout << "Please select the second card row and column seperated by a comma.\n";
            cin >> row2a >> comma >> column2a;

        }
     
        // cards chosen
        cout << "The cards you picked were " << cards[row1][column1] << " and " << cards[row2][column2] << endl;
     
       if (cards[row1][column1]== cards[row2][column2])

       {
            cards_unflipped[row1][column1] = cards[row1][column1];
            cards_unflipped[row2][column2] = cards[row2][column2];


            counter = counter++;
                if (counter == 8)
                {
                    bool nullexists = false;
                }

				if (bool nullexists = false)
				{
					cout << " Congrats, You Won!" << endl;
						break;
				}

				
			cout << "Press n to continue." << endl;
			while (1)
			 {
			   if (getchar() == 'n')
			   {
			      break;
				  }
			   }

		 // move to blank screen
		 for (g=0; g <= 20; g++)
			{
			    cout << endl;
		  }


    //reveal the flipped cards
    cout<<"    1 2 3 4\n";
    cout<<"  ";
    for (int i=0; i<=8; i++)
    {
        cout<<"-";
    }
    cout<<endl;
    for (int r=0; r<4; r++)
    {
        cout<<r+1<<" | ";
        for (int c=0; c<4; c++)
        {
               
                cout<<cards_unflipped[r][c]<<" ";
            }
        cout<<endl;
        }

       }
   
       else
       {
       
            cards_unflipped[row1][column1] = cards[row1][column1];
            cards_unflipped[row2][column2] = cards[row2][column2];
           

			//reveal the flipped cards
			cout<<"    1 2 3 4\n";
			cout<<"  ";
			for (int i=0; i<=8; i++)
			{
				cout<<"-";
			}
			cout<<endl;
			for (int r=0; r<4; r++)
			{
				cout<<r+1<<" | ";
				for (int c=0; c<4; c++)
				{
					cout<<cards_unflipped[r][c]<<" ";
				}

				cout<<endl;
			}

		// pause for user to look at the cards they flipped
		cout << "Press n to continue." << endl;
		while (1)
		{
			if (getchar() == 'n')
			{
				break;
			}
		}

		//reset flipped cards


		cards_unflipped[row1][column1] = 0;
        cards_unflipped[row2][column2] = 0;


		// move to blank screen

		for (g=0; g <= 20; g++)
		{
			cout << endl;
		}

		//reveal the flipped cards
		cout<<"    1 2 3 4\n";
		cout<<"  ";
		for (int i=0; i<=8; i++)
		{
			cout<<"-";
		}
		cout<<endl;
		for (int r=0; r<4; r++)
		{
        cout<<r+1<<" | ";
			for (int c=0; c<4; c++)
			{            
				cout<<cards_unflipped[r][c]<<" ";
            }
			cout<<endl;
        }

	   }
       
    }

	//GAME LOOP END -------------------------------------------------------------------------------

   cout << "You Won!" << endl;
 

    return 0;
}

Recommended Answers

All 12 Replies

It might have something to do with while (nullexists = true) . Do you mean == ?

Ahh that must be it thanks!

Heres another thing.
You are making the variable bool nullexists up at the top and then when you are doing anything with it you remake a new bool variable with the same name.

Line numbers are based off first post.

Line 26: bool nullexists = true; This is fine.

Line 78: while(nullexists = true) change to -> while(nullexists == true) Line 121: bool nullexists = false; change to -> nullexists = false; Line 124: if(bool nullexists = false) change to -> if(nullexists == false) Here is another way of writing "if(a == true)" and "if(a == false)". if(a) <- if it is true if(!a) <- if it is false
This works for bool variables.

You even have it from the code I provided with "makeNext".

nope still a no go, any other ideas?

I'm not saying that will fix it I'm just saying thats a problem with your code. I don't really like trying to fix up code that is poorly formatted but I'll give this a try since I helped you with part of it already.

thanks sfuo, i apparently need to brush up on my boolean

I got it to break the loop and quit the program but if you pick a card that has already got picked it will flip it back to 0.

The two things that I wanna ask you are:
1- Are you allowed to use functions?
2- Can I rewrite this for you and add comments showing you how to have a better structure?

ya sry that was the only problem, i missed your post before sry bout that

sufo, that would be great, yes we are allowed functions. I do have to turn it in shortly but im going to turn in my own work. It would be nice to see what i should be doing differently.

Yeah submit what you have because it works I will just post pretty much the same thing but with using functions and I'll clean up some parts.

6 hours later I'm back!
Sorry, I got side tracked but anyways I managed to finished a version using functions (I could have made it using a class but I decided not to).
This is half as many lines as the other version you have.

#include <iostream>

using namespace std;

struct CARD
{
	bool isFlipped;
	int value;
};

void makeCardSet(CARD cards[4][4])
{
	bool makeNext = false;
    srand(time(NULL));
    
    for( int i = 0; i < 4; i++ )
		for( int c = 0; c < 4; c++ )
			cards[i][c].value = 0;
			
    for( int i = 1; i < 9; i++ )
    {
        makeNext = false;
        while(!makeNext)
        {
            int x1, y1, x2, y2;
            x1 = rand()%4;
            y1 = rand()%4;
            x2 = rand()%4;
            y2 = rand()%4;
            if( (x1 != x2 || y1 != y2) && (cards[x1][y1].value == 0 && cards[x2][y2].value == 0) )
            {
                cards[x1][y1].value = i;
                cards[x1][y1].isFlipped = false;
                cards[x2][y2].value = i;
                cards[x2][y2].isFlipped = false;
                makeNext = true;
            }
        }
    }	
}

void display(CARD cards[4][4])
{
	cout << endl << "  1 2 3 4" << endl;
	for( int i = 0; i < 4; i++ )
	{
		cout << i+1 << " ";
		for( int c = 0; c < 4; c++ )
			if( cards[i][c].isFlipped == true )
				cout << cards[i][c].value << " ";
			else
				cout << "0 ";
		cout << endl;
	}
	cout << endl;
}

int main()
{
	bool done = false, valid;
	int row[2], col[2], pairs = 0;
	char divider;
	CARD cards[4][4];
	
	makeCardSet(cards);
	
	while(!done)
	{
		display(cards);
		for( int i = 0; i < 2; i++ )
		{
			cout << "Input row and column you would like to flip (ex 2,1):" << endl;
			do
			{
				valid = false;
				cin >> row[i] >> divider >> col[i];
				if( row[i] <= 0 || row[i] > 4 || col[i] <= 0 || col[i] > 4 )
					cout << "Input numbers within the grid!" << endl;
				else if( cards[row[i]-1][col[i]-1].isFlipped )
					cout << "That card is already flipped!" << endl;
				else if( i == 1 && row[1] == row[0]+1 && col[1] == col[0]+1 )
					cout << "You just picked that card!" << endl;
				else
					valid = true;
			}while(!valid);
			
			row[i]--;
			col[i]--;
			
			if( i == 0 )
				cout << "You flipped a " << cards[row[i]][col[i]].value << "!" << endl;
				
		}
		cards[row[0]][col[0]].isFlipped = true;
		cards[row[1]][col[1]].isFlipped = true;
		
		display(cards);
		
		if( cards[row[0]][col[0]].value != cards[row[1]][col[1]].value )
		{
			cards[row[0]][col[0]].isFlipped = false;
			cards[row[1]][col[1]].isFlipped = false;
			cout << "Not a match!" << endl;
		}
		else
		{
			pairs++;
			cout << "You found a pair!" << endl;
			
			if( pairs == 8 )
			{
				done = true;
				cout << "You Won!" << endl;
			}				
		}
		
		system("PAUSE"); //change this to some other thing if you aren't using windows
		for( int i = 0; i < 25; i++ )
			cout << endl;
	}
	
	return 0;
}

yes that is a lot cleaner. Thanks for writing this out for me. Im going to look it over in more detail in a bit, right now, study for exam!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.