Friday, April 17, 2020

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 permutations of this game is quite low, computers can determine best strategy while playing this game. The game play against computer has been well implemented using object oriented programming and the machine uses optimal strategy every time.

In this project we have implemented two ways of playing the game:
1) Playing Against Computer without AI
2) Playing Against Computer with AI

Link for project report: TIC-TAC-TOE Report

Topic 1: Intoduction
Presentor's info:
Name: Rushikesh Tople.
Div:TY-L
Roll no:67
GR.no : 1710028
Video Link:  Introduction

Topic 2:
Presentor's info: TIC-TAC-TOE Without AI Code
Name: Vedika Mahajan
Div:TY-L
Roll no:45
GR.no : 182054
Video Link:  TIC-TAC-TOE Without AI Code

Topic 3: Without AI -Output explaination
Presentor's info:
Name: Shrikant Karhale
Div:TY-L
Roll no:34
GR.no : 1710362
Video Link:  Without AI -Output explaination


Topic 4: TIC-TAC-TOE With AI Code
Presentor's info:
Name: Shraddha Katkar
Div:TY-L
Roll no:35
GR.no : 1710385
Video Link:  TIC-TAC-TOE With AI Code


Topic 5: With AI -Output explaination
Presentor's info:
Name: Gaurav Waghmare
Div:TY-L
Roll no:71
GR.no : 1710291
Video Link:  With AI -Output explaination

also,

Topic of our seminar is: Access Specifiers and Inheritance
Prezi Link: Access Specifiers and Inheritance in c++

THANK YOU!!!

Friday, April 10, 2020

How to Never Lose at Tic Tac Toe


Let’s have some basic definitions here.

Counter - Making a move that blocks your opponent
Centre - The square in the middle surrounded by all the other squares.
Edge - A piece in row or column with the centre.
Corner - A piece in diagonal with the centre.


When playing first

Don’t place your first move on an edge.


1) Centre

If you mark the centre, your opponent will either place the first piece on an edge or corner piece.
·        If they mark an edge, place your next piece on one of the two corners furthest from the edge piece. If they block that move, place your on corner in the same row.  Now you have two ways to win. If they don’t block it, you will win by placing your move on the edge.
·        If they mark a corner, opposite corner, or the corner that would make a diagonal of two X's and one O. If they place their next piece on an edge, you now have two ways of winning, depending on which edge they placed their O on. Otherwise, assuming you keep counter-attacking, the game will be a draw.


2) Corner


·        If their first move is not the centre, your next move should be in the other corner of the same row you placed your first piece. If they block you will have two ways to win.



·        If their first move is in the centre, it's a little bit trickier. Again, form a diagonal. If their next move is in the corner, you can trap them by placing your next piece at the intersection of the row and column of the previous two X's. If their next move is at an edge, you'll be forced to settle for a draw.


Playing second.



Depending on where your opponent plays the first piece, these are some ways to defend.

1) Centre

If he chooses the centre, place your O on the corner immediately, which will buy you some time. According to the best strategy, your opponent will place his next X on the opposite corner to yours. Your next piece should not be bordering your previous move. Then, it's the simple matter of continuously blocking and counter-attacking until a tie is reached.
2) Corner

If they mark a corner, mark the centre, or you will almost certainly lose against a good opponent. Then remember that there is one outcome in which a tie is possible from above.

Your opponent has two choices, to either form a diagonal or place their next piece somewhere else. Assuming that their move forms a diagonal, as the strategy would dictate, stay on the edges and off the corn
ers. You can force a tie this way.





Contributor:-

Gaurav Waghmare


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

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...