// 2 players perfect with row,col & diagonal
// killing itself is not possible
//add starting 4 locations

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


//class soldier
//{
//  private:
 int arr[19][19];
 void initialize(void);
 void draw_all(void);
 int getkey(void);
 void goto_block(int);
 void draw_circle(int,int,int);
 void check(int,int,int);
 int move(int,int);
 int move_comp(int,int);
 int can_i_get(void);
//  public:
 void start(void);
//};

//void soldier::start(void)
void start(void)
{
  int aa;
 initialize();
 draw_all();
 int row,col;
 row=1;
 col=7;

 int i,j;
 for(i=1;i<19;i++)
  for(j=1;j<19;j++)
   arr[i][j]=-1;
   /*
 arr[9][9]=0;
 arr[10][10]=0;
 draw_circle(9,9,14);
 draw_circle(10,10,14);

 arr[10][9]=1;
 arr[9][10]=1;
 draw_circle(9,10,14+1);
 draw_circle(10,9,14+1);
     */
 aa=19*9+9;
 while(1)
 {
  for(j=0;j<2;j++)
  {
   do
   {
    if(j==0)
     aa=move(row,col);
    else
     aa=move_comp(row,col);//do not set arr[][] to 1(ur is 1)
    row=aa/19;
    col=aa%19;
   }while(arr[row][col]!=-1);
   arr[row][col]=j;
   draw_circle(row,col,14+j);
   check(row,col,!j);
   check(row,col,j);
  }
 }
}

int can_i_get(void)
//int soldier::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 soldier::move_comp(int row,int col)
int move_comp(int row,int col)
{
 return(move(row,col));
}

void main()
{
// soldier ss;
 start();
}

void check(int row,int col,int no)
//void soldier::check(int row,int col,int no)
{
 int other;
 other=!no;
 int i,j,k;

 i=row;
 do
 {
 i=i+1;
 }while(arr[i][col]==other&&i<18);
 if(i<18&&(i-row)>1)
 {
  if(arr[i][col]==no)
  {
   for(j=row+1;j<i;j++)
   {
    arr[j][col]=no;
    draw_circle(j,col,14+no);
   }
  }
 }

 i=row;
 do
 {
 i=i-1;
 }while(arr[i][col]==other&&i>0);
 if(i>0&&(row-i)>1)
 {
  if(arr[i][col]==no)
  {
   for(j=row;j>i;j--)
   {
    arr[j][col]=no;
    draw_circle(j,col,14+no);
   }
  }
 }

 i=col;
 do
 {
 i=i+1;
 }while(arr[row][i]==other&&i<18);
 if(i<18&&(i-col)>1)
 {
  if(arr[row][i]==no)
  {
   for(j=col+1;j<i;j++)
   {
    arr[row][j]=no;
    draw_circle(row,j,14+no);
   }
  }
 }

 i=col;
 do
 {
 i=i-1;
 }while(arr[row][i]==other&&i>0);
 if(i>0&&(col-i)>1)
 {
  if(arr[row][i]==no)
  {
   for(j=col;j>i;j--)
   {
    arr[row][j]=no;
    draw_circle(row,j,14+no);
   }
  }
 }

 i=row;
 j=col;
 do
 {
  i=i+1;
  j=j+1;
 }while(arr[i][j]==other&&i<18&&j<18);
 if(i<18&&j<18&&(i-row)>1)
 {
  if(arr[i][j]==no)
  {
   for(k=row,j=col;k<i;k++,j++)
   {
    arr[k][j]=no;
    draw_circle(k,j,14+no);
   }
  }
 }

 i=row;
 j=col;
 do
 {
  i=i-1;
  j=j-1;
 }while(arr[i][j]==other&&i>0&&j>0);
 if(i>0&&j>0&&(row-i)>1)
 {
  if(arr[i][j]==no)
  {
   for(k=row,j=col;k>i;k--,j--)
   {
    arr[k][j]=no;
    draw_circle(k,j,14+no);
   }
  }
 }

 i=row;
 j=col;
 do
 {
  i=i+1;
  j=j-1;
 }while(arr[i][j]==other&&i<18&&j>0);
 if(i<18&&j>0&&(i-row)>1)
 {
  if(arr[i][j]==no)
  {
   for(k=row,j=col;k<i;k++,j--)
   {
    arr[k][j]=no;
    draw_circle(k,j,14+no);
   }
  }
 }

 i=row;
 j=col;
 do
 {
  i=i-1;
  j=j+1;
 }while(arr[i][j]==other&&i>0&&j<18);
 if(i>0&&j<18&&(row-i)>1)
 {
  if(arr[i][j]==no)
  {
   for(k=row,j=col;k>i;k--,j++)
   {
    arr[k][j]=no;
    draw_circle(k,j,14+no);
   }
  }
 }
 ///////////////now check for killing oneself
 ////////up down
    /*
 if(arr[row][col+1]==other&&arr[row][col-1]==other&&col<18&&col>0)
 {
  arr[row][col]=other;
  draw_circle(row,col,14+other);
 }
 if(arr[row+1][col]==other&&arr[row-1][col]==other&&row<18&&row>0)
 {
  arr[row][col]=other;
  draw_circle(row,col,14+other);
 }
///diagonal
 if(arr[row-1][col-1]==other&&arr[row+1][col+1]==other&&row>0&&col>0&&row<18&&col<18)
 {
  arr[row][col]=other;
  draw_circle(row,col,14+other);
 }
 if(arr[row-1][col+1]==other&&arr[row+1][col-1]==other&&row>0&&col>0&&row<18&&col<18)
 {
  arr[row][col]=other;
  draw_circle(row,col,14+other);
 }*/
}

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

void draw_all(void)
//void soldier::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 soldier::getkey(void)
int getkey(void)
{
 union REGS i,o;
 while(!kbhit());
  i.h.ah=0;
 int86(0x16,&i,&o);
 return(o.h.ah);
}

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

//int soldier::move(int row,int col)
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)
//void soldier::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);
}