0

Hi guys, our goal is to try to create a c++ rendition of the same game. I have coded all the way up to the first real part of the program, trying to remove cells based off of what the user inputs.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
                
void chooseNumber(int& gameRow, int& gameColumn)
{
                         
        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 deleteCell(int gameBoard[15][12], int gameRow, int gameColumn, int&
tempCounter)
{
        bool changeMade = false;
        int tempNum = gameBoard[gameRow][gameColumn];
        gameBoard[gameRow][gameColumn] = 0;
        cout << tempNum;

        do
        {
        
                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;
                                }
                                else
                                {
                                        cout << "Didn't work!";
                                } 
                        }
                }
        }
        while(changeMade);
        chooseNumber(gameRow,gameColumn);
                
         
        
}
        
int main()
{
        int gameBoard[15][12];
        int gameRow = 0;
        int gameColumn = 0;
        int tempCounter =1;
                         
        srand (  time(NULL) );
                                
        cout << "Welcome to the Same Game. The rules are as follows: "
             << endl << "You will be given a 15x12 grid filled with "
             << "random integers spanning from 1 to 9. 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. If "
             << "you run out of moves, then it's game over. \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, tempCounter);
}

For the life of me, I can't understand why my delete cell function won't work. Any help would be appreciated, whether it be pointing me to some other source or whatnot :)

2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by pacman326@gmail
0

What won't work? People mightn't compile your code so you're going to have to give us some more info.

Errors, warnings, crashing?

0

The deleteCell function doesn't work as expected. The else state that says didnt work was only for testing purposes. I don't believe that it is actually changing values that are the same number as the user selected # and next to a 0 into a 0.

Basically, I store what number the user chooses, and then convert the number at that spot to 0. Then I have it check throughout the array to see if there are numbers that are equal to the number the user chose and next to a 0. In that case, that number gets converted to 0, and I keep running the loop until no more can be found.

The program compiles, if you need any more information, I would be happy to provide it.

0

Alright, I believe I have verified that my deleteCell function works. However, I have noticed that how I have coded it allows it to go out of bounds. How would I be able to stop it from checking if column -1 or row -1 for example had a similar value.

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.