I'm a beginner in C++ and I am trying to make a tic tac toe game. I am almost complete, but the win checker says one has won with two consecutive symbols instead of three. Tips to make the code smaller are also appreciated. Keep in mind I am a beginner who has learned only a few months.
the checker:
//wincheck(messy(put it in a true if statement to make it smaller))
if (true) {
if ((_board[0][0] == 'X') && (_board[1][1] == 'X') && (_board[2][2] =
'X')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else if ((_board[2][0] == 'X') && (_board[1][1] == 'X') && (_board[0]
[2] = 'X')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else
//left to right
if ((_board[0][0] == 'X') && (_board[0][1] == 'X') && (_board[0]
[2] = 'X')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else if ((_board[1][0] == 'X') && (_board[1][1] == 'X') &&
(_board[1][2] = 'X')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else if ((_board[2][0] == 'X') && (_board[2][1] == 'X') &&
(_board[2][2] = 'X')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else
// up to down
if ((_board[0][0] == 'X') && (_board[1][0] == 'X') &&
(_board[2][0] = 'X')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else if ((_board[0][1] == 'X') && (_board[1][1] == 'X') &&
(_board[2][1] = 'X')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else if ((_board[0][2] == 'X') && (_board[1][2] == 'X') &&
(_board[2][2] = 'X')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
// check if O won
//diagonals
if ((_board[0][0] == 'O') && (_board[1][1] == 'O') &&
(_board[2][2] = 'O')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else if ((_board[2][0] == 'O') && (_board[1][1] == 'O') &&
(_board[0][2] = 'O')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else
//left to right
if ((_board[0][0] == 'O') && (_board[0][1] == 'O') &&
(_board[0][2] = 'O')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else if ((_board[1][0] == 'O') && (_board[1][1] == 'O')
&& (_board[1][2] = 'O')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else if ((_board[2][0] == 'O') && (_board[2][1] == 'O')
&& (_board[2][2] = 'O')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else
// up to down
if ((_board[0][0] == 'O') && (_board[1][0] == 'O') &&
(_board[2][0] = 'O')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else if ((_board[0][1] == 'O') && (_board[1][1] ==
'O') && (_board[2][1] = 'O')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
else if ((_board[0][2] == 'O') && (_board[1][2] ==
'O') && (_board[2][2] = 'O')) {
printBoard();
cout << name1 << " has won!" << endl;
return true;
}
}
}