Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

i couldn't find my mistake here is my code.its a tic tac toe game

+2 votes
asked Sep 29 by vizagtop power vizag (140 points)
#include <iostream>
#include <string>
using namespace std;

string n1= " ";
string n2= " ";
char token='x';
int row;
int column;
char position[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
bool tie=false;

void fun1()
{
    cout<<"Enter name of First player: ";
    cin>>n1;
    cout<<endl;
    
    cout<<"Enter name of Second player: ";
    cin>>n2;
    cout<<endl;
    
    cout<<n1<<" plays First.\n";
    cout<<n2<<" plays Second.\n";
    
}

void fun2()
{
    
    cout<<"   |   |   "<<endl;
    cout<<" "<<position[0][0]<<" | "<<position[0][1]<<" | "<<position[0][2]<<" "<<endl;
    cout<<"___|___|___"<<endl;
    cout<<"   |   |   "<<endl;
    cout<<" "<<position[1][0]<<" | "<<position[1][1]<<" | "<<position[1][2]<<" "<<endl;
    cout<<"___|___|___"<<endl;
    cout<<"   |   |   "<<endl;
    cout<<" "<<position[2][0]<<" | "<<position[2][1]<<" | "<<position[2][2]<<" "<<endl;
    cout<<"   |   |   "<<endl;
    
}

void fun3()
{
    int digit;
    
    if(token='x')
    {
        cout<<n1<<" select your position: ";
        cin>>digit;
        cout<<endl;
    }
if(token='0')
    {
        cout<<n2<<" select your position: ";
        cin>>digit;
        cout<<endl;
    }if(digit ==1)
    {
        row=0;
        column=0;
    }if(digit ==2)
    {
        row=0;
        column=1;
    }if(digit ==3)
    {
        row=0;
        column=2;
    }if(digit ==4)
    {
        row=1;
        column=0;
    }if(digit ==5)
    {
        row=1;
        column=1;
    }if(digit ==6)
    {
        row=1;
        column=2;
    }if(digit ==7)
    {
        row=2;
        column=0;
    }if(digit ==8)
    {
        row=2;
        column=1;
    }if(digit ==9)
    {
        row=2;
        column=2;
    }
    
    else if(digit>9  || digit<1)
    {
        cout<<"Invalid!!!"<<endl;
    }
    
    if(token=='x' && position[row][column] !='x' && position[row][column] != '0')
    {
        position[row][column]='x';
        token='0';
    }
    
    else if(token=='0' && position[row][column] !='x' && position[row][column] != '0')
    {
        position[row][column]='0';
        token='x';
    }
    else
    {
        cout<<"There is no space."<<endl;
    }
}

bool fun4()
{
    for(int i=0;i<3;i++)
    {
        if(position[i][0]==position[i][1]  && position[i][0] == position[i][2]  ||  position[0][i]==position[1][i]  && position[1][i] == position[2][i])
        {
            return true;
        }
    }
    if(position[0][0]==position[1][1]  && position[1][1] == position[2][2] || position[0][2]==position[1][1]  && position[1][1] == position[2][0])
    {
        return true;
    }
    
    for(int x;x<3;x++)
    {
        for(int y;y<3;y++)
        {
            if(position[x][y] != 'x' &&  position[x][y] != '0')
            return false;
        }
    }
    tie = true;
    return false;
}

int main()
{
    fun1();
    while(!fun4())
    {
        fun2();
        fun3();
        fun4();
    }
    if(token =='x' && tie == false)
    {
        cout<<n2<<" Wins!!"<<endl;
    }
    if(token == '0' && tie==false)
    {
        cout<<n1<<" Wins!!"<<endl;
    }
    else
    {
        cout<<"Its a draw!"<<endl;
    }
    
    return 0;
}

1 Answer

0 votes
answered Oct 2 by xDELLx (10,360 points)

Correct your code as highlighted below .Also please Learn to Debug code it will make life easier.


 43 void fun3()
 44 {
 45     int digit;
 46
 47     if(token=='x')
 48     {

 49         cout<<n1<<" select your position: ";
 50         cin>>digit;
 51         cout<<endl;
 52     }
 53     if(token=='0')
 54     {

 55         cout<<n2<<" select your position: ";
 56         cin>>digit;
 57         cout<<endl;
 58     }if(digit ==1)
 59     {
 60         row=0;
 61         column=0;
 62     }if(digit ==2)
 63     {

.....

.....

132     for(int x=0;x<3;x++)
133     {
134         for(int y=0;y<3;y++)
135         {

136             if(position[x][y] != 'x' &&  position[x][y] != '0')
137             return false;
138         }
139     }


Too many if -else combinations present to verify other things.Checkout switch case it will improve readbilty of code

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...