tic tac toe program review

Hi,

I recently wrote this tic tac toe program, and it does everything I want it to do, but I was wondering if there were any more efficient tactics I could've used, particularly with error checking the user input and recognizing the winner?

#include <iostream>

using namespace std;

void displayTable(char table[4][4]);

int main()
{
char table[4][4] = {' ', '1', '2', '3',
'1', '*', '*' , '*',
'2', '*', '*' , '*',
'3', '*', '*' , '*'};

displayTable(table);


int row_choice, column_choice;
int turnCount = 0;
cout << "One person is X's and one person is O's." << endl;
cout << "X goes first." << endl;


do
{
cout << "Enter the numbers for the row and column where you want to play." << endl;
cin >> row_choice>>column_choice;

while (cin.fail())
{
cout << "This was an invalid input." << endl;
cin.clear();
cin.ignore(80, '\n');
cout << "Please re-enter the numbers." << endl;
cin >> row_choice>>column_choice;
}


while (row_choice > 3||row_choice < 1||column_choice > 3 || column_choice < 1)
{
cout << "That is not a valid placement." << endl;
displayTable(table);
cout << "Enter the numbers for the row and column where you want to play." << endl;
cin >> row_choice >> column_choice;
}
while (table[row_choice][column_choice] != '*')
{
cout << "That spot is already taken." << endl;
displayTable(table);
cout << "Enter the numbers for the row and column where you want to play." << endl;
cin >> row_choice >> column_choice;
}

if ((turnCount%2) == 0)
table[row_choice][column_choice] = 'X';
else
table[row_choice][column_choice] = 'O';

displayTable(table);

if (table[1][1] == table[1][2] && table[1][2] == table[1][3] && table[1][1] != '*'||table[2][1] == table[2][2] && table[2][2] == table[2][3] && table[2][1] != '*'||
table[3][1] == table[3][2] && table[3][2] == table[3][3] && table[3][1] != '*'||table[1][1] == table[2][1] && table[2][1] == table[3][1] && table[1][1] != '*'||
table[1][2] == table[2][2] && table[2][2] == table[3][2] && table[1][2] != '*'||table[1][3] == table[2][3] && table[2][3] == table[3][3] && table[1][3] != '*'||
table[1][1] == table[2][2] && table[2][2] == table[3][3] && table[1][1] != '*'||table[1][3] == table[2][2] && table[2][2] == table[3][1] && table[1][3] != '*')
{
if (table[row_choice][column_choice] == 'X')
{
cout << "X wins!"<< endl;
return 0;
}
else
{
cout << "O wins!" << endl;
return 0;
}
}

turnCount++;
}while (turnCount < 9);

cout << "CAT! Nobody wins!" << endl;


return 0;
}

void displayTable(char table[4][4])
{
for (int row=0; row < 4; row++)
{
for (int column=0; column<4; column++)
cout <<table[row][column] << " | ";
cout << endl;
cout << " -------------" << endl;
}
}
Topic archived. No new replies allowed.