0% found this document useful (0 votes)
120 views

Tic Tac Toe

The document describes a Tic-Tac-Toe game program written in C++. The program uses a single array to represent the game board and defines functions to check if the board is full, if a player or CPU has won, and to draw the board. The main function uses a minimax algorithm to have the CPU select its moves and plays the game between a user and CPU, declaring a draw if the board is full or a win for the player or CPU that completes a line.

Uploaded by

VIVEK WANI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

Tic Tac Toe

The document describes a Tic-Tac-Toe game program written in C++. The program uses a single array to represent the game board and defines functions to check if the board is full, if a player or CPU has won, and to draw the board. The main function uses a minimax algorithm to have the CPU select its moves and plays the game between a user and CPU, declaring a draw if the board is full or a win for the player or CPU that completes a line.

Uploaded by

VIVEK WANI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Tic-Tac-Toe

--------------------------------------------------------------------------------------------------------------------------------------

Name: Vivek Wani

Gr.No:182142

Roll No:80

Batch:B4

Program
#include<iostream>
using namespace std;
int index1;
char board[9] = {'*','*','*','*','*','*','*','*','*'};// Single array
represents the board '*' means empty box in board

int isFull()// Board is full


{
for(int i =0;i<9;i++)
{
if(board[i]!='X')
{
if(board[i]!='O')
{
return 0;
}
}
}
return 1;
}

int user_won()//Checks whether user has won


{
for(int i=0;i<9;i+=3)
{

if((board[i]==board[i+1])&&(board[i+1]==board[i+2])&&(board[i]=='O'))
return 1;
}
for(int i=0;i<3;i++)
{

if((board[i]==board[i+3])&&(board[i+3]==board[i+6])&&(board[i]=='O'))
return 1;
}
if((board[0]==board[4])&&(board[4]==board[8])&&(board[0]=='O'))
{
return 1;
}
if((board[2]==board[4])&&(board[4]==board[6])&&(board[2]=='O'))
{
return 1;
}
return 0;
}
int cpu_won()// Checks whether CPU has won
{
for(int i=0;i<9;i+=3)
{

if((board[i]==board[i+1])&&(board[i+1]==board[i+2])&&(board[i]=='X'))
return 1;
}
for(int i=0;i<3;i++)
{

if((board[i]==board[i+3])&&(board[i+3]==board[i+6])&&(board[i]=='X'))
return 1;
}
if((board[0]==board[4])&&(board[4]==board[8])&&(board[0]=='X'))
{
return 1;
}
if((board[2]==board[4])&&(board[4]==board[6])&&(board[2]=='X'))
{
return 1;
}
return 0;
}

void draw_board() //display tic-tac-toe board


{
cout<<endl;
cout<<board[0]<<"|"<<board[1]<<"|"<<board[2]<<endl;
cout<<"-----"<<endl;
cout<<board[3]<<"|"<<board[4]<<"|"<<board[5]<<endl;
cout<<"-----"<<endl;
cout<<board[6]<<"|"<<board[7]<<"|"<<board[8]<<endl;
}

int minimax(bool flag)// The minimax function


{

int max_val=-1000,min_val=1000;
int i,j,value = 1;
if(cpu_won() == 1)
{return 10;}
else if(user_won() == 1)
{return -10;}
else if(isFull()== 1)
{return 0;}
int score[9] = {1,1,1,1,1,1,1,1,1};//if score[i]=1 then it is empty

for(i=0;i<9;i++)
{
if(board[i] == '*')
{
if(min_val>max_val) // reverse of pruning
condition.....
{
if(flag == true)
{
board[i] = 'X';
value = minimax(false);
}
else
{
board[i] = 'O';
value = minimax(true);
}
board[i] = '*';
score[i] = value;
}
}
}

if(flag == true)
{
max_val = -1000;
for(j=0;j<9;j++)
{
if(score[j] > max_val && score[j] != 1)
{
max_val = score[j];
index1 = j;
}
}
return max_val;
}
if(flag == false)
{
min_val = 1000;
for(j=0;j<9;j++)
{
if(score[j] < min_val && score[j] != 1)
{
min_val = score[j];
index1 = j;
}
}
return min_val;
}
}

int main() //The main function


{
int move,choice;
cout<<"-------------------------TIC TAC
TOE-----------------------------------------------------";
cout<<endl<<"USER--->(O) CPU------>(X)";
cout<<endl<<"SELECT : 1-> Player first 2-> CPU first:";
cin>>choice;
if(choice == 1)
{
draw_board();
up:cout<<endl<<"Enter the move:";
cin>>move;
if(board[move-1]=='*')
{
board[move-1] = 'O';
draw_board();
}
else
{
cout<<endl<<"Invalid Move......Try different move";
goto up;
}
}

while(true)
{

cout<<endl<<"CPU MOVE....";
minimax(true);
board[index1] = 'X';
draw_board();
if(cpu_won()==1)
{
cout<<endl<<"CPU WON.....";
break;
}
if(isFull()==1)
{
cout<<endl<<"Draw....";
break;
}
again: cout<<endl<<"Enter the move:";
cin>>move;
if(board[move-1]=='*')
{
board[move-1] = 'O';
draw_board();
}
else
{
cout<<endl<<"Invalid Move......Try different move";
goto again;
}
if(user_won()==1)
{
cout<<endl<<"You Won......";
break;
}
if(isFull() == 1)
{
cout<<endl<<"Draw....";
break;
}
}

--------------------------------------------------------------------------------------------------------------------------------------

Output

-------------------------TIC TAC TOE-----------------------------------------------------

USER--->(O) CPU------>(X)

SELECT : 1-> Player first 2-> CPU first:1

*|*|*

-----

*|*|*
-----

*|*|*

Enter the move:2

*|O|*

-----

*|*|*

-----

*|*|*

CPU MOVE....

X|O|*

-----

*|*|*

-----

*|*|*

Enter the move:5

X|O|*

-----

*|O|*

-----

*|*|*

CPU MOVE....

X|O|*

-----

*|O|*

-----

*|X|*

Enter the move:7

X|O|*

-----

*|O|*

-----
O|X|*

CPU MOVE....

X|O|X

-----

*|O|*

-----

O|X|*

Enter the move:4

X|O|X

-----

O|O|*

-----

O|X|*

CPU MOVE....

X|O|X

-----

O|O|X

-----

O|X|*

Enter the move:9

X|O|X

-----

O|O|X

-----

O|X|O

Draw....

Process returned 0 (0x0) execution time : 119.820 s

Press any key to continue.

You might also like