Christian
Christian

Reputation: 35

Tic Tac Toe cin input issue

question on tic tac toe. I'm trying to allow the user to input "0,0"(row/column) format exactly, and would be invalid otherwise. Error is that if I input any of the correct grid numbers, it would give an invalid error regardless.

"i.e - '1,2'

invalid error

invalid error

invalid error"

So nothing the user input is inserted in any of the grid if they input the numbers correctly.

So here is my code, any advice/help is appreciated on moving forward. Also note i'm just trying to verify and input the X's and O's, not really checking user if they tie/win

#include <iostream>
#include "Tictactoe.h"

using namespace std;

int main(){
char board[3][3]={{'.','.','.'},{'.','.','.'},{'.','.','.'}};
bool gameover(true);
int iPlayerTurn(1);

do {
    // Print board

cout << " -   0   1   2" << endl;
cout << "   +---+---+---+" << endl;

cout << " 0" << " | " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << endl;
cout << "   +---+---+---+" << endl;

cout << " 1" << " | " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << endl;
cout << "   +---+---+---+" << endl;

cout << " 2" << " | " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << endl;
cout << "   +---+---+---+" << endl;



char cPlayerMark;
    if (iPlayerTurn == 1) {
        cPlayerMark = 'X';
    } else {
        cPlayerMark = 'O';
    }
// check if move is valid

    std::cout << "Player" << iPlayerTurn << "'s move:" << std::endl;
    bool bValidMove;
    // Loop until we get a valid move
    do {
        char cNextMove;
        cin >> cNextMove;
        bValidMove = true;

        // Check for a valid move
        if (cNextMove == '0,0' && board[0][0] == '.') {
            board[0][0] = cPlayerMark;
        } else if (cNextMove == '0,1' && board[0][1] == '.') {
            board[0][1] = cPlayerMark;
        } else if (cNextMove == '0,2' && board[0][2] == '.') {
            board[0][2] = cPlayerMark;
        } else if (cNextMove == '1,0' && board[1][0] == '.') {
            board[1][0] = cPlayerMark;
        } else if (cNextMove == '1,1' && board[1][1] == '.') {
            board[1][1] = cPlayerMark;
        } else if (cNextMove == '1,2' && board[1][2] == '.') {
            board[1][2] = cPlayerMark;
        } else if (cNextMove == '2,0' && board[2][0] == '.') {
            board[2][0] = cPlayerMark;
        } else if (cNextMove == '2,1' && board[2][1] == '.') {
            board[2][1] = cPlayerMark;
        } else if (cNextMove == '2,2' && board[2][2] == '.') {
            board[2][2] = cPlayerMark;
        } else {
            cout << "Invalid Move. Try again." <<endl;
            bValidMove = false;
        }
    } while (!bValidMove);

}while (!gameover);
}

Upvotes: 0

Views: 226

Answers (1)

thor
thor

Reputation: 22560

The problem is that you have defined cNextMove as a char instead of string. So it can only contain one character no matter how many characters the user entered. And the comparison will always be false.

If you compile your code with gcc4.8, you will actually get an warning like:

test.cpp:45:26: warning: multi-character character constant [-Wmultichar]
         if (cNextMove == '0,0' && board[0][0] == '.') {

Upvotes: 1

Related Questions