10363 - Tic Tac Toe

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

usmankayani
New poster
Posts: 1
Joined: Thu Dec 27, 2012 9:27 am

10363 tic tac toe

Post by usmankayani »

i can not find my compilation error

#include<iostream>
using namespace std;

int main()
{

int p=0,c,b,check=0,repeat;
cin>>repeat;

do
{
char y='y';
char a[3][3]={'.','.','.','.','.','.','.','.','.'};
p=0;
do
{
system("cls");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<a[j];
}
cout<<endl;
}
if((a[0][1]=='X'&&a[1][1]=='X'&&a[2][1]=='X')||(a[0][0]=='X'&&a[1][0]=='X'&&a[2][0]=='X')||(a[0][2]=='X'&&a[1][2]=='X'&&a[2][2]=='X'))
{
cout<<"YES "<<endl;
y='f';
}
else
{
if((a[0][1]=='O'&&a[1][1]=='O'&&a[2][1]=='O')||(a[0][0]=='O'&&a[1][0]=='O'&&a[2][0]=='O')||(a[0][2]=='O'&&a[1][2]=='O'&&a[2][2]=='O'))
{
cout<<"NO"<<endl;
y='f';
}
}
if((a[0][0]=='X'&&a[0][1]=='X'&&a[0][2]=='X')||(a[1][0]=='X'&&a[1][1]=='X'&&a[1][2]=='X')||(a[2][0]=='X'&&a[2][1]=='X'&&a[2][2]=='X'))
{
cout<<"YES "<<endl;
y='f';
}
else
{
if((a[0][0]=='O'&&a[0][1]=='O'&&a[0][2]=='O')||(a[1][0]=='O'&&a[1][1]=='O'&&a[1][2]=='O')||(a[2][0]=='O'&&a[2][1]=='O'&&a[2][2]=='O'))
{
cout<<"NO "<<endl;
y='f';
}
}
if((a[0][0]=='X'&&a[1][1]=='X'&&a[2][2]=='X')||(a[0][2]=='X'&&a[1][1]=='X'&&a[2][0]=='X'))
{
cout<<"YES "<<endl;
y='f';
}
else
{
if((a[0][0]=='O'&&a[1][1]=='O'&&a[2][2]=='O')||(a[0][2]=='O'&&a[1][1]=='O'&&a[2][0]=='O'))
{
cout<<"NO"<<endl;
y='f';
}
}
if(y=='y')
{
if(p==0)
{
cin>>b;
cin>>c;
if(a[c]=='.'&&b<3&&c<3)
{
a[c]='X';
p=1;
}
else
{
cout<<"Invalid choice - try again"<<endl;
}
}
else
{
cin>>b>>c;
if(a[c]=='.'&&b<3&&c<3)
{
a[c]='O';
p=0;
}
else
{
cout<<"Invalid choice - try again"<<endl;
}
}
}
int o=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(a[j]!='.')
{
o++;
}
}
}
if(o==9)
{
cout<<" TIE !! "<<endl;
y='f';
}
}while(y=='y');
check++;
system("pause");
}while(check<repeat);


}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10363 tic tac toe

Post by brianfry713 »

Click my submissions to see the reason for your compile error.
don't use system("pause");
Check input and AC output for thousands of problems on uDebug!
raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

Re: 10363 - Tic Tac Toe

Post by raj »

how become this a valid tic tac game?????

1
XXX
XOO
XOO

why this case indicates "yes" as we see x wins 2 time how this is possible...??????
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10363 - Tic Tac Toe

Post by brianfry713 »

.XX
XOO
XOO
Check input and AC output for thousands of problems on uDebug!
raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

Re: 10363 - Tic Tac Toe

Post by raj »

ohh thanks sir.... :@brainfry... :D
Farsan
New poster
Posts: 34
Joined: Fri Aug 12, 2011 6:37 am

Re: 10363 - Tic Tac Toe

Post by Farsan »

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10363 - Tic Tac Toe

Post by brianfry713 »

Try the I/O in this thread.
Check input and AC output for thousands of problems on uDebug!
Dr. Dre
New poster
Posts: 2
Joined: Tue Jan 07, 2014 6:24 am

Re: 10363 - Tic Tac Toe

Post by Dr. Dre »

Thanks to every one who contributed their knowledge to this. I appreciate it

My 2 cents:
Make sure to skip unwanted input once you exit out of a for-loop early. So if you are reading in 9 values (each spot on the board), make sure to skip over the remaining values if you happen to exit the for-loop early.

The conditions I used:

Code: Select all

IF sum(wins) == 0:
    // check that either X == O or X - 1 == O
ELSE IF sum(wins) == 1:
    IF xwin == 1:
        // check that X - 1 == O
    ELSE: // owin == 1
        // check that X == O
ELSE
    // Cannot possibly come from tictactoe game
milton00
New poster
Posts: 1
Joined: Sat Mar 07, 2015 9:59 am

Re: 10363 - Tic Tac Toe

Post by milton00 »

pls anybody help me..!! i am new poster in this site.. i don't know why i am getting WA....on this problem..

Code: Select all

    
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    int i,j,x_count,o_count,flag_x,flag_o,t,test;
    char B[10][10];
    //freopen("c.txt","r",stdin);
    scanf("%d",&test);
    for(t=1; t<=test; t++)
    {
        x_count=0,o_count=0;

        for(i=0; i<3; i++)
        {
            getchar();
            for(j=0; j<3; j++)
            {
                scanf("%c",&B[i][j]);
                if(B[i][j]=='X')
                    x_count++;
                else if(B[i][j]=='O')
                    o_count++;
            }
        }
        getchar();

        flag_x=0,flag_o=0;

        if((B[0][0]==B[0][1]&&B[0][1]==B[0][2])||(B[0][0]==B[1][0]&&B[1][0]==B[2][0])||(B[0][0]==B[1][1]&&B[1][1]==B[2][2]))
        {
            if(B[0][0]=='X')
                flag_x=1;
            else if(B[0][0]=='O')
                flag_o=1;
        }
        if((B[0][0]==B[0][1]&&B[0][1]==B[0][2])||(B[0][1]==B[1][1]&&B[1][1]==B[2][1]))
        {
            if(B[0][1]=='X')
                flag_x=1;
            else if(B[0][1]=='O')
                flag_o=1;
        }
        if((B[0][0]==B[0][1]&&B[0][1]==B[0][2])||(B[0][2]==B[1][2]&&B[1][2]==B[2][2])||(B[0][2]==B[1][1]&&B[1][1]==B[2][0]))
        {
            if(B[0][2]=='X')
                flag_x=1;
            else if(B[0][2]=='O')
                flag_o=1;
        }
        if((B[0][0]==B[1][0]&&B[1][0]==B[2][0])||(B[1][0]==B[1][1]&&B[1][1]==B[1][2]))
        {
            if(B[1][0]=='X')
                flag_x=1;
            else if(B[1][0]=='O')
                flag_o=1;

        }
        if((B[0][0]==B[1][1]&&B[1][1]==B[2][2])||(B[0][2]==B[1][1]&&B[1][1]==B[2][0])||(B[0][1]==B[1][1]&&B[1][1]==B[2][1])||(B[1][0]==B[1][1]&&B[1][1]==B[1][2]))
        {
            if(B[1][1]=='X')
                flag_x=1;
            else if(B[1][1]=='O')
                flag_o=1;

        }
        if((B[1][0]==B[1][1]&&B[1][1]==B[1][2])||(B[0][2]==B[1][2]&&B[1][2]==B[2][2]))
        {
            if(B[1][2]=='X')
                flag_x=1;
            else if(B[1][2]=='O')
                flag_o=1;

        }
        if((B[0][0]==B[1][0]&&B[1][0]==B[2][0])||(B[0][2]==B[1][1]&&B[1][1]==B[2][0])||(B[2][0]==B[2][1]&&B[2][1]==B[2][2]))
        {
            if(B[2][0]=='X')
                flag_x=1;
            else if(B[2][0]=='O')
                flag_o=1;

        }
        if((B[2][0]==B[2][1]&&B[2][1]==B[2][2])||(B[0][1]==B[1][1]&&B[1][1]==B[2][1]))
        {
            if(B[2][1]=='X')
                flag_x=1;
            else if(B[2][1]=='O')
                flag_o=1;

        }
        if((B[0][0]==B[1][1]&&B[1][1]==B[2][2])||(B[0][2]==B[1][2]&&B[1][2]==B[2][2])||(B[2][0]==B[2][1]&&B[2][1]==B[2][2]))
        {
            if(B[2][2]=='X')
                flag_x=1;
            else if(B[2][2]=='O')
                flag_o=1;
        }
        if(flag_o==1&flag_x==1)
            printf("no\n");
        else if(flag_o==1&&o_count==x_count)
            printf("yes\n");
        else if(flag_o==1&&o_count<x_count)
            printf("no\n");
        else if(x_count==o_count||(x_count-o_count)==1)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}   
pfiesteria
New poster
Posts: 9
Joined: Mon Aug 13, 2007 7:45 am

Re: 10363 - Tic Tac Toe

Post by pfiesteria »

Finally I got AC using Python :D . But I suppose input data may have garbage in the last (e.g. additional space), so length for every row input is NOT 3...
Post Reply

Return to “Volume 103 (10300-10399)”