#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <graphics.h>


void printboard(int);
void nextgame();
int n,r,c,pr,pc,kr,kc;
int winlevel=9000,smlevel=9000;
int maxlevel;

char grid[104][104][2];
int turn;
int commentary=0;
char *path;

int main(int argc,char* argv[])
{

	commentary=0;
	path="";

	if(argc>1)
		commentary=atoi(argv[1]);
	if(argc>2)
		path=argv[2];



		cout << "\nTRYST 2001 - ByteWise Contest \nSubmitted by:\nAshish Gupta and Sumanta Bhowmik\n98131\nD-30  , D-32 Jwalamukhi Hostel";
		cout << "\n\nEnter number of games to analyze :";
	 cin >> n;
	 int i;
	 for(i=0;i<n;i++)
	 {

                cout << "\nEnter Test Case (Board r,c ; Pawn r,c ; Knight r,c" << (i+1) << endl;
		cin >> r >> c >> pr >> pc >> kr >> kc;

		//___Data validation____
		if(r<3 || r>=100 || c<2 || c>=100 || pr<1 || pr>r || pc < 1 || pc>c ||
			kr< 1 || kr>r || kc <1 || kc>c)
		{
			cout << "\nInvalid input !";
			continue;
		 }


		nextgame();
		closegraph();

	}

	return 0;
}


// This function prints the board for the current move
void printboard(int move)
{
		if(commentary==2)
		{
			int grDriver=VGA,grMode=VGAHI;


			initgraph(&grDriver,&grMode,path);

			if(graphresult()) cout << "\n\nPath entered for graphic display is not correct";


int bw,bh;
bw=getmaxx()/c;
bh=(getmaxy()-50)/r;
int xc=0,yc=0;
int tc=getmaxy()-40;

		char buffer[100];
		sprintf(buffer,"Analyzing move number %d",move);

		outtextxy(10,tc,buffer);
		int i,j,k;
		for(i=r;i>=1;i--)
		{
			xc=0;
			for(j=1;j<=c;j++)
			{
				if(grid[1+i][1+j][turn]==1 && i==pr+move && j==pc)
				{
					setfillstyle(SOLID_FILL,EGA_GREEN);
					bar(xc,yc,xc+bw,yc+bh);
					outtextxy(xc+1,yc+1,"W");
				}

				else if(grid[1+i][1+j][turn]==1)
				{
					setfillstyle(SOLID_FILL,EGA_RED);
					bar(xc,yc,xc+bw,yc+bh);
					outtextxy(xc+1,yc+1,"K");

				}
				else if(i==pr+move && j==pc)
				{
					setfillstyle(SOLID_FILL,EGA_BLUE);
					bar(xc,yc,xc+bw,yc+bh);
					outtextxy(xc+1,yc+1,"P");

				}

				  else
				  {
//					setfillstyle(SOLID_FILL,EGA_GREEN);
					rectangle(xc,yc,xc+bw,yc+bh);

				  }

					xc+=bw;


			   }

			   yc+=bh;


		}


getchar();
return;

		}

		int i,j,k;
		cout << "\nMove number : " << move;
		for(i=r;i>=1;i--)
		{
			cout << endl;
			for(j=1;j<=c;j++)
			{
				if(grid[1+i][1+j][turn]==1 && i==pr+move && j==pc)
					cout << " W";

				else if(grid[1+i][1+j][turn]==1)

					cout << " K";
				else if(i==pr+move && j==pc)
					cout << " P";

				  else cout << " .";
			   }

		}
		cout << endl;
		getchar();

}

// This function actually tries all the moves and finds the winning move
void nextgame()
{
   int i,j,k;

	for(i=0;i<103;i++)
		for(j=0;j<103;j++)
			grid[i][j][0]=grid[i][j][1]=0;

	int move;
	int maxmoves=r-pr;

	turn=0;
	grid[1+kr][1+kc][turn]=1;

	for(move=0;move<=maxmoves;move++)
	{
		  cout << "\nAnalyzing move number... " << move;
		   if(commentary)
			printboard(move);

		//___pawn wins
		if(pr+move==r)
		{
			break;
		}

		//___Check stats for current move___
		if(grid[1+pr+move][1+pc][turn]==1)
		{
			winlevel=move;
			break;
		}

		//___stalemate
		if(grid[1+pr+move+1][1+pc][turn]==1 && smlevel>200)
		{
		  smlevel=move;
		  //break;
		}

		for(i=1;i<=r;i++)
		{

			for(j=1;j<=c;j++)
			{
				if(grid[1+i][1+j][turn]==1)
				{
					grid[1+i][1+j][turn]=0;

					grid[1+i+1][1+j+2][1-turn]=1;
					grid[1+i+1][1+j-2][1-turn]=1;
					grid[1+i-1][1+j+2][1-turn]=1;
					grid[1+i-1][1+j-2][1-turn]=1;


					grid[1+i+2][1+j+1][1-turn]=1;
					grid[1+i+2][1+j-1][1-turn]=1;
					grid[1+i-2][1+j+1][1-turn]=1;
					grid[1+i-2][1+j-1][1-turn]=1;


				  }
			   }

			}

			turn=1-turn;
		}

	closegraph();
	cout << "\n\n";
	if(winlevel<9000)
	{
		cout << "\nKnight wins after " << winlevel << " moves.";

	}
	else if(smlevel<9000)
	{
		cout << "\nKnight stalemates after " << smlevel << " moves.";


	}
	else
	{
	  cout << "\nPawn wins after " << (r-pr-1) << " moves of Knight.";

	}
}
