Search code examples
c++vectortic-tac-toe

Creating a tic tac toe game. I want to create a 3x3 vector that stores the values for rows and columns then print out the board in the function


void printBoard(vector<vector<char>>& board) {

vector<vector<char> > draw(3, vector<char>(3, '-')); //Attempt to create 3x3 Vector with '-'
for (int i = 0; i < draw.size(); i++) {
    cout << draw.at(i) << " "; //Prints out vector
}

Output should look like this

_  _  _ 
_  _  _
_  _  _ 

Solution

  • //Play Tic Tac Toe game between user and computer
    
    #include<iostream>
    #include<cstdio>
    #include<stdlib.h>
    #include<time.h>
    
    #define BLANK 5
    
    using namespace std;
    
    /***************** Display the Matrix **********************************************/
    //Display the matrix
    void display(int matrix[3][3])
    {
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++)
                cout<<matrix[i][j]<<"\t";
            cout<<endl;
        }
    }
    
    /************** Chance of WIN Function *****************************************************/
    
    //Funtion to detect the chance of either for user or systemĵ
    int chance_of_win(int matrix[3][3],int i, int j,int choice){
    
    int result=0;//This variale is used to return the required position
    int other_choice;//This variable is used for other choice of the variable choice
    
    if(choice==0)
        other_choice=1;
    else
        other_choice=0;
    
    int count1=0;//This variable is used to check the count upto 2
    
        //Diagonal Intelligent
        if(i==j){
            for(int k=0;k<3;k++){
                if(matrix[k][k]==choice)
                    count1++;
    
                if(count1==2){   // That means user is going to win and system has to stop that
                    for(int k=0;k<3;k++){
                        if(matrix[k][k]!=choice && matrix[k][k]!=other_choice){
                            int temp=k;
                            temp=temp*10;
                            result=temp+k;
                            return result;
                        }
                    }
                }
            }//for looop ends here
        }//If Structure ends here
    
    count1=0; //Reinitilize the count to zero
    
        //Reverse Diagonal intelligent
        for(int m=0,n=2;m<3,n>=0;m++,n--){
            if(matrix[m][n]==choice){
                count1++;
            }
    
            if(count1==2){   // That means user/system is going to win reverse diagnally
            for(int m=0,n=2;m<3,n>=0;m++,n--){
                    if(matrix[m][n]!=choice && matrix[m][n]!=other_choice){
                        int temp=m;
                        temp=temp*10;
                        result=temp+n;
                        return result;
                    }
                }
            }//End of If structure
        }//End for loop
    
    count1=0; //Reinitilize the count to zero
    
        //Row Intelligent
        for(int k=0;k<3;k++){
            if(matrix[i][k]==choice)
                count1++;
    
            if(count1==2){   // That means user/system is going to win
                for(int k=0;k<3;k++){
                    if(matrix[i][k]!=choice && matrix[i][k]!=other_choice){
                        int temp=i;
                        temp=temp*10;//for the ith coordiante
                        result=temp+k;//for the jth cordinate
                        return result;//Return the required attribute of i and j
                    }
                }
            }
        }//for looop ends here
    
    count1=0; //Reinitilize the count to zero
    
        //Column Intelligent
        for(int k=0;k<3;k++){
            if(matrix[k][j]==choice)
                count1++;
    
            if(count1==2){   // That means user is going to win and system has to stop that
                for(int k=0;k<3;k++){
                    if(matrix[k][j]!=choice && matrix[k][j]!=other_choice){
                        int temp=k;
                        temp=temp*10;//for the ith coordinate
                        result=temp+j;//for the jth coordinate
                        return result;//Return the required attribute of i and j
                    }
                }
            }
        }//for looop ends here
    
    
    return result;
    }//function ends here
    
    /******************* Check Win Bool Function ******************************************************/
    
    //This function is used to check the win of the system/user
    bool checkwin(int matrix[3][3],int i, int j,int choice){
    bool flag=false;//Initialize the chance of win false
    int count1=0;
        //Diagonal checkwin forward
        if(i==j){
            for(int k=0;k<3;k++){
                if(matrix[k][k]==choice){
                    count1++;
                }
                if(matrix[k][k]==BLANK)
                    break;
            }
    
            if(count1==3)//Means all diagonal elements are equal
                flag=true;
        }
    
        //If the Diaganoal Forward is same then return
        if(flag){
            cout<<"Diagonal Win\n";
            return flag;
        }
    
        //Reverse Diagonal checkwin
        for(int m=0,n=2;m<3,n>=0;m++,n--){
            if(matrix[m][n]!=choice || matrix[m][n]==BLANK){
                flag=false;//If diagonal is not same
                break;
            }
             flag=true;
        }
    
        //If the Reverse Diaganoal Forward is same then return
        if(flag){
            cout<<"Reverse Diagonal Win\n";
            return flag;
        }
    
        //Row checkwin
        for(int k=0;k<3;k++){
            if(matrix[i][k]!=choice || matrix[i][k]==BLANK){
                flag=false;// Row is not same
                break;
             }
             flag=true;
        }
        //If row is same then return
        if(flag){
            cout<<"Row Win\n";
            return flag;
        }
    
        //Column checkwin
        for(int k=0;k<3;k++){
            if(matrix[k][j]!=choice || matrix[k][j]==BLANK){
                flag=false;//Column is not same
                break;
             }
             flag=true;
        }
        //If the Column is same then return
        if(flag){
            cout<<"Column Win\n";
            return flag;
        }
    
    
        return flag;//return the result false result i.e there is no chance of win
                    //as we have checked all the conditions
    }
    
    /*************************  Main Function **************************************************/
    
    int main(){
    
    int matrix[3][3];
    
    bool flag;
    int toss;
    srand(time(NULL));
    toss=rand()%2;
    
    if(toss){
        flag=true;
        cout<<"User Wins the Toss\n";
    }
    else{
        flag=false;
        cout<<"System Wins the Toss\n";
    }
    //Initialise all the elements of matrix to BLANK i.e. Blank
    
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            matrix[i][j]=BLANK;
    
    cout<<"For user the choice is 0\n";
    cout<<"For system the choice is 1\n";
    
    int v=1;//Initialise the the variable v , it has the increment till 9 to cover all the elements of the matrix
    
    bool system1=false;//To check the chance of win of system
    
    int user_status=0;//To check the chance of win of user and accordingly system will put his move
    int system_status;////To check the chance of win of system and accordingly system will put his move
    
    while(v<=9){
    
    int i,j;// "i" is for the row coordinate and "j" is for the column coordinate
    
        if(flag==true){// If user win's the toss
            cout<<"Yours turn\n";
            cout<<"Enter the row coordinate";
            cin>>i;
            i--;//For user convenience i th coordinate
            cout<<"Enter the column coordinate";
            cin>>j;
            j--;//For user convenience jth coordinate
            if(matrix[i][j]==BLANK)
                matrix[i][j]=0;//Put the user move
            else{
                cout<<"Already Occupied\n"; //Warn user to fill the blank space
                continue;//Don't count this in "variable v" means don't increment the variable "v"
                        //as it was invalid move
            }
    
            // After three attempts it will check , this code is for system
            if(v>2)
                user_status=chance_of_win(matrix,i,j,0);//User chance of win
    
            //checkwin whether game is over i.e whether user win
            if(v>4){
                if(checkwin(matrix,i,j,0)){
                    cout<<"\n\tBingo !! User win\n\tCongrats Well played\n";
                    display(matrix);
                    return 0;
                }
             }
    
            flag=false;// Let the System play next move
            display(matrix);//display the matrix
            cout<<"\nWait! System turns\n";
        }
    
        else{//System's Turn
            if(system1==true){//Chance of System of winning
                j=system_status%10;//get the j coordinate
                i=system_status/10;//get the i coordinate
                //cout<<"System chance win i = "<<i<<" j = "<<j<<endl;
    
                /*If Structure of Check whether place is empty for winning the system*/
                if(matrix[i][j]==BLANK){//Is place is empty
                    matrix[i][j]=1;
                    if(checkwin(matrix,i,j,1)){
                        display(matrix);//Display the current scenerio of the game
                        cout<<"Sorry You loose !! System wins\n";
                        return 0;
                    }//end if structure of check win
                }
                else//Means space is occupied by user, and chance of winning by system is lost
                    system1=false;//Now let the system to defense the user's move
                /*Ends If Structure of Check whether place is empty for winning the system*/
            }
    
            if(system1==false){
                if(user_status!=0){//If User is going to win , warn the system
                    j=user_status%10;//get the j coordinate
                    i=user_status/10;//get the i coordinate
                    //cout<<"User chance win i = "<<i<<" j = "<<j<<endl;
                }
                else{
                    if(v==9){//There is no point to check random number if noone is winning at the end
                            cout<<"\t\tMatch draw"<<endl;
                            return 0;
                    }
                    srand(time(NULL));
                    i=rand()%3; //random i coordinate
                    srand(time(NULL));
                    j=rand()%3; //random j coordinate
                }
                /*If System turn's of writting*/
                if(matrix[i][j]==BLANK)
                    matrix[i][j]=1;
                else
                    continue;
                /*End If Structure of writting system turn's*/
            }//end If Structure is sytem chance of win = false
    
            if(v>2){// This condition is necessary to avoid irrevelant check
                system_status=chance_of_win(matrix,i,j,1); //System chance of win
                if(system_status==0){
                        system1=false;
                        cout<<"\n Not System Chance of win \n";
                }
                else{
                    system1=true;
                    cout<<"\n System Chance of win \n";
                }
            }
            else{
                system_status=0;
                system1=false;
            }
    
            flag=true;//Let the user play his next move
            display(matrix);
    
        }
    v++;
    }//end of while v<9
    
    return 0;
    }