/*--------------------------------------------------------------------*\
	TRYST Problem #3

	Compiles under Platform :

	Turbo C++
	------------

						   MINESWEEPER
						   -----------
	A computer program to Play Minesweeper
	with fully interactive console graphics display !

									Designed and programmed by:
									   ASHISH GUPTA
									   A-2 , Jwalamukhi
									   98131
									   B.Tech
									   COMPUTER SCIENCE AND ENGINEERING
									   IITD


	All the help required to play the game is automatically
	displayed when the program is executed.

\*--------------------------------------------------------------------*/


#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>
#include <ctype.h>


//_______________Global Functions______________
void PaintBoard();
void DisplayUnit(int i,int j);
void Reverse();
void Normal();
void Normalize(int x,int y);
void PaintMines();
void DisplayScore(int score);
void DisplayIntro();
void Animate(char* s,int x,int y);

//___________Global Variables_____________
int board[10][10];
int bcopy[10][10];

void main()
{

clrscr();
DisplayIntro();

Animate("MINESWEEPER",34,1);
Normal();

int i,j;

for(i=1;i<=80;i++)
{
	gotoxy(i,2); cout << 'Í';
}


//____________Make grid__________

gotoxy(3,3); cout << 'É';
gotoxy(43,3); cout << '»';
gotoxy(3,23); cout << 'È';
gotoxy(43,23); cout << '¼';

//________H lines_______
for(i=4;i<=42;i++)
{
	gotoxy(i,3); cout << 'Í';
}
for(i=4;i<=42;i++)
{
	gotoxy(i,23); cout << 'Í';
}

//________V Lines______

for(i=4;i<=22;i++)
{
	gotoxy(3,i); cout << 'º';
}

for(i=4;i<=22;i++)
{
	gotoxy(43,i); cout << 'º';
}

//____________Actual Grid__________

for(i=5;i<23;i+=2)
{
	for(j=4;j<=42;j++)
	{
		gotoxy(j,i); cout << 'Ä';
	}
}

for(i=7;i<43;i+=4)
{
	for(j=4;j<=22;j++)
	{
		gotoxy(i,j); cout << '³';
	}
}


PaintBoard();

//__________Help System

for(i=3;i<=24;i++)
{
	gotoxy(48,i); cout << '³';
}

for(i=49;i<=80;i++)
{
	gotoxy(i,6); cout << 'Ä';
}

for(i=49;i<=80;i++)
{
	gotoxy(i,22); cout << 'Ä';
}

for(i=49;i<=80;i++)
{
	gotoxy(i,20); cout << 'Ä';
}

gotoxy(60,7);
Reverse();
cputs("HELP");
Normal();

gotoxy(55,9);
cputs("w  -   move up");
gotoxy(55,10);
cputs("s  -   move down");
gotoxy(55,11);
cputs("a  -   move left");
gotoxy(55,12);
cputs("d  -   move right");
gotoxy(55,13);
cputs("z  -   show all mines");
gotoxy(55,14);
cputs("Space  -   Dig here");
gotoxy(55,15);
cputs("i  -   Indicate Mine Here");
gotoxy(55,16);
cputs("m  -   Exit the game");
gotoxy(49,18);
cputs("No. indicates safe places around");




//_________Finished making board______

//_________Generate mines_________

int mines=0;
randomize();
while(mines<10)
{
	int x=random(10);
	int y=random(10);

	if(board[y][x]==0)
	{
		board[y][x]=-1;
		bcopy[y][x]=-1;
		mines++;
	}

}

//___________Implement Game____________
int x=0,y=0,mc=0;
int score=0;
char choice;

DisplayScore(score);
while(1)
{
	Reverse();
	gotoxy(5+x*4,4+y*2);
	DisplayUnit(x,y);

	choice=char(tolower(getch()));
	switch(choice)
	{
		case 'w':
			Normalize(x,y);
			if(y>0) y--;
			break;
		case 's':
			Normalize(x,y);
			if(y<9) y++;
			break;
		case 'a':
			Normalize(x,y);
			if(x>0) x--;
			break;
		case 'd':
			Normalize(x,y);
			if(x<9) x++;
			break;

		case 'i':       //________Indicate mine____
			board[y][x]=-2;
			break;


		case 'z':   //______Show Mines____________
			Normalize(x,y);
			PaintMines();

			gotoxy(54,23);
			Reverse();
			cputs("Press any key");

			getch();

			gotoxy(54,23);
			Normal();
			cputs("             ");

			PaintBoard();
			break;

		case ' ':  //________Dig here______
			if(bcopy[y][x]==-1)  //___________Bomb Found_____
			{
				gotoxy(54,5);
				textcolor(6);
				cputs("You lose the game");

				gotoxy(54,23);
				textcolor(0);
				cputs("Press any key");

				sound(70);
				delay(500);

				nosound();
				getch();
				exit(0);
			}

			if(board[y][x]>0)
				break;
			else
			{
				score++;
				DisplayScore(score);
			}


			mc=0;
			if(x>0  && y>0 && bcopy[y-1][x-1]!=-1) mc++;
			if(x<=8 && y>0 && bcopy[y-1][x+1]!=-1) mc++;
			if(x>0 && y<=8 && bcopy[y+1][x-1]!=-1) mc++;
			if (x<=8 && y<=8 && bcopy[y+1][x+1]!=-1) mc++;
			if(y>0 && bcopy[y-1][x]!=-1) mc++;
			if(x>0 && bcopy[y][x-1]!=-1) mc++;
			if(x<=8 && bcopy[y][x+1]!=-1) mc++;
			if(y<=8 && bcopy[y+1][x]!=-1) mc++;
			board[y][x]=mc;
			break;

		case 'm':  //_______Exit______________
			exit(0);

	}


	//________If indcated mines are correct , Win_______
	int flag=1;
	for(i=0;i<10;i++)
	{
		for(j=0;j<10;j++)
		{
			if(board[j][i]==-1)
			{
				flag=0;
				break;
			}
		}
	}

			if(flag)  //___________Win the game_____
			{
				gotoxy(54,5);
				textcolor(6);
				cputs("You win the game");

				gotoxy(54,4);
				textcolor(6);
				Reverse();
				cputs("Congratulations !");

				gotoxy(54,23);
				textcolor(0);
				cputs("Press any key");
				getch();
				exit(0);
			}



}



}



void PaintMines()
{

int i,j;


for(i=0;i<10;i++)
{
	for(j=0;j<10;j++)
	{
		gotoxy(5+i*4,4+j*2);
		if(bcopy[j][i]==-1)
			cputs("*");
		else
			cputs("°");

	}
}


}

void PaintBoard()
{

int i,j;


for(i=0;i<10;i++)
{
	for(j=0;j<10;j++)
	{
		gotoxy(5+i*4,4+j*2);
		DisplayUnit(i,j);
	}
}
}

void DisplayUnit(int i,int j)
{
char b[2];
		if(board[j][i]>0)
			cputs(itoa(board[j][i],b,10));
		else if(board[j][i]==-2)
			cputs("*");
		else
			cputs("°");
}


void Reverse()
{
	textcolor(0);
	textbackground(7);
}

void Normal()
{
	textcolor(7);
	textbackground(0);
}

void Normalize(int x,int y)
{
gotoxy(5+x*4,4+y*2);

Normal();
DisplayUnit(x,y);
}


void DisplayScore(int score)
{
Normal();
gotoxy(52,21);
cout << "Score : " << score;
}

void DisplayIntro()
{
clrscr();
gotoxy(28,3);
Reverse();
Animate("Welcome to MINESWEEPER !",29,3);

gotoxy(28,4);
Reverse();
cputs("------------------------");

Normal();
cout << "\n\nThe Rules of the game are simple.";
cout << "\n\nYou are on a land containing 10 mines and you have to detect";
cout << "\nthose 10  mines by  indicating those. If you discover all 10";
cout << "\nmines correctly  then you WIN !. Or you  can get to know the";
cout << "\nnumber of safe  places around by digging a place. These will";
cout << "\nhelp you in  discovering the  mines around ! If you dig on a";
cout << "\nmine you DIE ! ";

cout << "\n\nBest of Luck !!";

cout << "\n\n\nNote :  Less the score better you are !\nThere is an option of cheating too !";


gotoxy(1,24);
cputs("Press any key...");
getch();
Normal();
clrscr();
}

void Animate(char* s,int x,int y)
{

int i,j,l;
char buffer[2];
buffer[1]=NULL;

l=strlen(s);

for(i=l-1;i>=0;i--)
{
	for(j=1;j<=(i+x-1);j++)
	{
		buffer[0]=s[i];
		Reverse();
		gotoxy(j,y);
		cputs(buffer);

		sound(200+j*50);
		delay(1);
		nosound();

	   if(j<(i+x-1))
	   {
			gotoxy(j,y);
			Normal();
			cputs(" ");
	   }
	}
}

}



