Program to implement Tic Tac Toe

//Implementation of Tic Tac Toe program

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int option, test, row, col, turn, option2 = 0;
char board[3][3];

//print the board to the screen
void drawBoard()
{
cout<<“\n”;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout<<“|”<<board[i][j]<<“| “;
}
cout<<“\n ————-\n”;
}
}

//this method returns a 0 if there’s no winner, 1 if X wins, 2 if O wins, 3 if tie
int checkWinner()
{
//check to see if X won
if(board[0][0] == ‘X’ && board[0][1] == ‘X’ && board[0][2] == ‘X’)
return 1;
if(board[1][0] == ‘X’ && board[1][1] == ‘X’ && board[1][2] == ‘X’)
return 1;
if(board[2][0] == ‘X’ && board[2][1] == ‘X’ && board[2][2] == ‘X’)
return 1;

if(board[0][0] == ‘X’ && board[1][0] == ‘X’ && board[2][0] == ‘X’)
return 1;
if(board[0][1] == ‘X’ && board[1][1] == ‘X’ && board[2][1] == ‘X’)
return 1;
if(board[0][2] == ‘X’ && board[1][2] == ‘X’ && board[2][2] == ‘X’)
return 1;

if(board[0][0] == ‘X’ && board[1][1] == ‘X’ && board[2][2] == ‘X’)
return 1;
if(board[2][0] == ‘X’ && board[1][1] == ‘X’ && board[0][2] == ‘X’)
return 1;
//check to see if O won
if(board[0][0] == ‘O’ && board[0][1] == ‘O’ && board[0][2] == ‘O’)
return 2;
if(board[1][0] == ‘O’ && board[1][1] == ‘O’ && board[1][2] == ‘O’)
return 2;
if(board[2][0] == ‘O’ && board[2][1] == ‘O’ && board[2][2] == ‘O’)
return 2;

if(board[0][0] == ‘O’ && board[1][0] == ‘O’ && board[2][0] == ‘O’)
return 2;
if(board[0][1] == ‘O’ && board[1][1] == ‘O’ && board[2][1] == ‘O’)
return 2;
if(board[0][2] == ‘O’ && board[1][2] == ‘O’ && board[2][2] == ‘O’)
return 2;

if(board[0][0] == ‘O’ && board[1][1] == ‘O’ && board[2][2] == ‘O’)
return 2;
if(board[2][0] == ‘O’ && board[1][1] == ‘O’ && board[0][2] == ‘O’)
return 2;}
//AI. generate a random move and see if it’s available. if it is, then make it
//if it isn’t then generate another random move
void AI()
{
col = -1;
row = -1;
while(col == -1 || row == -1)
{
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
col = rand()%3;
row = rand()%3;
if(board[row][col] != ‘ ‘)
{
col = -1;
row = -1;
}
board[row][col] = ‘O’;
}

}
int main()
{

do
{
option2 = 0; //so we can play multiple times
cout<<“\n Enter a menu option:\n”;
cout<<“—————————–\n\n”;
cout<<” 1. Play Tic Tac Toe\n”;
cout<<” 2. Quit\n”;
cin>>option;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = ‘ ‘;
}
}
if(option !=2)
{
while(option2 != -1)
{
if(turn%2 == 0)//if it’s even then it’s X’s turn
{
int condition = 1;
while(condition == 1)
{
cout<<“\n\n Enter a move:\n”;
cout<<” Enter the row you wish to make your move on (0,1,2)\n”;
cin>>row;
cout<<” Enter the column you wish to make your move on (0,1,2)\n”;
cin>>col;
if(board[row][col] == ‘ ‘)
{
board[row][col] = ‘X’;
turn++;
condition = 0;
}
else
cout<<” Invalid move!\n”;
}
}
else //AI must do something
{
AI();
turn++;
}

if(checkWinner() == 1) //X wins
{
cout<<” Congratulations! X won!\n\n”;
option2 = -1;
}
else if(checkWinner() == 2) //o wins
{
cout<<” Try Again! Computer won!\n\n”;
option2 = -1;
}
drawBoard();
}

}

}while(option != 2);
cout<<“\n\n\n\n Enter anything to quit.”;
cin>>test;
}

Mohit Arora
Follow me