I have developed a player v/s player game for tic tac toe..but can't understand the logic for AI ..can someone please include the AI segment in my code because i so much need it for my school project

#include<iostream.h>

#include<conio.h>
#include<string.h>

char grid [3][3];
void display_grid()
  {
   for(int i=0;i<3;i++)
   {
      cout<<"\n\t_____________\n\t| ";
      for(int j=0;j<3;j++)
       cout<<grid[i][j]<<" | ";
   }
   cout<<"\n\t_____________\n\t";
}
int checkwin()
{
  int i,j,c=0;char ch;
  for(i=0;i<3;i++)
  {
    c=0;
    ch=grid[i][0];
    for(j=1;j<3;j++)
     if(ch!=grid[i][j]){c=1;break;}
    if(c==0){if(ch=='X')return 1; else if(ch=='O') return 2;}
  }
  for(j=0;j<3;j++)
  {
    c=0;
    ch=grid[0][j];
    for(i=1;i<3;i++)
     if(ch!=grid[i][j]){c=1;break;}
    if(c==0){if(ch=='X')return 1; else if(ch=='O')return 2;}
  }
  c=0;
  ch=grid[0][0];
  for(i=1;i<3;i++)
  {
     if(grid[i][i]!=ch){c=1;break;}
  }
  if(c==0){if(ch=='X')return 1; else if(ch=='O') return 2;}
  c=0;
  ch=grid[0][3-1];
  for(i=1;i<3;i++)
  {
     if(grid[i][3-i-1]!=ch){c=1;break;}
  }
  if(c==0){if(ch=='X')return 1; else if(ch=='O') return 2;}
  else return 0;

}
void main()
{
  clrscr();
  int r,c;
  int flag=0;
  int count=0;
  while(flag==0)
  {
    P1:
    cout<<"\n\n Player 1\n\n   row=";cin>>r;cout<<"   column=";cin>>c;
    if((r>0 && r<4)&&(c>0 && c<4))
     {grid[r-1][c-1]='X';count++;}
    else
     {cout<<"\n Invalid, Try Again..!";goto P1;}
    display_grid();
    flag=checkwin();
    if(flag==1){cout<<"\n Player 1 WINS ";break;}else if(flag==2){cout<<"\n Player 2 WINS ";break;}
    if(count==9){cout<<"\n Draw..!";break;}

    P2:
    cout<<"\n\n Player 2\n\n   row=";cin>>r;cout<<"   column=";cin>>c;
    if((r>0 && r<4)&&(c>0 && c<4)&&grid[r-1][c-1]=='\0')
     {grid[r-1][c-1]='O';count ++;}
    else
     {cout<<"\n Invalid, Try Again..!";goto P2;}
    display_grid();
    flag=checkwin();
    if(flag==1){cout<<"\n Player 1 WINS ";break;}else if(flag==2){cout<<"\n Player 2 WINS ";break;}
    if(count==9){cout<<"\n Draw..!";break;}
  }
  getch();
}

Harish

  1. Your code is unreadable - a solid mess of undocumented stuff strung end-to-end without any thought towards making it comprehensible. It even has gotos! It's useless.

  2. Can someone do the AI part of your school project for you? As in cheat? As in get an unfair advantage over your peers who actually do the work themselves? As in waste your teacher's time by not bothering to learn what this exercise is supposed to teach? Guess what... the answer is no.

Come back here with code we can read, make a serious attempt to do your own homework, ask sensible questoins, and we will help.

commented: You beat me by 22 seconds. +14

I don't imagine anyone here is going to write your code for you but at least you were honest enough to admit it is for a school assignment. Having said that I want to mention that if I were marking your assignment (and it is something I used to do in university) I would give you a zero based solely on your complete lack of comments.

You should write code as if it is meant for people to read and understand, and only incidentally for computers to execute.

I'm going to answer you another way. Look over the 48 implementions of Tic Tac Toe at https://rosettacode.org/wiki/Tic-tac-toe

It's a well down routine without any need for AI. But if you insist, the studies are done. Example: https://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe_AI.html

So did your class build your AI knowledge before the homework or were classes skipped, the course isn't a good one? There have been classes that the teacher well, was not engaged, going through the motions.

As in waste your teacher's time by not bothering to learn

Spot-on post James, except possibly for this part. Consider the headers...

#include<iostream.h>
#include<conio.h>
#include<string.h>

No comment on conio.h since I'm not familiar with it and whether it has been completely phased out, but the other two in a C++ program...

Unless the professor has a darn good reason for using these instead of iostream and cstring in 2017 (and I can't think of any for a new Tic Tac Toe program), it's the professor who is wasting everyone's time and not bothering to learn. Presumably the OP is using these headers because the professor told the class to use them.

There have been classes that the teacher well, was not engaged, going through the motions.

See above. And see below.

void main()

It is int main. Now I've been told that this is not a problem in some of the more modern C++ specs as it was before, but I still see no reason at all to not return 0 on a clean program exit even if the compiler does it for you. To follow up on RJ's point, a return 0 tells the READER where you are exiting if nothing else.

The void main combined with the old style headers and some of the other code suggests that the OP learned from someone who started with a C background decades ago and stopped keeping up with the times, or as rproffitt put it, is "going through the motions".

commented: It's rough but teachers should keep up. There's no excuse today for void main() and outdated compilers. Unlearning is harder than learning. +14

#include<conio.h>
Obviously Turbo C++ is used. Some countries still use it, and use older computer equipment.
By lack of educational budget? I don't know,
But it's better than nothing I guess.

commented: There are free compilers that are current. Not to mention Visual Studio Community edition. +14

There's no excuse today for <etc>. Unlearning is harder than learning.

That is the excuse why teachers continue to use outdated materials

commented: I get it. The teacher must unlearn. We have asked for too much! +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.