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;
}