Im new to posting questions here so let me know if ive done something wrong. So this program works fine 90% of the time as seen here.
But then the logic is off sometimes as seen here. Ive looked through my code for a while so ive finally given in and ask for help! My logic() function seems to be accurate. Ive matched all possible routes to win; rows, columns and diagonals. For some reason if the top row is -> 'X' | 'O' | 'X' -> it declares player one (x) to be the winner. Any ideas?
#include <iostream>
#include <iomanip>
using namespace std;
void drawBoard();
char board[3][3] = {'1','2','3','4','5','6','7','8','9'}; // 3x3 board
char player = 'X';
void game();
void toggle();
char logic();
void winner();
int main()
{
drawBoard();
while(1){
game();
drawBoard();
toggle();
if(logic() == 'X')
cout << "Player 1 (X) Wins!" << endl;
else if(logic() == 'O')
cout << "Player 2 (O) Wins!" << endl;
}
return 0;
}
void drawBoard(){
system("cls");
cout << setw(35) << "Tic Tac Toe V 2.0! " << endl << endl;
cout << setw(21) << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
cout << setw(30) << " --------- " << endl;
cout << setw(21) << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
cout << setw(30) << " --------- " << endl;
cout << setw(21) << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
}
void game(){
int input;
cout << "Make your move..." << endl;
cin >> input;
if(input == 1)
board[0][0] = player;
else if(input == 2)
board[0][1] = player;
else if(input == 3)
board[0][2] = player;
else if(input == 4)
board[1][0] = player;
else if(input == 5)
board[1][1] = player;
else if(input == 6)
board[1][2] = player;
else if(input == 7)
board[2][0] = player;
else if(input == 8)
board[2][1] = player;
else if(input == 9)
board[2][2] = player;
}
void toggle(){
if(player == 'X')
player = 'O';
else
player = 'X';
}
char logic(){
// Player 1
/// Across
if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
return 'X';
else if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
return 'X';
else if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
return 'X';
/// Columns
else if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
return 'X';
else if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
return 'X';
else if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
return 'X';
/// Diagonals
else if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
return 'X';
else if(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')
return 'X';
// Player 2
/// Across
if(board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
return 'O';
else if(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
return 'O';
else if(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
return 'O';
/// Columns
else if(board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
return 'O';
else if(board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
return 'O';
else if(board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
return 'O';
/// Diagonals
else if(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
return 'O';
else if(board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
return 'O';
}
In the function logic()
, you are missing a return statement, meaning that if none of the conditions are satisficed, the function will just return anything that could have been in that register. I found that if you add a return 0;
to the end of the function, you don't get that behavior.