Hi guys. I have been working on this program for HOURS, remind me to never try to code something like this in a straight amount of time ever again. Anyways, the program is the same game basically, except it not only shifts numbers down, but left too.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// Allows user to input the number of the row and column they wish to remove
void chooseNumber(int& gameRow, int& gameColumn)
{
int quit;
cout << "Would you like to quit? Type 1 to quit, and 0 to continue." << endl;
cin >> quit;
if(quit == 1)
{
exit(0);
}
cout << "Please enter two values, the row and column of the cell "
<< "you would like to pull from the gameboard." << endl;
cin >> gameRow;
cin >> gameColumn;
while( gameRow < 1 && gameRow > 9)
{
cout << "Please enter a number between 1 and 9 for the "
<< "row." <<endl;
cin >> gameRow;
}
while ( gameColumn < 1 && gameColumn > 9)
{
cout << "Please enter a number between 1 and 9 for the "
<< "column." << endl;
cin >> gameColumn;
}
}
void displayBoard(int gameBoard[15][12])
{
cout << "Here is the board. \n" << endl;
for(int i= 0; i < 15; i++)
{
for(int column= 0; column < 12; column++)
{
cout << gameBoard[i][column];
}
cout << endl;
}
}
void shift(int gameBoard[15][12])
{
bool zeroFound;
do
{
zeroFound = false;
for(int i = 0; i < 15; i++)
{
for(int column = 0; column < 12; column++)
{
if(gameBoard[i][column] == 0 && gameBoard[i-1][column] != 0 && i != 0)
{
gameBoard[i][column] = gameBoard[i-1][column];
gameBoard[i-1][column] = 0;
zeroFound = true;
}
}
}
}
while(zeroFound);
}
void shiftColumnLeft(int gameBoard[15][12], int gameRow, int gameColumn)
{
bool change;
do
{
change = false;
for(int column = 0; column < 12; column++)
{
for (int i = 0 ; i < 15; i++)
{
if(gameBoard[i][column] == 0 && gameBoard[i][column+1] !=0 && column != 11)
{
gameBoard[i][column] = gameBoard[i][column+1];
gameBoard[i][column+1] = 0;
change = true;
}
}
}
}
while(change);
}
void winOrLose(int gameBoard[15][12])
{
bool move;
int tempNum;
do
{
move = false;
for(int i = 0 ; i < 15; i++)
{
for(int column = 0 ; column < 12; column++)
{
if( gameBoard[i][column] > 0)
{
tempNum = gameBoard[i][column];
if(gameBoard[i+1][column] == tempNum ||gameBoard[i-1][column] == tempNum ||
gameBoard[i][column-1] == tempNum || gameBoard[i][column+ 1] == tempNum)
{
move = true;
}
}
}
}
}
while(move);
if(move = false)
{
for(int i = 0; i <15 ; i++)
{
for(int column = 0; column < 12; column++)
{
if(gameBoard[i][column] != 0)
{
cout << "YOU LOSE!"<< endl;
exit(0);
}
}
}
cout << "YOU WIN!";
}
}
void deleteCell(int gameBoard[15][12], int& gameRow, int& gameColumn)
{
if ( gameBoard[gameRow][gameColumn] == 0)
{
cout << "Nothing there." << endl;
chooseNumber(gameRow,gameColumn);
}
bool changeMade = false;
int tempNum = gameBoard[gameRow][gameColumn];
gameBoard[gameRow][gameColumn] = 0;
if(gameBoard[gameRow+1][gameColumn] == tempNum ||gameBoard[gameRow-1][gameColumn] == tempNum ||
gameBoard[gameRow][gameColumn-1] == tempNum || gameBoard[gameRow][gameColumn + 1] == tempNum)
{
do
{
changeMade = false;
for(int i = 0; i <15 ; i++)
{
for(int column = 0; column <12 ; column++)
{
if( gameBoard[i][column] == tempNum &&(gameBoard[i+1][column] == 0 ||gameBoard[i-1][column] == 0 ||
gameBoard[i][column-1] == 0 || gameBoard[i][column+1] == 0))
{
gameBoard[i][column] = 0;
changeMade = true;
}
}
}
}
while(changeMade);
}
else
{
cout << "Not valid move!" << endl;
chooseNumber(gameRow,gameColumn);
deleteCell(gameBoard,gameRow,gameColumn);
}
shift(gameBoard);
shiftColumnLeft(gameBoard,gameRow,gameColumn);
displayBoard(gameBoard);
winOrLose(gameBoard);
chooseNumber(gameRow,gameColumn);
deleteCell(gameBoard, gameRow, gameColumn);
}
int main()
{
int gameBoard[15][12];
int gameRow = 0;
int gameColumn = 0;
srand ( time(NULL) );
cout << "Welcome to the Same Game. The rules are as follows: "
<< "You will be given a 15x12 grid filled with "
<< "random integers spanning from 1 to 9. \n The object of "
<< "the game is to eliminate 'cells', or two or more "
<< "similar integers connected together vertically or "
<< " horizontally until the entire board is clear. \nA small "
<< "twist on the game is that the numbers will shift to "
<< " their left after dropping if there is a vacant spot. \n If "
<< "you run out of moves, then it's game over. 0's will "
<< "stand for blanks. \n" << endl;
for(int i= 0; i < 15; i++)
{
for(int column= 0; column < 12; column++)
{
gameBoard[i][column] = rand() % 9 + 1;
}
cout << endl;
}
displayBoard(gameBoard);
chooseNumber(gameRow, gameColumn);
deleteCell(gameBoard,gameRow,gameColumn);
}
It compiles fine, no error messages, nada. However, I can't get it to tell me I win or lose depending on how the game goes. The function in question is winOrLose. Thank you so much for your time, I am exhausted beyond all measure.