/*
	MazezaM

	Copyright (C) 2002
	Malcolm  Tyrrell
	tyrrelmr@cs.tcd.ie

	CP/M version (C) 2004-2008
	Ventzislav  Tzvetkov
	http://drhirudo.hit.bg 

	z88dk version by Stefano Bodrato (www.z88dk.org)

	Color VT100 version with 42 levels by Anna Christina Naß <acn@acn.wtf>

	Build:

	zcc +cpm -lndos -O3 -o MAZEZAM.COM mazezam.c

	This code may be used and distributed under
	the terms of the GNU General Public Licence.
*/

// the following assures that this games works when using Z3PLUS etc.
#pragma output REGISTER_SP = 0xa000
#pragma output hrgpage = 36096

#include <stdio.h>	// We use printf(); putchar();
#include <string.h>	// strlen for the Level name centering.
#include <stdlib.h> // We use rand(); srand();
#include <conio.h>  // We use getk();

#define LEVELS	42	// Number of Levels
#define TRUE	1
#define FALSE	0

#define PLAYER		"P"	// player symbol
#define COLWAY		15	// color of entrance and exit
#define COLDOOR		11	// color of entrance door
#define COLPLAYER	14	// color of player
#define COLBRICK	8	// color of bricks

unsigned int uLives,uWidth,uHeight, i, j, l, t, r, rx, lx, bCloseFlag=FALSE, Column;
		/* Global miscellaneous variables - i,j for loops and Player coordinates,
		t,r,rx,lx - for the Level build. */

char *MazeLine[12], // Text pointer to the Level (Max 12 lines)
	Mazezam[200], // Storage of the level.
	PressedKey;   // Which key is pressed.
static char *ARGH[]={
			"ARGH!",
			"OUCH!",
			"GRRR!",
			"NOOU!",
			"NAAH!",
			"UUUF!",
			"AAAH!",
			"OUUF!",
			"PUFF!"
		};

/* we need 12 colors as the "biggest" maze has 12 lines */
static int LineColors[]={ 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13 };

/* We use escape control combinations for handling of the display */

ClearScreen()
{
	printf("\033[2J\033[H");
}

InverseOn()
{
	printf("\033[7m");
}

InverseOff()
{
	printf("\033[0m");
}

ResetAttr()
{
	printf("\033[0m");
}

FgCol(color)
int color;
{
	if(color<8)
		printf("\033[3%dm", color);
    if(color>=8)
        printf("\033[3%d;1m", color-8); // bright colors begin at 8
}

BgCol(color)
int color;
{
	char str[6];
	printf("\033[4%dm", color);
}

ShuffleColors()
{
	int n = sizeof(LineColors) / sizeof(LineColors[0]);
    for (int i = 0; i < n - 1; i++) 
	{
		int j = i + (rand()%n) / (n / (n - i) + 1);
		int t = LineColors[j];
		LineColors[j] = LineColors[i];
		LineColors[i] = t;
	}
}

PrintPlayer()
{
	FgCol(COLPLAYER);
	printf(PLAYER);
	ResetAttr();
}

ColorLine(line)
int line;
{
	FgCol(LineColors[line-1]);
}

void PrintEnter()
{
	printf("\n");
}

void FillRow()
{
	FgCol(COLBRICK);
	printf("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
	ResetAttr();
	PrintEnter();
}

void FillWall()
{
	FgCol(COLBRICK);
	printf("::::::::::::::::::::::::::");
	ResetAttr();
}

void FillSpace()
{
	printf("                            ");
}

void gotogxy(int x,int y)
{
	printf("\033[%d;%dH",y+1,x+1); // Console sequence for setting cursor to X,Y position.
}

void Level(int MazeNumber)
{
	int k;
	char *LevelName; // Pointer to the Level name string.

	ShuffleColors();

	ClearScreen();
	switch (MazeNumber)
		{
		case 1:
			LevelName="Humble Origins";uWidth=7;uHeight=2;lx=rx=1;
			MazeLine[1]=" #  #  ";
			MazeLine[2]=" #  ## ";
			break;

		case 2:
			LevelName="Easy Does It";uWidth=8;uHeight=lx=3;rx=2;
			MazeLine[1]="  #  ###";
			MazeLine[2]="  # # # ";
			MazeLine[3]=" # # #  ";
			break;

		case 3:
			LevelName="Up,  Up and Away";uWidth=5;uHeight=lx=11;rx=1;
			MazeLine[1]= "  #  ";
			MazeLine[2]= " # ##";
			MazeLine[3]= " ##  ";
			MazeLine[4]= "# #  ";
			MazeLine[5]= " # # ";
			MazeLine[6]= " ### ";
			MazeLine[7]= "# #  ";
			MazeLine[8]= " # # ";
			MazeLine[9]= " # # ";
			MazeLine[10]="# ## ";
			MazeLine[11]=" #   ";
			break;

		case 4:
			LevelName="Little  Harder";uWidth=5;uHeight=4;lx=2;rx=1;
			MazeLine[1]=" # # ";
			MazeLine[2]="  # #";
			MazeLine[3]=" #   ";
			MazeLine[4]=" ## #";
			break;

		case 5:
			LevelName="To and Fro";uWidth=13;uHeight=6;lx=rx=1;
			MazeLine[1]="   #####     ";
			MazeLine[2]="# #####  ### ";
			MazeLine[3]=" # ### ####  ";
			MazeLine[4]="# #  ####### ";
			MazeLine[5]=" ### #   ##  ";
			MazeLine[6]="# # # ##  #  ";
			break;

		case 6:
			LevelName="Loop-de-Loop";uWidth=14;uHeight=4;lx=2;rx=4;
			MazeLine[1]=" ##### ## ##  ";
			MazeLine[2]="   ##  ##  ## ";
			MazeLine[3]="##  # ## ###  ";
			MazeLine[4]="   ########  #";
			break;

		case 7:
			LevelName="Somehow Easy";uWidth=5;uHeight=rx=3;lx=1;
			MazeLine[1]="  ## ";
			MazeLine[2]=" #   ";
			MazeLine[3]=" # ##";
			break;

		case 8:
			LevelName="Be Prepared";uWidth=7;uHeight=6;lx=5;rx=3;
			MazeLine[1]="   #   ";
			MazeLine[2]=" ####  ";
			MazeLine[3]=" ### ##";
			MazeLine[4]=" # # # ";
			MazeLine[5]=" # ##  ";
			MazeLine[6]="# ##   ";
			break;

		case 9:
			LevelName="Two  Front Doors";uWidth=16;uHeight=rx=7;lx=1;
			MazeLine[1]="       #######  ";
			MazeLine[2]="  #### #### # # ";
			MazeLine[3]="## ## ###### # #";
			MazeLine[4]="##     # #      ";
			MazeLine[5]="  ############# ";
			MazeLine[6]=" # ##    # ###  ";
			MazeLine[7]="  #   ###     ##";
			break;

		case 10:
			LevelName="Through, through";uWidth=15;uHeight=4;lx=3;rx=1;
			MazeLine[1]=" ####  ####  ##";
			MazeLine[2]=" # ## ## # ##  ";
			MazeLine[3]=" # ## #### ##  ";
			MazeLine[4]=" # ##  ####  # ";
			break;

		case 11:
			LevelName="Double Cross";uWidth=9;uHeight=lx=7;rx=3;
			MazeLine[1]=" #  #### ";
			MazeLine[2]=" #  # ## ";
			MazeLine[3]=" # #### #";
			MazeLine[4]="# ##  #  ";
			MazeLine[5]="  #   ###";
			MazeLine[6]=" ######  ";
			MazeLine[7]="  #      ";
			break;

		case 12:
			LevelName="Inside Out";uWidth=14;uHeight=10;lx=8;rx=1;
			MazeLine[1]= "            # ";
			MazeLine[2]= " ##########  #";
			MazeLine[3]= " ###       ## ";
			MazeLine[4]= " # ######## # ";
			MazeLine[5]= " ### ###  ### ";
			MazeLine[6]= " ###   #  ### ";
			MazeLine[7]= " # ####### ## ";
			MazeLine[8]= " # # #    ### ";
			MazeLine[9]= " ############ ";
			MazeLine[10]="              ";
			break;

		case 13:
			LevelName="Hidden corridor ";uWidth=12;uHeight=10;lx=4;rx=1;
			MazeLine[1]= " ## # # ### ";
			MazeLine[2]= " #   ##   # ";
			MazeLine[3]= "  ##  #  ## ";
			MazeLine[4]= " ########   ";
			MazeLine[5]= " #    # ##  ";
			MazeLine[6]= "# ## # # #  ";
			MazeLine[7]= " ####### # #";
			MazeLine[8]= "  ###### #  ";
			MazeLine[9]= " # # # # ## ";
			MazeLine[10]="# # # # #   ";
			break;

		case 14:
			LevelName="First Principles";uWidth=5;uHeight=2;lx=2;rx=1;
			MazeLine[1]=" # # ";
			MazeLine[2]=" #   ";
			break;

		case 15:
			LevelName="Baby Steps";uWidth=7;uHeight=3;lx=1;rx=2;
			MazeLine[1]=" ##  ##";
			MazeLine[2]=" # # # ";
			MazeLine[3]=" ## #  ";
			break;

		case 16:
			LevelName="Conway's Game Of Death";uWidth=14;uHeight=12;lx=12;rx=3;
			MazeLine[1]= "  ##      ## #";
			MazeLine[2]= "   ##   ##   #";
			MazeLine[3]= "#  # # # #  # ";
			MazeLine[4]= "### ## ## ####";
			MazeLine[5]= " # # # # # #  ";
			MazeLine[6]= "  ###   ###   ";
			MazeLine[7]= "  ###   ###   ";
			MazeLine[8]= " # # # # # #  ";
			MazeLine[9]= "### ## ## ### ";
			MazeLine[10]="#  # ### #  # ";
			MazeLine[11]="   ##   ##    ";
			MazeLine[12]="  ##      ##  ";
			break;

		case 17:
			LevelName="Opening Gambit";uWidth=7;uHeight=4;lx=2;rx=3;
			MazeLine[1]="  # #  ";
			MazeLine[2]=" ## ## ";
			MazeLine[3]=" # # # ";
			MazeLine[4]="# ###  ";
			break;

		case 18:
			LevelName="Collatz";uWidth=17;uHeight=5;lx=4;rx=5;
			MazeLine[1]="### #   #  #  #  ";
			MazeLine[2]="  # ##  #  # ##  ";
			MazeLine[3]="### # # # ### #  ";
			MazeLine[4]="  # #  ##  #  #  ";
			MazeLine[5]="### #   #  # ### ";
			break;

		case 19:
			LevelName="Fishcake";uWidth=10;uHeight=7;lx=2;rx=7;
			MazeLine[1]=" ## ### # ";
			MazeLine[2]=" ##  # #  ";
			MazeLine[3]="# #####   ";
			MazeLine[4]="  # ###  #";
			MazeLine[5]="# # # # # ";
			MazeLine[6]=" # ##  ## ";
			MazeLine[7]="  #####  #";
			break;

		case 20:
			LevelName="Ego";uWidth=12;uHeight=5;lx=5;rx=5;
			MazeLine[1]=" #  # #  #  ";
			MazeLine[2]=" # #  #  ## ";
			MazeLine[3]=" ##   #  # #";
			MazeLine[4]=" # #   ##  #";
			MazeLine[5]=" #  #  ##  #";
			break;

		case 21:
			LevelName="Little Rascal";uWidth=5;uHeight=4;lx=4;rx=4;
			MazeLine[1]=" #   ";
			MazeLine[2]=" # ##";
			MazeLine[3]=" # # ";
			MazeLine[4]=" #  #";
			break;

		case 22:
			LevelName="Leap Frog";uWidth=7;uHeight=4;lx=1;rx=4;
			MazeLine[1]=" # # # ";
			MazeLine[2]="# ###  ";
			MazeLine[3]=" # # ##";
			MazeLine[4]=" #  #  ";
			break;

		case 23:
			LevelName="Shuttles";uWidth=7;uHeight=5;lx=1;rx=5;
			MazeLine[1]="  ### #";
			MazeLine[2]="  # #  ";
			MazeLine[3]="  # ###";
			MazeLine[4]="# # # #";
			MazeLine[5]="  #  # ";
			break;

		case 24:
			LevelName="The Nudge";uWidth=8;uHeight=3;lx=3;rx=3;
			MazeLine[1]="# #     ";
			MazeLine[2]="  # # ##";
			MazeLine[3]=" ## ### ";
			break;

		case 25:
			LevelName="A Dog On A Plinth";uWidth=8;uHeight=9;lx=9;rx=9;
			MazeLine[1]=" ##    #";
			MazeLine[2]=" ##     ";
			MazeLine[3]="   ### #";
			MazeLine[4]="   #### ";
			MazeLine[5]="   #### ";
			MazeLine[6]="    # ##";
			MazeLine[7]="    #  #";
			MazeLine[8]="    #  #";
			MazeLine[9]="   #### ";
			break;

		case 26:
			LevelName="The Pit";uWidth=5;uHeight=10;lx=1;rx=1;
			MazeLine[1]= " # # ";
			MazeLine[2]= "# #  ";
			MazeLine[3]= " # ##";
			MazeLine[4]= "  ## ";
			MazeLine[5]= " # # ";
			MazeLine[6]= "  ## ";
			MazeLine[7]= "# ## ";
			MazeLine[8]= " #  #";
			MazeLine[9]= "  # #";
			MazeLine[10]="  ## ";
			break;

		case 27:
			LevelName="Duck Commander";uWidth=7;uHeight=8;lx=8;rx=1;
			MazeLine[1]="  ##   ";
			MazeLine[2]="  # ###";
			MazeLine[3]="# #### ";
			MazeLine[4]="# #  # ";
			MazeLine[5]="   ##  ";
			MazeLine[6]="# # #  ";
			MazeLine[7]=" # ## #";
			MazeLine[8]="  #  # ";
			break;

		case 28:
			LevelName="Nudge-nudge";uWidth=7;uHeight=4;lx=2;rx=2;
			MazeLine[1]=" ## ## ";
			MazeLine[2]=" # # # ";
			MazeLine[3]=" # ### ";
			MazeLine[4]="   ##  ";
			break;

		case 29:
			LevelName="Ragged Rascle";uWidth=6;uHeight=5;lx=4;rx=2;
			MazeLine[1]=" #    ";
			MazeLine[2]=" ##  #";
			MazeLine[3]=" ## # ";
			MazeLine[4]=" # ###";
			MazeLine[5]="  #   ";
			break;

		case 30:
			LevelName="Knot Theory";uWidth=6;uHeight=6;lx=1;rx=6;
			MazeLine[1]="  # # ";
			MazeLine[2]="# # # ";
			MazeLine[3]="  # # ";
			MazeLine[4]="# ##  ";
			MazeLine[5]="# ##  ";
			MazeLine[6]="  # ##";
			break;

		case 31:
			LevelName="Rack and Pinion";uWidth=10;uHeight=4;lx=1;rx=3;
			MazeLine[1]="  ## # #  ";
			MazeLine[2]="### # ##  ";
			MazeLine[3]=" ##  # # #";
			MazeLine[4]="## ### #  ";
			break;

		case 32:
			LevelName="Blue Meanie";uWidth=7;uHeight=4;lx=1;rx=4;
			MazeLine[1]=" ## #  ";
			MazeLine[2]=" #  # #";
			MazeLine[3]="# ###  ";
			MazeLine[4]="  #  # ";
			break;

		case 33:
			LevelName="Great Things";uWidth=7;uHeight=4;lx=3;rx=4;
			MazeLine[1]=" # #   ";
			MazeLine[2]=" ## ## ";
			MazeLine[3]="  # # #";
			MazeLine[4]=" #  ###";
			break;

		case 34:
			LevelName="Just Enough";uWidth=8;uHeight=5;lx=3;rx=3;
			MazeLine[1]="# # ##  ";
			MazeLine[2]="## # ## ";
			MazeLine[3]="   # # #";
			MazeLine[4]="# ####  ";
			MazeLine[5]="   # #  ";
			break;

		case 35:
			LevelName="The Secret Staircase";uWidth=11;uHeight=4;lx=1;rx=3;
			MazeLine[1]=" ### ##    ";
			MazeLine[2]=" ##  ## # #";
			MazeLine[3]="#  #### ## ";
			MazeLine[4]="# ##       ";
			break;

		case 36:
			LevelName="Clear The Way";uWidth=8;uHeight=5;lx=4;rx=2;
			MazeLine[1]="  #  ###";
			MazeLine[2]="   ##  #";
			MazeLine[3]=" # ### #";
			MazeLine[4]=" # # ## ";
			MazeLine[5]=" #  # ##";
			break;

		case 37:
			LevelName="A Mismatch Made In Heaven";uWidth=9;uHeight=4;lx=4;rx=1;
			MazeLine[1]=" #   #   ";
			MazeLine[2]="## ####  ";
			MazeLine[3]="### ##   ";
			MazeLine[4]=" # #     ";
			break;

		case 38:
			LevelName="Zen Garden";uWidth=7;uHeight=6;lx=1;rx=1;
			MazeLine[1]=" #  ## ";
			MazeLine[2]=" # ##  ";
			MazeLine[3]="#### # ";
			MazeLine[4]="   # ##";
			MazeLine[5]="# #### ";
			MazeLine[6]="       ";
			break;

		case 39:
			LevelName="The Wumpus Room";uWidth=18;uHeight=5;lx=1;rx=5;
			MazeLine[1]="  #### ### ### #  ";
			MazeLine[2]="## # #  ### ### # ";
			MazeLine[3]=" ## ## #### ####  ";
			MazeLine[4]="#### ## ###  ## # ";
			MazeLine[5]="  # #   ## # ##  #";
			break;

		case 40:
			LevelName="Double Decker";uWidth=9;uHeight=7;lx=2;rx=5;
			MazeLine[1]=" #  ##   ";
			MazeLine[2]=" ## # # #";
			MazeLine[3]="# # ##   ";
			MazeLine[4]=" ########";
			MazeLine[5]="  # #### ";
			MazeLine[6]="# ##  #  ";
			MazeLine[7]=" # ###   ";
			break;

		case 41:
			LevelName="Back to Front";uWidth=11;uHeight=10;lx=1;rx=10;
			MazeLine[1]= "           ";
			MazeLine[2]= "########## ";
			MazeLine[3]= "### ####   ";
			MazeLine[4]= "# # ## #   ";
			MazeLine[5]= "    # #### ";
			MazeLine[6]= "##### ##   ";
			MazeLine[7]= " ### ## ## ";
			MazeLine[8]= "    ##  ###";
			MazeLine[9]= " ##########";
			MazeLine[10]="           ";
			break;

		case 42:
			LevelName="The Beast";uWidth=10;uHeight=7;lx=1;rx=1;
			MazeLine[1]=" # ## #   ";
			MazeLine[2]="# # ### ##";
			MazeLine[3]="  # # #   ";
			MazeLine[4]=" ## ## ###";
			MazeLine[5]="  # # # # ";
			MazeLine[6]=" ## # ### ";
			MazeLine[7]="  # ### # ";
			break;

		default:
			ClearScreen();
			gotogxy(9,7);
			printf("Congratulations!");
			gotogxy(2,8);
			printf("You've completed the MazezaM");
			gotogxy(12,10);
			InverseOn();
			printf("Well Done!");
			InverseOff();
			bCloseFlag=TRUE;
			getkey();
			break;
		}

	if (bCloseFlag) return;

	for (i=1;i<uHeight+1;i++) strcpy(&Mazezam[i*uWidth],MazeLine[i]); // Copy the Level Data.

	l=((80-uWidth)/2);
	t=((22-uHeight)/2);
	r=80-l-uWidth;
	FgCol(COLBRICK); printf(":::"); ResetAttr();
	printf("  Level ");
	if (MazeNumber<10) printf("0%d  ",MazeNumber);
	else printf("%d  ",MazeNumber);
	FgCol(COLBRICK); printf("::::::::::::::::::::::::::::::::::::::::::::::::::"); ResetAttr();
	printf("  Lives ");
	if (uLives<10) printf("0%d",uLives);
	else printf("%d",uLives);
	FgCol(COLBRICK); printf("  "); printf(":::"); PrintEnter(); ResetAttr();


	for (i=1;i!=t+1;i++) FillRow();

	for (i=1;i!=uHeight+1;i++)
	{
		if (i==lx) for (k=0;k!=l;k++) { FgCol(COLWAY);   printf("_"); ResetAttr(); }
		if (i!=lx) for (k=0;k!=l;k++) { FgCol(COLBRICK); printf(":"); ResetAttr(); }
		ColorLine(i); printf(MazeLine[i]); ResetAttr(); // Print the Level line.
		if (i==rx) for (k=0;k!=r;k++) { FgCol(COLWAY);   printf("_"); ResetAttr(); }
		if (i!=rx) for (k=0;k!=r;k++) { FgCol(COLBRICK); printf(":"); ResetAttr(); }
		PrintEnter();
	}

	FgCol(COLBRICK); for (i=t+uHeight+1;i!=22;i++) FillRow(); ResetAttr();
	j=strlen(LevelName);
	FgCol(COLBRICK); for (k=0;k!=((78-j)/2);k++) printf(":"); ResetAttr();
	InverseOn();
	printf(" "); printf(LevelName); printf(" ");
	InverseOff();
	FgCol(COLBRICK); for (k=0;k!=((78-j)/2);k++) printf(":"); ResetAttr();
	PrintEnter();

	for (i=0;i!=l;i++)
	{
		gotogxy(i+1,t+lx);
		PrintPlayer(); //Start animation
		gotogxy(i,t+lx);
		FgCol(COLWAY);
		printf("_"); // Fill after
	}

	gotogxy(l-1,t+lx);
	FgCol(COLDOOR); printf("O");
	PrintPlayer();
	i=lx;
	j=1;
}

void Title()
{
	int seed; // for random seed initialization

	ClearScreen();
	gotogxy(0,2);
	FillRow();
	FillWall(); BgCol(4); FgCol(13);
	printf("        M"); FgCol(10); printf(" a"); FgCol(11); printf(" z");
	FgCol(9); printf(" e"); FgCol(11); printf(" z"); FgCol(10); printf(" a");
	FgCol(13); printf(" M       "); ResetAttr();
	FillWall(); PrintEnter();
    FillRow();
	FillWall(); FillSpace(); FillWall(); PrintEnter();
	FillWall(); printf(" CPM version (C) 2004-08 by "); FillWall(); PrintEnter();
	FillWall(); printf("     Ventzislav Tzvetkov    "); FillWall(); PrintEnter();
	FillWall(); printf("  VT100 color  (C) 2020 by  "); FillWall(); PrintEnter();
	FillWall(); printf("     Anna Christina Nass    "); FillWall(); PrintEnter();
	FillWall(); FillSpace(); FillWall(); PrintEnter();
	FillWall(); FillSpace(); FillWall(); PrintEnter();
    FillRow();
	PrintEnter();
	PrintEnter();	
	printf("  Keys:    8 ^         r - retry level\n");
	printf("             |         q - quit\n");
	printf("        4 <--+--> 6  \n");
	printf("             |\n");
	printf("             v 2       Press SPACE to start\n");
    printf(" (or use WASD keys)\n");

	uLives=3;
	Column=40;
	for (;;)
	{
		gotogxy(Column,11);
		PrintPlayer();
		PressedKey=getk();
		seed++;

		if (PressedKey==' ') break;
		if (PressedKey=='Q' || PressedKey=='q')
			{
			bCloseFlag=TRUE;
			break;
			}
		if (PressedKey=='6' || PressedKey=='d' || PressedKey=='D')
			{
			gotogxy(Column,11);
			Column+=(Column<53);
			printf(" ");
			}
		if (PressedKey=='4' || PressedKey=='a' || PressedKey=='A')
			{
			gotogxy(Column,11);
			Column-=(Column>26);
			printf(" ");
			}
 	}
	srand(seed);
}


int main()
{
	int Mazeno,Loop;

	printf("\033[?25l"); // Hide Cursor escape sequence

	for (;;)
	{
		Title();
		Mazeno=1;
		if (bCloseFlag) break;
		Level(Mazeno); 

		for(;;)
		{
			PressedKey=getkey();

			// restart level
			if (PressedKey=='R' || PressedKey=='r')
			{
				uLives--;
				if (uLives<1) PressedKey='q';
		 		else
				{
				gotogxy(l+j-3,t+i-1);
				InverseOn();
				printf(ARGH[rand()%9]);
				InverseOff();
				getkey();
				Level(Mazeno);
				}
			}

			// quit
			if (PressedKey=='Q' || PressedKey=='q')
			{
				gotogxy(35,10);
				InverseOn();
				printf("GAME OVER!");
				InverseOff();
				getkey();
				bCloseFlag=TRUE;
				break;
   			}

			// right - level cleared
			if ((PressedKey=='6' || PressedKey=='D' || PressedKey=='d') && (i==rx && j==uWidth))
			{
				gotogxy(l+j-1,t+i);
				printf(" ");
				for (i=l+uWidth;i!=80;i++)
				{
					gotogxy(i,t+rx);
					PrintPlayer();
					gotogxy(i,t+rx);
					FgCol(COLWAY); printf("_"); ResetAttr();
				}
				InverseOn();
				i=t+rx;
				switch (rand()%6)
				{
				case 0:
					gotogxy(13,i);
					printf("Hurray!");
					break;

				case 1:
					gotogxy(13,i);
					printf("Hurrah!");
					break;

				case 2:
					gotogxy(16,i);
					printf("Yes!");
					break;

				case 3:
					gotogxy(14,i);
					printf("Great!");
					break;

				case 4:
					gotogxy(12,i);
					printf("Yee-hah!");
					break;

				default:
					gotogxy(16,i);
					printf("Yay!");
				}
				InverseOff();
				getkey();
				uLives++;
				Level(++Mazeno);
				PressedKey=0;
			}

			// right
			if ((PressedKey=='6' || PressedKey=='D' || PressedKey=='d') && j<uWidth)
				if ((Mazezam[i*uWidth+j])==' ')
				{
					gotogxy(l+j-1,t+i);
					printf(" ");
					j++;
					gotogxy(l+j-1,t+i);
					PrintPlayer();
				} 
				else
					if (Mazezam[(i*uWidth)+uWidth-1]==' ')
					{
						j++;
						for (Loop=uWidth-1;Loop>0;Loop--)
							Mazezam[(i*uWidth)+Loop]=Mazezam[(i*uWidth)+Loop-1];
						Mazezam[i*uWidth]=' ';
						gotogxy(l,t+i);
						for (Loop=0;Loop<uWidth;Loop++)
						{
							ColorLine(i);
							putchar(Mazezam[i*uWidth+Loop]);
							ResetAttr();
						}
						gotogxy(l+j-1,t+i);
						PrintPlayer();
					}

			// left
			if ((PressedKey=='4' || PressedKey=='A' || PressedKey=='a') && j>1)
				if (Mazezam[(i*uWidth)+j-2]==' ')
				{
					gotogxy(l+j-1,t+i);
					printf(" ");
					j--;
					gotogxy(l+j-1,t+i);
					PrintPlayer();
				}
				else
					if (Mazezam[i*uWidth]==' ')
					{
						j--;
						for (Loop=1;Loop!=uWidth;Loop++)
							Mazezam[(i*uWidth)+Loop-1]=Mazezam[(i*uWidth)+Loop];
						Mazezam[(i*uWidth)+uWidth-1]=' ';
						gotogxy(l,t+i);
						for (Loop=0;Loop<uWidth;Loop++) 
						{
							ColorLine(i);
							putchar(Mazezam[i*uWidth+Loop]);
							ResetAttr();
						}
						gotogxy(l+j-1,t+i);
						PrintPlayer();
					}

			// down
			if ((PressedKey=='2' || PressedKey=='S' || PressedKey=='s') && i<uHeight)
				if (Mazezam[((i+1)*uWidth)+j-1]==' ')
				{
					gotogxy(l+j-1,t+i);
					printf(" ");
					i++;
					gotogxy(l+j-1,t+i);
					PrintPlayer();
				}

			// up
			if ((PressedKey=='8' || PressedKey=='W' || PressedKey=='w') && i>1)
				if (Mazezam[((i-1)*uWidth)+j-1]==' ')
				{
					gotogxy(l+j-1,t+i);
					printf(" ");
					i--;
					gotogxy(l+j-1,t+i);
					PrintPlayer();
				}

			PressedKey=0;
			if (bCloseFlag) break;
		}
		if (bCloseFlag) break;
	}
	ClearScreen();

	printf("\033[?25h"); // Show Cursor escape sequence
	return 0;
}
