-4

I'm currently making an ultra simple tictactoe program but I've run into a slight issue. I've created a gameboard using a 2-D array of characters (not a crazy fan of using chars for this, but figured it was the easiest way to enable me to enter X's and O's into the array to output on the screen).

In the PlayerOneMove() function, I prompt the user to enter the cell (1-9) where he would like to place his game piece. The input is stored as a char. I then pass his cell choice the MakeMove() function, which checks if the character the user entered is currently stored in the array. However, the function always returns false.

I realize this is an issue with the compiler seeing the input as an integer and not matching the character that is in the array (because if I replace the elements in the array with letters and then input a letter it finds the match), but I'm not sure how to solve this. I've tried adding the ASCII value to the character and changing the char input to an integer input and then static casting it to a char to no avail. Any help would be appreciated before I decide to just completely annihilate the character array!!

class TicTacToe {
    private: 
    char board[3][3] =
        {
            {'1','2','3'},
            {'4','5','6'},
            {'7','8','9'}
        };
    public:
    void DrawBoard();
    void PlayerOneMove();
    bool MakePlay(char, char);
};

void TicTacToe::PlayerOneMove() {
    char cell;
    cout << "Player One, please select a cell: ";
    cin >> cell;

    while (!MakePlay(cell, 'O')) {
        cout << "That cell is unavailable. Please select another cell: ";
        cin >> cell;
    }
}

bool TicTacToe::MakePlay(char cell, char player) {
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            if (board[row][col] == cell) {
                board[row][col] = player;
                return true;
            }
            else return false;
        }
    }
}

int main() {
    TicTacToe game;
    game.DrawBoard();
    game.PlayerOneMove();

    return 0;
}

example: https://i.stack.imgur.com/sGFjZ.jpg

micronoob
  • 11
  • 2
  • I can't reproduce your problem. [mcve] please, including your provided input and output. – user202729 May 16 '18 at 06:45
  • ([Try it online!](https://tio.run/##ZVNNb9swDL37V3DdwQ7qFe2yra2d5B8U3SG3rgdGYRqhimTIcooi8G/PaNp1YkewAH48kY/PkiqKH29KHY/ftVWmWtNMuzJ4wt0iqkpt38DijsoCFUEZ1nkUKYNlCUutlqiWjuAQAa/C6z0GykA8tUUPK4d@/TJ95Q/mEm7WobfEi@/iNP7JexrX6Sj1i8O/ef@5TN1z@IH3Y1z3mTpvmVQro1Um9t7pNfw1@En@2dKT21MyaVEr5ww84Ts12aThmwprTnOdSA72M2bZqEY3hYypyJi2pnJVgNkMrlo0MDyFwhCWrB0ZUgFQ4Bn8s1fdGW1hseiKSORjqw1B8u1EjnMpxM/xZHKmXt9sucUg50GXUFncoza4MnTDgw9aWxe25DsCXfsLCiJkVEeRCHSmwEAraDmJWciwX9Q2zkOibQDvPmAOt7kYM5iKcX19PkKPVdxKsI0hWDaG2GbpDSTtpeJSry@M4Ys1Fy5jaPuLR9COan6B9BQqbyH4iobJeuCRYTU77AbZOYHrk3DNQDvUNpkcTo8k5OFmfA/r4/HuPw)) – user202729 May 16 '18 at 06:46
  • When I run the program and enter any number 1-9 when prompted for a cell, the program tells me that the cell is unavailable (it's returning false that the input 1-9 was found in the array). I realize the char '9' is not the same as the integer 9 and figured that this was where the problem lay, but am unsure of how to convert the user input (1-9) into '1' - '9' so the function will find a match. – micronoob May 16 '18 at 06:51
  • `std::cin` does not return an `int` – Joseph D. May 16 '18 at 06:52
  • to answer your question, you can use the ascii table to see how to convert chars/ints. https://www.asciitable.com/ For example, char '0' is ascii 48. So a common trick, for a char c, is to do: int x = c - 48. This only works for ascii, for unicode or other char sets it gets more complicated. – Sean F May 16 '18 at 06:57
  • Here's some blog post that might be relevant to your case: [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – grek40 May 17 '18 at 06:22

1 Answers1

0

You have a logical error in MakePlay.

When you enter the nested for-loops it will check if the input matches the first cell, if it doesn't it will return false. The rest of the cells will never get checked.

What you want to do is check all the cells, if it does not match any of them then return false.

You could re-write it like this:

bool TicTacToe::MakePlay(char cell, char player) {
    bool found_cell = false;
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            if (board[row][col] == cell) {
                board[row][col] = player;
                found_cell = true;
            }
        }
    }
    return found_cell;
}
super
  • 12,335
  • 2
  • 19
  • 29
  • Thank you so much, this was the issue! I was so convinced that it was a conversion issue (because I was only testing the first cell) that I didn't even realize that my return statement was in the wrong location. – micronoob May 16 '18 at 07:10