#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>

//enum fillcolor{yellow=15,white=14};
int arr[19][19];

void initialize(void);
void draw_all(void);
int getkey(void);
void goto_block(int);
void draw_circle(int,int,int);
int move(int,int);

void initialize(void)
{
 int gd,gm;
 gd=DETECT;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 cleardevice();
 setbkcolor(DARKGRAY);
 setcolor(BLUE);
 rectangle(0,0,getmaxx()-1,getmaxy()-1);
}

void draw_all(void)
{
 int i,j;
		  //all vertically
 for(i=1;i<7;i++)  //horizontal
  for(j=1;j<19;j++)//vertical
   rectangle(150+25*i,25*j,175+25*i,25+25*j);

		 //all horizontally
 for(i=1;i<19;i++)
  for(j=1;j<7;j++)
   rectangle(25*i,150+25*j,25+25*i,175+25*j);
}

int getkey(void)
{
 union REGS i,o;
 while(!kbhit());
  i.h.ah=0;
 int86(0x16,&i,&o);
 return(o.h.ah);
}

void goto_block(int no)
{
 rectangle(25*(no%19),25*(no/19),25+25*(no%19),25+25*(no/19));
}

int move(int row,int col)
{
 setcolor(GREEN);
 goto_block(row*19+col);
 int ch;
 do
 {
  ch=getkey();
  switch(ch)
  {
   case 72://move up
    if(row!=1 && row!=7)
    {
     setcolor(BLUE);
     goto_block(row*19+col);//row*19+column
     row=row-1;
     setcolor(GREEN);
     goto_block(row*19+col);
    }
    else
    {
     if(row==7 && col > 6 && col < 13 )
     {
      setcolor(BLUE);
      goto_block(row*19+col);
      row=row-1;
      setcolor(GREEN);
      goto_block(row*19+col);
     }
    }
    break;
   case 80: //move down
    if(row!=18 && row!=12)
    {
     setcolor(BLUE);
     goto_block(row*19+col);//row*19+column
     row=row+1;
     setcolor(GREEN);
     goto_block(row*19+col);
    }
    else
    {
     if(row==12 && col > 6 && col < 13 )
     {
      setcolor(BLUE);
      goto_block(row*19+col);
      row=row+1;
      setcolor(GREEN);
      goto_block(row*19+col);
     }
    }
    break;
   case 75://move left
    if(col!=1 && col!=7)
    {
     setcolor(BLUE);
     goto_block(row*19+col);//row*19+column
     col=col-1;
     setcolor(GREEN);
     goto_block(row*19+col);
    }
    else
    {
     if(col==7 && row > 6 && row < 13 )
     {
      setcolor(BLUE);
      goto_block(row*19+col);
      col=col-1;
      setcolor(GREEN);
      goto_block(row*19+col);
     }
    }
    break;
   case 77: //move right
    if(col!=12 && col!=18)
    {
     setcolor(BLUE);
     goto_block(row*19+col);//row*19+column
     col=col+1;
     setcolor(GREEN);
     goto_block(row*19+col);
    }
    else
    {
     if(col==12 && row > 6 && row < 13 )
     {
      setcolor(BLUE);
      goto_block(row*19+col);
      col=col+1;
      setcolor(GREEN);
      goto_block(row*19+col);
     }
    }
    break;
  }
 }
 while(ch!=1&&ch!=28);//esc is to exit
 if(ch==1)
 {
  closegraph();
  exit(1);
 }
 return row*19+col;
}

void draw_circle(int a, int b,int color)
{
 a=a*25+12;
 b=b*25+12;
 setcolor(color);
 circle(b,a,10);
 setfillstyle(SOLID_FILL,color);
 floodfill(b,a,color);
}

void display_result()
{
 int i,j,c1=0,c2=0;
 char temp[10];
 settextstyle(1,0,1);

 setfillstyle(SOLID_FILL,getbkcolor());
 bar(50,80,100,100);
 bar(400,80,450,100);

 setcolor(YELLOW);
 outtextxy(50,30,"Player 1");

 for(i=1;i<19;i++)
  for(j=1;j<19;j++)
   if(arr[i][j]==1)
    c1++;
 itoa(c1,temp,10);
 outtextxy(50,80,temp);

 setcolor(WHITE);////////////1
 outtextxy(400,30,"Player 2");
 for(i=1;i<19;i++)
  for(j=1;j<19;j++)
   if(arr[i][j]==2)
    c2++;
 itoa(c2,temp,10);
 outtextxy(400,80,temp);

 if(c1+c2==180)
 {
  setcolor(LIGHTMAGENTA);
  settextstyle(4,0,6);
  if(c1>c2)
  {
   outtextxy(150,200,"Player 1 Wins.");
  }
  else
  {
   if(c1<c2)
   {
    outtextxy(150,200,"Player 2 Wins.");
   }
   else
   {
    outtextxy(150,200,"It's a Draw.");
   }
  }
  getch();
  closegraph();
  exit(1);
 }
}

int check_row(int no)
{
 int ab=0;
 int other;
 if(no==1)
  other=2;
 else
  other=1;
 int i,j,ii;
 int start,end;

 for(i=1;i<19;i++)
 {
  for(j=1;j<19;j++)
  {
   if(arr[i][j]==no&&arr[i][j+1]==other)
   {
    start=j+1;
    do
    {
     j=j+1;
    }while(arr[i][j]==other);
    end=j;
    if(arr[i][j]==no)
    {
     for(ii=start;ii<end;ii++)
     {
      ab=1;
      arr[i][ii]=no;
      draw_circle(i,ii,13+no);
     }
    }
   }
  }
 }
 return ab;
}

int check_column(int no)
{
 int ab=0;
 int other;
 if(no==1)
  other=2;
 else
  other=1;
 int i,j,ii;
 int start,end;

 for(i=1;i<19;i++)
 {
  for(j=1;j<19;j++)
  {
   if(arr[i][j]==no&&arr[i+1][j]==other)
   {
    start=i+1;
    do
    {
     i=i+1;
    }while(arr[i][j]==other);
    end=i;
    if(arr[i][j]==no)
    {
     for(ii=start;ii<end;ii++)
     {
      ab=1;
      arr[ii][j]=no;
      draw_circle(ii,j,13+no);
     }
    }
   }
  }
 }
 return ab;
}



int can_i_get(void)
{
 int bb,aa=0;
 int i,j;

 int j1,j2;

 for(i=1;i<19;i++)
 {
  if(i<7)
  {
   j1=7;
   j2=13;
  }
  else
  {
   if(i<13)
   {
    j1=1;
    j2=19;
   }
   else
   {
    j1=7;
    j2=13;
   }
  }
  for(j=j1;j<j2;j++)
  {
   if(arr[i][j]==2&&arr[i][j+1]==1)
   {
    do
    {
     j=j+1;
    }while(arr[i][j]==1&&j<j2-1);
    if(arr[i][j]==0)
    {
     bb=i*19+j;
     if(bb>aa && (arr[i][j1]!=2||arr[i][j2-1]!=2) )
     {
      aa=bb;
     }
    }
   }
  }
 }

 for(i=1;i<19;i++)
 {
  if(i<7)
  {
   j1=6;
   j2=12;
  }
  else
  {
   if(i<13)
   {
    j1=0;
    j2=18;
   }
   else
   {
    j1=6;
    j2=12;
   }
  }
  for(j=j2;j>j1;j--)
  {
   if(arr[i][j]==2&&arr[i][j-1]==1)
   {
    do
    {
     j=j-1;
    }while(arr[i][j]==1&&j>j1+1);
    if(arr[i][j]==0)
    {
     bb=i*19+j;
     if(bb>aa && (arr[i][j1+1]!=2||arr[i][j2]!=2) )
     {  // if both sides are of computers do not take it in last it is urs
      aa=bb;
     }
    }
   }
  }
 }
 return aa;//aa as r*19+c
}



int move_computer(int row,int col)//value for computer is 2
{
 setcolor(BLUE);
 goto_block(row*19+col);

 int i,j,op;

 op=can_i_get();

 if(op==0)
 {

  for(i=7;i<13;i++)// in row of 18 if one side is computers take other
   if( (arr[i][1]==2&&arr[i][18]==0) || (arr[i][1]==0&&arr[i][18]==2))
    if(arr[i][1]==2)
     return i*19+18;
    else
     return i*19+1;

  for(i=1;i<7;i++) // in row of 6 one side is computers take other
   if( (arr[i][7]==2&&arr[i][12]==0) || (arr[i][7]==0&&arr[i][12]==2))
    if(arr[i][7]==2)
     return i*19+12;
    else
     return i*19+7;

  for(i=13;i<19;i++) // in row of 6 one side is computers take other
   if( (arr[i][7]==2&&arr[i][12]==0) || (arr[i][7]==0&&arr[i][12]==2))
    if(arr[i][7]==2)
     return i*19+12;
    else
     return i*19+7;

  for(i=7;i<13;i++)  //rows where both sides are blank
   if(arr[i][1]==0&&arr[i][18]==0)
    if(arr[i][1]==0)
     return i*19+1;

  for(i=1;i<7;i++) //corner of row of 6
   if(arr[i][7]==0&&arr[i][12]==0)
    if(arr[i][7]==0)
     return i*19+7;

  for(i=13;i<19;i++) //corner of row of 6
   if(arr[i][7]==0&&arr[i][12]==0)
    if(arr[i][7]==0)
     return i*19+7;

  for(i=7;i<13;i++)  //rows where any one side is blank
   if(arr[i][1]==0||arr[i][18]==0)
    if(arr[i][1]==0)
     return i*19+1;
    else
     return i*19+18;

  for(i=1;i<7;i++)
   if(arr[i][7]==0||arr[i][12]==0)
    if(arr[i][7]==0)
     return i*19+7;
    else
     return i*19+12;

  for(i=13;i<19;i++)
   if(arr[i][7]==0||arr[i][12]==0)
    if(arr[i][7]==0)
     return i*19+7;
    else
     return i*19+12;

  // comp can not get points and it even does not have any corners
  // rows where both sides are of comp will be in last of computes
  // so rows where one side is computer's are considered now

  for(i=7;i<13;i++)
  {
   if( (arr[i][1]==2&&arr[i][18]==1) || (arr[i][1]==1&&arr[i][18]==2))
   {
    if(arr[i][1]==2)
    {
     j=1;
     do
     {
      j=j+1;
     }while(arr[i][j]==2&&j<18);
     if(arr[i][j]==0)
     {
      return i*19+j;
     }
    }
    else
    {//here arr[i][18] is equal to 2
     j=18;
     do
     {
      j=j-1;
     }while(arr[i][j]==2&&j>1);
     if(arr[i][j]==0)
     {
      return i*19+j;
     }
    }
   }
  }//row 7 to 13 one element check completed Now same check for row 1 to 6

  for(i=1;i<7;i++)
  {
   if( (arr[i][7]==2&&arr[i][12]==1) || (arr[i][7]==1&&arr[i][12]==2))
   {
    if(arr[i][7]==2)
    {
     j=7;
     do
     {
      j=j+1;
     }while(arr[i][j]==2&&j<12);
     if(arr[i][j]==0)
     {
      return i*19+j;
     }
    }
    else
    {//here arr[i][18] is equal to 2
     j=12;
     do
     {
      j=j-1;
     }while(arr[i][j]==2&&j>7);
     if(arr[i][j]==0)
     {
      return i*19+j;
     }
    }
   }
  }//now for row 13 to 18

  for(i=13;i<19;i++)
  {
   if( (arr[i][7]==2&&arr[i][12]==1) || (arr[i][7]==1&&arr[i][12]==2))
   {
    if(arr[i][7]==2)
    {
     j=7;
     do
     {
      j=j+1;
     }while(arr[i][j]==2&&j<12);
     if(arr[i][j]==0)
     {
      return i*19+j;
     }
    }
    else
    {//here arr[i][18] is equal to 2
     j=12;
     do
     {
      j=j-1;
     }while(arr[i][j]==2&&j>7);
     if(arr[i][j]==0)
     {
      return i*19+j;
     }
    }
   }
  }

///////play any random
  randomize();
  do
  {
   i=1+random(17);
   if(i<7||i>12)
    j=7+random(5);
   else
    j=1+random(17);
  }while(arr[i][j]!=0);
  return i*19+j;
 }
 else
 {
  return op;
 }
}
