#include<iostream.h>
#include<stdio.h>
char A[3][3]={1,2,3,
              4,5,6,
              7,8,9};
void printmaze();
void makemove(int,int);
void checkmove1(int,int);
void checkmove();
void checkrow();
void checkcol();
void checkdiag1();
void checkdiag2();
void makealtmove();
void result();



int main()
{
int m11,m12,m21,m22,m31,m32,m41,m42,m51,m52;
char ch;
do
 {
 cout<<"Welcome to tic-tac-toe"<<endl;
 printmaze();
 cout<<"for choosing 1--> input 0 0, 2-->0 1, 3-->0 2 \n 4-->1 0, 5-->1 1, 6-->1 2 \n 7-->2 0, 8--> 2 1, 9-->2 2\n";

 cout<<"Enter space indices";
 cin>>m11>>m12;
 makemove(m11,m12);
 cout<<"Now the computer will make its move\n";
 checkmove1(m11,m12);

 cout<<"Make your move";
 cin>>m21>>m22;
 makemove(m21,m22);
 checkmove();

 cout<<"Make your move";
 cin>>m31>>m32;
 makemove(m31,m32);
 checkmove();

 cout<<"Make your move";
 cin>>m41>>m42;
 makemove(m41,m42);
 checkmove();

 cout<<"Make your move";
 cin>>m51>>m52;
 makemove(m51,m52);
 checkmove();

 cout<<"Analyzing game results...please wait"<<endl;
 result();

 cout<<"Do you want to play another game?(Y/N)";
 ch=getchar();
 }while(ch=='Y');
return 0;
}


//printing function
void printmaze()
{
    cout << "     |     |     " << endl;
    cout << "  " << A[0][0] << "  |  " << A[0][1] << "  |  " << A[0][2] << endl;

    cout << "_____|_____|_____" << endl;
    cout << "     |     |     " << endl;

    cout << "  " << A[1][0] << "  |  " << A[1][1] << "  |  " << A[1][2] << endl;

    cout << "_____|_____|_____" << endl;
    cout << "     |     |     " << endl;

    cout << "  " << A[2][0] << "  |  " << A[2][1] << "  |  " << A[2][2] << endl;

    cout << "     |     |     " << endl << endl;
}

//replacing function for making moves
void makemove(int i, int j)
{ A[i][j]='X';
  printmaze();
}

void checkmove1(int i, int j)
{ if(i==1&&j==1)
  {A[0][0]='0';
   printmaze();}
  else
  {A[1][1]='0';
   printmaze();}
}

void checkmove()
{
cout<<"Now the computer will make its move\n";
checkrow();
} 

void checkrow()
{int i,j,nox=0,flag=1;
 for(i=0;i<3;i++)
 {for(j=0;j<3;j++)
  {
   if(A[i][j]=='X')
   nox++;
  }
  if(nox==2)
  { for(j=0;j<3;j++)
    {if((A[i][j]!='X') && (A[i][j]!='0'))
      {A[i][j]='0';
      printmaze();
       flag++;}
    }
  }
  if(flag==1)
  checkcol();
} 



void checkcol()
{int i,j,nox=0,flag=1;
 for(j=0;j<3;j++)
 {for(i=0;i<3;i++)
  {
   if(A[i][j]=='X')
   nox++;
  }
  if(nox==2)
  { for(i=0;i<3;i++)
    {if((A[i][j]!='X') && (A[i][j]!='0'))
      {A[i][j]='0';
      printmaze();
       flag++;}
    }
  }
  if(flag==1)
  checkdiag1();
} 




void checkdiag2()
{int i,j,nox=0,flag=1;
 for(i=0,j=2;i<3;i++,j--)
 {if(A[i][j]=='X')
  nox++;
 }
 if(nox==2)
 {for(i=0,j=2;i<3;i++,j--)
  {if( (A[i][j]!='X') && (A[i][j]!='0') )
   { A[i][j]='0';
     printmaze();
     flag++;
   }
  } 
 }
if(flag==1)
makealtmove();
}


void makealtmove()
{int i,j;
for(i=0;i<3;i++)
 {for(j=0;j<3;j++)
  {if( (A[i][j]!='X') && (A[i][j]!='0') )
  {A[i][j]='0'
  printmaze();}
  }
 }
}

void result()
{ int i,j,nox,noo,flag=1;
  for(i=0;i<3;i++)
  { nox=0;
    noo=0;
    for(j=0;j<3;j++)
   { if(A[i][j]=='X')
     nox++;
     if(A[i][j]=='0')
     noo++;
   }
    if(nox==3)
    {cout<<"You win!! Congratulations";
     flag++;}
    if(noo=3)
    {cout<<"You lose!! Computer wins";
     flag++;} 
  }

  if(flag==1)
 { for(j=0;j<3;j++)
  { nox=0;
    noo=0;
    for(i=0;i<3;i++)
   { if(A[i][j]=='X')
     nox++;
     if(A[i][j]=='0')
     noo++;
   }
    if(nox==3)
   {cout<<"You win!! Congratulations";
   flag++;}
   if(noo=3)
   {cout<<"You lose!! Computer wins";
   flag++;} 
  }
 }

if(flag==1)
 { nox=0;
   noo=0;
   for(i=0,j=2;i<3;i++,j--)
  { if(A[i][j]=='X')
    nox++;
    if(A[i][j]=='0')
    noo; 
  }
   if(nox==3)
   {cout<<"You win!! Congratulations";
   flag++;}
   if(noo=3)
   {cout<<"You lose!! Computer wins";
   flag++;}
 }

if(flag==1)
{cout<<"The game is a draw between you and computer";}

}

Edited 1 Year Ago by deepeshmathuria: Typo mistake

What are the errors the compiler is giving you? Al so formating of you code is terrible. Code block should look like either of the following

some line of code;
if (some condintion)
{
    some line of code;
    another line of code;
    if (another condition)
    {
        some line of code;
    }
}

// or

some line of code;
if (some condintion) {
    some line of code;
    another line of code;
    if (another condition) {
        some line of code;
    }
}

I personally perfer the first method because you can clearly see the brackets match. The second method seams to be just as popular if not more so. The biggest thing is find one way of doing your brackets and indentation and stay with it throughout your code.

Good Variable names are another part of good codding.

int m11,m12,m21,m22,m31,m32,m41,m42,m51,m52;

What are these variables for? I have no idea. It looks like from your code that they might be x and y posistions but thats just a guess. You dont seam to do anythig with them after you pass them to makemove() so why not use one set of variables and keep reusing them?

You seem to be writing your code in a version of C++ from about 20 years ago. A modern compiler doesn't recognise <iostream.h>, and a modern compiler uses namespaces, which you're not doing. Let me guess; you're using Turbo C++ from about 1993, and you have had to force Windows 7 to work with it, even though Turbo C++ was writen for MSDOS twenty years ago? Are you forced to do this by school, or can you instead switch to a free, modern C++ compiler, instead of learning how to be a C++ programmer in the year 1993? This being the year 2014, it is very very hard to get a job in 1993.

Anyway.

Your function checkrow is wrong. It doesn't have enough }, or it has too many {

Your function checkcol is wrong. It doesn't have enough }, or it has too many {

As Nathan says, if you wrote your code so that you could see what you're doing, you wouldn't make those mistakes.

Line 175 is missing a ;

The function checkdiag1 does not exist.

Edited 1 Year Ago by Moschops

This article has been dead for over six months. Start a new discussion instead.