1

I am writing a Tic-Tac-Toe game using C++. I created a 2D array for the board. The issue I am experiencing is when the user is picking a position to place their piece(X or O). When the user selects any letter in either the first row or first column, the program would just end. If, for example, the user picks the middle cell, it would correctly place the user's piece. Any help would be appreciated. Thanks!

#include <iostream>
using namespace std;
char board[3][3] = { { 'A', 'B', 'C' }, { 'D', 'E', 'F' }, { 'G', 'H', 'I' } };
char player1 = 'X', player2 = 'O';



//displays board
void displayBoard() {
cout << "_________________" << endl;
cout << "|    |     |    |" << endl;
cout << "| " << board[0][0] << "  |  " << board[0][1] << "  |  "
        << board[0][2] << " |" << endl;
cout << "|____|_____|____|" << endl;
cout << "|    |     |    |" << endl;
cout << "| " << board[1][0] << "  |  " << board[1][1] << "  |  "
        << board[1][2] << " |" << endl;
cout << "|____|_____|____|" << endl;
cout << "|    |     |    |" << endl;
cout << "| " << board[2][0] << "  |  " << board[2][1] << "  |  "
        << board[2][2] << " |" << endl;
cout << "|    |     |    |" << endl;
cout << "|_______________|" << endl;
}

//check if game is over
bool checkStatus(char piece) {
bool over = false;
if ((board[0][0] == piece) && (board[0][1] = piece)
        && (board[0][2] = piece)) {
    over = true;
} else if ((board[1][0] == piece) && (board[1][1] = piece) && (board[1][2] =
        piece)) {
    over = true;
} else if ((board[2][0] == piece) && (board[2][1] = piece) && (board[2][2] =
        piece)) {
    over = true;
} else if ((board[0][0] == piece) && (board[1][0] = piece) && (board[2][0] =
        piece)) {
    over = true;
} else if ((board[0][1] == piece) && (board[1][1] = piece) && (board[2][1] =
        piece)) {
    over = true;
} else if ((board[0][2] == piece) && (board[1][2] = piece) && (board[2][2] =
        piece)) {
    over = true;
} else if ((board[0][0] == piece) && (board[1][1] = piece) && (board[2][2] =
        piece)) {
    over = true;
} else if ((board[0][2] == piece) && (board[1][1] = piece) && (board[2][0] =
        piece)) {
    over = true;
} else {
    over = false;
}
return over;
}

//gameplay
void game(char player1, char player2, bool current) {
bool turn = current; //true:player 1; false:player2
int row = 0, column = 0;
char position;
while (checkStatus(player1) != true && checkStatus(player2) != true) {
//player 1
    if (turn != false) {
        std::cout << "Player 1 Turn. Choose a Letter" << endl;
    }
//player2
    else if (turn != true) {
        std::cout << "Player 2 Turn. Choose a Letter" << endl;
    }
    displayBoard();
    std::cin >> position;
    position = toupper(position);
    switch (position) {
    case 'A':
        row = (position - 'A') / 3;
        column = (position - 'A') % 3;
        break;
    case 'B':
        row = 0;
        column = 1;
        break;
    case 'C':
        row = 0;
        column = 2;
        break;
    case 'D':
        row = 1;
        column = 0;
        break;
    case 'E':
        row = 1;
        column = 1;
        break;
    case 'F':
        row = 1;
        column = 2;
        break;
    case 'G':
        row = 2;
        column = 0;
        break;
    case 'H':
        row = 2;
        column = 1;
        break;
    case 'I':
        row = 2;
        column = 2;
        break;
    default:
        cout << "You didn't enter a correct letter! Try again\n";
        game(player1, player2, turn);
    }
    cin.clear();

//player X
    if (turn != false && board[row][column] != 'X'
            && board[row][column] != 'O') {
        board[row][column] = 'X';
        turn = false;
        checkStatus(player1);
        checkStatus(player2);
    }

//player O
    else if (turn != true && board[row][column] != 'X'
            && board[row][column] != 'O') {
        board[row][column] = 'O';
        turn = true;
        checkStatus(player1);
        checkStatus(player2);
    } else {
        cout << "Choose a valid cell!";
        game(player1, player2, turn);
    }
}
}

int main() {
int choice;
std::cout << "Welcome to Tic-Tac-Toe\n"
        "1 player game, enter 1\n"
        "2 player game, enter 2" << endl;
while (!(cin >> choice) || choice < 1 || choice > 2) {
    cout
            << "Bad input. Try again!\nfor a 1 player game, enter 1\nfor a 2 plater game, enter 2"
            << endl;
    cin.clear();
    cin.ignore(INT_MAX, '\n');
}

//two player game
if (choice != 1) {
    cout << "Tic-Tac-Toe: 2 Players" << endl;
    game(player1, player2, true);
} else {
}
return 0;
}
7
  • First, why are you calling game recursively? If I keep hitting the wrong key for input, your call stack piles up and up and up with calls. Commented Jan 19, 2015 at 4:31
  • I understand this is an issue, but right now I'm just assuming the user will enter a valid entry each time. I just would like it to print out the "X" or "O" into row/column 0. All it does is end the program when the user picks, lets say, board[0][0](corner cell). Commented Jan 19, 2015 at 4:36
  • Have you used the debugger? If so, what is the path that the code takes when it gets out of the switch statement? Commented Jan 19, 2015 at 4:37
  • Funny thing about that, I am using eclipse to run my C++ code ( i am new to this language) and for some reason, the debugger will not launch. That is something I am actively looking to fix right now too. Just gives me an error. Commented Jan 19, 2015 at 4:39
  • Someone posted this: row = (position - 'A') / 3; column = (position - 'A') % 3; but I'm not sure i understand what this is intended to do. Commented Jan 19, 2015 at 4:43

1 Answer 1

1

You fell pray to probably one of the oldest and most common programming error in c: if (a==b) vs if (a=b)

All the checks in checkStatus look like this:

if ((board[0][0] == piece) && (board[0][1] = piece) && (board[0][2] = piece))

when they should look like this:

if ((board[0][0] == piece) && (board[0][1] == piece) && (board[0][2] == piece))

Also, as far as game logic is concerned, you can condense the code after the switch statement to

if (board[row][column] != 'X' && board[row][column] != 'O') {
    if (turn)  board[row][column] = 'X';
    else       board[row][column] = 'O';       
    turn = !turn;
} else {
    cout << "Choose a valid cell!";       
}

this should produce the exact same behaviou.r

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.