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.

Recommended Answers

All 17 Replies

chooseNumber() only permits the player to enter a value between 1 and 9. Yet the matrix is 15x12. Why the difference ? 1 and 9 are the values in the cells, not the matrix size.

Thanks for catching that, that should of been 15 and 12 as you said. Any insight on winOrLose?

Another suggestion: don't hard-code the array dimensions all over the place like that. If you ever have to change it chances are pretty good that you will make a mistake somewhere and it will require a considerable amount of debugging.

The solution to that problem is to define const integers at the top of the program then use those constants whenever needed. That way there is only one place in the program to change the array dimensions.

Well, I compiled with VC++ 2005 Express and get one warning

'deleteCell' : recursive on all control paths, function will cause runtime stack overflow

You need to fix that because if that function is called it will crash your program.

[edit]
move gets set to true at line 118, which means the test at line 126 is never true.

Well, I compiled with VC++ 2005 Express and get one warning


You need to fix that because if that function is called it will crash your program.

I don't understand, it works just fine when I run the program. Also, the assignment specified that the array be that size, thus the only reason why I didn't make it changeable.


EDIT: Saw your edit, checking out my code.

What that function should be checking on is whether any number in the array neighbors an equal value. If so, a flag is marked, and the game should continue. If there are no matches, but every number equals one another (ie everything is 0), then it should display win. If it finds a value(s) that is/are not 0, it checks to see if they have any matching neighbors, which should set the flag off and let play to continue.
If none of the remaining numbers have a partner it should display you lose.

Therefore, how would it always flag true? It was at least my intention that it flag only if the above conditions are true, lol.

I see how it is now an infinite loop, it will just run through the same value over and over when it goes back through, I will post up something new if I find a sol'n.

Updated code:

#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 < 0 && gameRow > 14)
        {                               
                cout << "Please enter a number between 0 and 14 for the "
                     << "row." <<endl;
                cin >> gameRow;
        }
                        
        while ( gameColumn < 0 && gameColumn > 11)
        {
                cout << "Please enter a number between 0 and 11 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)
{
                for(int column = 0; column < 12; column++)
                {
                                if(gameBoard[14][column] == 0)
                                {
                                        for(int i = 0; i < 15 ; i++)
                                        {
                                                gameBoard[i][column] = gameBoard[i][column+1];
                                                gameBoard[i][column+1] = 0;
        
                                        }
                                }
                }
}
                                        
void winOrLose(int gameBoard[15][12], bool& moveDone)
{
        if( gameBoard[14][0] == 0)
        {        
                cout << "YOU WIN!" << endl;
               exit(0); 
        }
        if( gameBoard[14][0] != 0 && moveDone  == false)
        {
                cout << "GAME OVER!" << endl;
                exit(0);
        }
}
void deleteCell(int gameBoard[15][12], int& gameRow, int& gameColumn, bool& changeMade, bool& moveDone)
{
        if ( gameBoard[gameRow][gameColumn] == 0)
        {
                cout << "Nothing there." << endl;
                chooseNumber(gameRow,gameColumn);
        }
                                                
        changeMade = false;
        moveDone = 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][co$
                                                gameBoard[i][column-1] == 0 || gameBoard[i][column+1] == 0))
                                                {
                                                        gameBoard[i][column] = 0;
                                                        changeMade = true;
                                                        moveDone = true;
                                                }
         
                                        }       
                                }
                        }
                        while(changeMade);
                 
                }
        else
        {
                        cout << "Not valid move!" << endl;
                        chooseNumber(gameRow,gameColumn);
                        deleteCell(gameBoard,gameRow,gameColumn,changeMade, moveDone);
        }       

shift(gameBoard); 
        shiftColumnLeft(gameBoard,gameRow,gameColumn);
        displayBoard(gameBoard);
        winOrLose(gameBoard, changeMade);
        chooseNumber(gameRow,gameColumn);
        deleteCell(gameBoard, gameRow, gameColumn, changeMade,moveDone);
                                         
                                                
                                                
                                                 
}
int main()
                                                        
{
        int gameBoard[15][12];
        int gameRow = 0;
        int gameColumn = 0;
        bool changeMade, moveDone;
        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. \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;
        }
        for(int i = 0; i < 15 ; i++)
        {
                gameBoard[i][0] = 0 ;
        }
        displayBoard(gameBoard); 
        chooseNumber(gameRow, gameColumn);
        deleteCell(gameBoard,gameRow,gameColumn,changeMade, moveDone);
}

I am still unsure how to put line #s, I couldnt find it. :(
My shiftLeft function does not work, for some reason, the last statement scrambles my entire array, and my winOrLose statement only works if you win. Any ideas would be gladly appreciated

Still having trouble, I tried updating the code with bounds checking, but to no avail.

ie

454
454
454

should be

054
054
054

but I get

050
050
050

here is updated code:

#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);
        //}             
        // prompt user for input
        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;
        cout <<  gameRow;               
        cout <<  gameColumn;

        // error checking for user input
        while( gameRow < 0 && gameRow > 3)
        {
                cout << "Please enter a number between 0 and 14 for the "
                     << "row." <<endl;
                cin >> gameRow;
        }

        while ( gameColumn < 0 && gameColumn > 3)
        {
                cout << "Please enter a number between 0 and 11 for the "
                     << "column." << endl;
                cin >> gameColumn;
        }

}

// print board to user
void displayBoard(int gameBoard[3][3])
{

        cout << "Here is the board. \n" << endl;

                for(int i= 0; i < 3; i++)
                {
                        for(int column= 0; column < 3; column++)
               for(int column= 0; column < 3; column++)
                        {   

                        //      cout <<  gameBoard[i][column];
                                cout <<  gameBoard[i][column];
                        }
                        cout << endl;
                }

}
// if there is a 0 below a number in a column, swap the two numbers
void shift(int  gameBoard[3][3])
{
        bool zeroFound;
        do
        {
                zeroFound = false;
                for(int i = 0; i < 3; i++)
                {
                        for(int column = 0; column < 3; 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);

}
// if there is a column of 0's, replace that column with the column to the right of it.
void shiftColumnLeft( int gameRow, int gameColumn, int gameBoard[3][3])
{
        bool changeMade;
        do{
                changeMade = false;
                for(int column = 0 ; column < 3; column++)
                {
                                if(gameBoard[2][column] == 0 && column != 0)
                                {
                                        for(int i = 0; i < 3 ; i++)
                                        {
                                                gameBoard[i][column] = gameBoard[i][column-1];
                                                gameBoard[i][column-1] = 0;
                                                changeMade = true;
                                        }
                                }
                }
           }
        while(changeMade);

}
// declares whether a user has won or lost the game if either situation occurs
void winOrLose( bool& moveDone, int gameBoard[3][3])
{
        if( gameBoard[2][0] == 0)
        {               
                cout << "YOU WIN!" << endl;
                exit(0);
        }
        if( gameBoard[2][0] != 0 && moveDone  == false)
        {
                cout << "GAME OVER!" << endl;
                exit(0);
        }
}
// deletes the user defined cells

void deleteCell(int& gameRow, int& gameColumn, bool& changeMade, bool& moveDone,int gameBoard[3][3])
{
        // error checking to make sure that the value exists
        if ( gameBoard[gameRow][gameColumn] == 0)
        {
                cout << "Nothing there." << endl;
                chooseNumber(gameRow,gameColumn);
        }

        changeMade = false;
        moveDone = false;               


        int tempNum;
       tempNum = gameBoard[gameRow][gameColumn];
        gameBoard[gameRow][gameColumn] = 10;


                        do
                        {
                                changeMade = false;
                                for(int i = 0; i < 3 ; i++)
                                {
                                        for(int column = 0; column <3; column++)
                                        {


                                                        if( gameBoard[i][column] == tempNum)
                                                        {
                                                                if(i == 0 && column == 0)
                                                                {
                                                                        if(gameBoard[i+1][column] == 10 || gameBoard[i][column+1] == 10)
                                                                        {

                                                                                cout << " executed";
                                                                                gameBoard[i][column] = 10;
                                                                                changeMade = true;
                                                                                moveDone = true;
                                                                                displayBoard(gameBoard);
                                                                        }
                                                                }
                                                                if(i == 0 && column != 0)
                                                                {
                                                                        if(gameBoard[i+1][column] == 10 || gameBoard[i][column-1] == 10 || gameBoard[i][column+1] == 10)
                                                                        {
                                                                                cout << "executed1";
                                                                                gameBoard[i][column] = 10;
                                                                                changeMade = true;
                                                                                moveDone = true;
                                                                                displayBoard(gameBoard);
                                                                        }
                                                                }
                                                                if( column == 0 && i != 0)
                                                                {
                                                                        if(gameBoard[i+1][column] == 10 || gameBoard[i-1][column] == 10 || gameBoard[i][column+1] == 10)
                                                                        {
                                                                                cout << "executed2";
                                                                                gameBoard[i][column] = 10;
                                                                                changeMade = true;
                                                                                moveDone = true;
                                                                                displayBoard(gameBoard);
                                                                        }
                                                                }

                                                                if( i == 2 && column == 2)
                                                                {
                                                                        if(gameBoard[i-1][column] == 10 || gameBoard[i][column-1] == 10)
                                                                        {
                                                                                cout << "executed3";
                                                                                gameBoard[i][column] = 10;
                                                                                changeMade = true;
                                                                                moveDone = true;
                                                                                displayBoard(gameBoard);
                                                                        }
                                                                }
                                                                if(i == 2 && column != 2)
                                                                {
                                                                        if(gameBoard[i-1][column] == 10 || gameBoard[i][column-1] == 10 || gameBoard[i][column+1] == 10)
                                                                        {
                                                                                cout <<  "executed4";
                                                                                gameBoard[i][column] = 10;
                                                                                changeMade = true;
                                                                                moveDone = true;
                                                                                displayBoard(gameBoard);
                                                                        }
                                                                }
                                                                if( i != 2 && column == 2)
                                                                {
                                                                        if(gameBoard[i+1][column] == 10 ||gameBoard[i-1][column] == 10 || gameBoard[i][column-1] == 10)
                                                                        {
                                                                                cout << "executed5";
                                                                                gameBoard[i][column] = 10;
                                                                                changeMade = true;
                                                                                moveDone = true;
                                                                                displayBoard(gameBoard);
                                                                        }
                                                                }
                                                        }
                                     }
                                }
                        }
                        while(changeMade);

        // error checking to make sure move is valid
        if( !moveDone)
        {
                        cout << "Not valid move!" << endl;
                        chooseNumber(gameRow,gameColumn);
                        deleteCell(gameRow,gameColumn,changeMade, moveDone, gameBoard);
        }
         else
        {
                for( int i=0; i< 3; i++)
                {
                        for( int column=0; column< 3; column++)
                        {
                                if(gameBoard[i][column] == 10)
                                {
                                        gameBoard[i][column]=0;
                                }
                        }
                }
        }
//      shift(gameBoard);
        shiftColumnLeft(gameRow,gameColumn,gameBoard);
        displayBoard(gameBoard);

//      winOrLose( changeMade, gameBoard);
        chooseNumber(gameRow,gameColumn);
        deleteCell( gameRow, gameColumn, changeMade,moveDone, gameBoard);




}
int main()

{
        // define row and column size
        int gameBoard[3][3];

        int gameRow = 0;
        int gameColumn = 0;
        bool changeMade, moveDone;
        srand (  time(NULL) );
        // welcomes user to program
        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. \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 < 3; i++)
        {
                for(int column= 0; column < 3; column++)
                {                                                
                         gameBoard[i][column] = rand() % 9 + 1;
                }
                cout << endl;
        }
        gameBoard[0][0] = 4;
        gameBoard[0][1] = 5;
        gameBoard[0][2] = 4;
        gameBoard[1][0] = 4;
        gameBoard[1][1] = 5;
        gameBoard[1][2] = 4;
        gameBoard[2][0] = 4;
        gameBoard[2][1] = 5;
        gameBoard[2][2] = 4;
        displayBoard(gameBoard);
        chooseNumber(gameRow, gameColumn);
        deleteCell(gameRow,gameColumn,changeMade, moveDone, gameBoard);
}

I still can't get numbers on the side? I tried using code and inline code to no avail. Sorry, I will keep looking. Thanks!

Note: The problem is no longer the win or lose statement at the moment, but the removal of matching #s

I still can't get numbers on the side? I tried using code and inline code to no avail. Sorry, I will keep looking. Thanks!

The trick is to use the languate option like this (but remove the spaces)
[ code=cplusplus]
// your code here
[ /code]

ah, I was putting in [code = c++)

Anyone see anything? I will keep pouring over it myself

Bump? I am down to my last 24 hours. :(

It still keeps jumping over and deleting the values.

Most recent code is at bottom of page 1, thanks

The error only occurs when the first and last row have matching values it seems, ie

343
343
343

Alright, I managed to fix the delete statement. Basically, I had not account for cases when column and row were on the outsides of the array. Now I am back to my original problem. I can easily tell if I have won cause a victory would ensue from having a 0 at the bottom left hand integer of the array (the numbers shift left as rows of 0s form)

Does anyone have an idea how to find if there are no legal moves left and thus a loss?

Hehe, last hour until its due. Thanks if you can give any last minute advice. At 6 pm CST, I promise not to bother you guys anymore. :)

Finished guys, thank you for those who helped. My project from hell is over with 50 minutes to spare :)

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.