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

enum fillcolor{yellow=14,white=15};

void initialize(void);
void draw_all(void);
int getkey(void);
void goto_block(int);
void draw_circle(int,fillcolor);
int calc_pos(int);    //finding array position
int calc_rev_pos(int);
int return_row(int);
int move(int row,int col);


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 pos,fillcolor color)
{
 int row,col;
 row=pos%19;
 col=pos/19;
 row=row*25+12;
 col=col*25+12;
 setcolor(color);
 circle(row,col,10);
 setfillstyle(SOLID_FILL,color);
 floodfill(row,col,color);
}

int calc_pos(int pos)
{
 int row,col;
 row=pos/19;
 col=pos%19;

 int ans;
 if(row<7)
  ans=6*(row-1)+col-6;
 else
  if(row<13)
   ans=36+18*(row-1-6)+col;
  else
   ans=6*(row-1)+col-6+72;
  return ans;
}

int return_row(int arraypos)
{
 arraypos=arraypos-1;
 int row;
 if(arraypos<37)
  row=arraypos/6;
 else
  if(arraypos<145)
   row=6+(arraypos-36)/18;
  else
   row=12+(arraypos-144)/6;
 row=row+1;
 return row;
}

int calc_rev_pos(int pos)//from array pos find r*19+c
{
 int r,c,temp;
 if(pos<37)
 {
  r=return_row(pos);
  temp=pos-1;
  temp=temp%6;
  c=temp+1+6;
 }
 else
 {
  if(pos<145)
  {
   r=return_row(pos);
   temp=pos-37;
   temp=temp%18;
   c=temp+1;
  }
  else
  {
   r=return_row(pos);
   temp=pos-145;
   temp=temp%6;
   c=temp+1+6;
  }
 }
 return (r*19+c);
}

/*
void main()
{
 clrscr();
 int aa;
 aa=calc_rev_pos(180);
 cout<<"\n\t"<<aa;
 getch();
}
*/