Friday, April 10, 2020

Code of Tic Tac Toe Playing Against Computer

#include<iostream>

using namespace std;
int grid[3][3];
int y1 , y2;

void initialize(char b[3][3])
{
cout<<"PLAYER - 1 [X]\t PLAYER - 2 [O]\n\n";
cout<<"\t\t     |     |     \n";
cout<<"\t\t "<<b[0][0]<<"   | "<<b[0][1]<<"   | "<<b[0][2]<<"    \n";
cout<<"\t\t_____|_____|_____\n";
cout<<"\t\t     |     |     \n";
cout<<"\t\t  "<<b[1][0]<<"  |  "<<b[1][1]<<"  | "<<b[1][2]<<"  \n";
cout<<"\t\t_____|_____|_____\n";
cout<<"\t\t     |     |     \n";
cout<<"\t\t  "<<b[2][0]<<"  |  "<<b[2][1]<<"  | "<<b[2][2]<<"  \n";
cout<<"\t\t     |     |    \n";
}

void display()
{
    char b[3][3];
    for(int i=0;i<3;i++)    {
        for(int j=0;j<3;j++)        {
        if(grid[i][j]== 0)      {
            b[i][j] = ' ';
        }
            else if(grid[i][j]==10) {
                b[i][j] ='X';
            }
            else if(grid[i][j]== -10)   {
                b[i][j] ='O';
            }
        }
    }   
    initialize(b);
}

int winningposiblity()
{
    int w1=0,w2=0;
    
    for(int i=0;i<3;i++)    {   
    if  (grid[i][0]+grid[i][1]+grid[i][2]==20
     || grid[i][0]+grid[i][1]+grid[i][2]==10)
     
                w1++;
                
                
    else if  (grid[i][0]+grid[i][1]+grid[i][2]== -20 
    || grid[i][0]+grid[i][1]+grid[i][2]== -10)
    
                w2++;
        
            
    if  (grid[0][i]+grid[1][i+1]+grid[i][i+2]==20
     || grid[0][i]+grid[1][i+1]+grid[i][i+2]==10)
     
                w1++;
                
    else if  (grid[0][i]+grid[1][i+1]+grid[i][i+2]== -20
     || grid[0][i]+grid[1][i+1]+grid[i][i+2]== -10)

                w2++;   
    }

    if  (grid[0][0]+grid[1][1]+grid[2][2]==20
     || grid[0][0]+grid[1][1]+grid[2][2]==10)
     
        w1++;
        
    else if  (grid[0][0]+grid[1][1]+grid[2][2]== -20
     || grid[0][0]+grid[1][1]+grid[2][2]== -10)
     
        w2++;

    if  (grid[0][2]+grid[1][1]+grid[2][0]==20
     || grid[0][2]+grid[1][1]+grid[2][0]==10)
     
        w1++;
        
    else if  (grid[0][2]+grid[1][1]+grid[2][0]== -20
     || grid[0][2]+grid[1][1]+grid[2][0]== -10)
     
        w2++;

    return w2-w1;   
}


int anyof(int player,int i1,int i2,
        int j1,int j2,int k1,int k2)
{
    
    if(grid[i1][i2]==0 && 
    grid[j1][j2]==grid[k1][k2] && grid[j1][j2]==player) 
    {
        grid[i1][i2]= -10;
        cout<<"Computer's move : "<<i1*3+i2+1<<endl;
        return 1;
    }
    
    if(grid[j1][j2]==0 &&
     grid[i1][i2]==grid[k1][k2] && grid[i1][i2]==player)
    {
        grid[j1][j2]= -10;
        cout<<"Computer's move : "<<j1*3+j2+1<<endl;
        return 1;
    }
    
    if(grid[k1][k2]==0 && 
    grid[i1][i2]==grid[j1][j2] && grid[j1][j2]==player) 
    {
        grid[k1][k2]= -10;
        cout<<"Computer's move : "<<k1*3+k2+1<<endl;
        return 1;
    }
    
    return 0;
}

int isplayerwinning(int player)
{
    
    if(anyof(player,0,0,1,1,2,2) ||
     anyof(player,0,2,1,1,2,0))
     
        return 1;

    for(int i=0;i<3;i++)    {
        
        if(anyof(player,i,0,i,1,i,2))
            return 1;

        if(anyof(player,0,i,1,i,2,i))
            return 1;
    }

    return 0;
}

void player2()
{
    
    if(isplayerwinning(-10) ||
     isplayerwinning(10))
     
        return;
        
    int max=0,a=0,b=0;
    for(int i=0;i<3;i++)    {
        for(int j=0;j<3;j++)    {
            if(grid[i][j]==0)   {
                grid[i][j]= -10;
                
            
                int heuristic=winningposiblity();
                if(heuristic>max)
                {
                    max= heuristic;
                    a=i;
                    b=j;
                }
                grid[i][j]=0;
            }
        }
    }
    grid[a][b]= -10;
    cout<<"Computer's move : "<<a*3+b+1<<endl;
}

int iswin()
{
    
    if(grid[0][0]==grid[1][1] && 
    grid[1][1]==grid[2][2] && grid[0][0]!=0)
    
        return grid[0][0];
    
    if(grid[0][2]==grid[1][1] && 
    grid[1][1]==grid[2][0] && grid[0][2]!=0)
    
        return grid[0][2];
    

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

    return 0;
}

void playGame()
{
    cout<<"********************** TIC TAC TOE "<<
    "**********************"<<endl;
    for(int i=0;i<3;i++)    {
        for(int j=0;j<3;j++)
            (grid[i][j] = 0);
    }
    
    int turn=1,flag=0;
    int total=9;
    while(total--)  {
        int w=iswin();
        if(w!=0)    {
            if(w==10)   {
                y1++;
                cout<<"*****Congratulations!!! "<<
                "You won the game*****"<<endl<<endl;
            }
            
            else    {
                y2++;
                cout<<"********Sorry!!! "<<
                "Computer won the game********"<<endl<<endl;
            }
            flag=1;
            break;
        }

        if(turn==1) {
            int a,b,c;
            cout<<"Player's move : ";
            cin>>c;
            a=(c-1)/3;
            b=(c-1)%3;
            grid[a][b]=10;
            turn =2;
        }
        
        else    {
            player2(); //computer
            turn=1;
        }
        
        cout<<"DISPLAY:"<<endl;
        display();
        cout<<endl;
    }

    if(flag==0) {
        cout<<"-----------DRAW----------"<<endl<<endl;
    }
    
}

int main()
{
    
bool z = true;
int choice;
while(z)    {
 cout<<"1: New Game \t2: View Score \t3: End Game\n";
 cin >> choice;
 switch(choice) {
    case 1:
        playGame();
        break;
    
    case 2:
     cout<<"You - "<< y1 <<" Computer - "<< y2 << "\n";     
      break;
    
    case 3:
        z = false;
        break;
            
    default:
        cout<<"Try Again!!";
        break;
  }
}
return 0;
}


Contributor:-

Gaurav Waghmare

13 comments:

Documentation

Course Project Title: TIC-TAC-TOE GAME in C++ Description: Tic-Tac-Toe is a zero-sum and perfect information game. As the total perm...