Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define DEPTH 2
#define WALL 1
#define NOWALL 0
#define MAXFLOOR 2
typedef enum { false, true } bool;
bool NORTH = true;
bool SOUTH = true;
bool EAST = true;
bool WEST = true;
bool STAIRS = false;
struct object
{
int x;
int y;
int z;
int gold;
int hp;
char name[15];
};
struct object player = {1,1,0,300,50};
void init(void); // pre-processing
void process(void); // controls game loop
void check(void); // checks available directions
void move(char *); // moves player
void panel(void);
int map[3][10][10] =
{1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , // Floor 1 (0)
1 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , // Floor 2 (1)
1 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , // Floor 3 (2)
1 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 ,
1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
int main(void)
{
init();
process();
return 0;
}
void init(void)
{
printf("Enter you character's name \n");
scanf(" %s",player.name);
system("cls");
printf("Welcome %s\n\n",player.name);
check();
}
void process()
{
char dir = '\0';
do
{
panel();
scanf(" %c",dir);
system("cls");
move(&dir);
check();
}while(dir != 'q');
}
void move(char *p_dir)
{
//select direction
switch(*p_dir)
{
// Processing for direction
case 'n':
if (NORTH == true) player.x--;
else printf("There is a wall in the way\n\n");
break;
case 's':
if (SOUTH == true) player.x++;
else printf("There is a wall in the way\n\n");
break;
case 'e':
if (EAST == true) player.y++;
else printf("There is a wall in the way\n\n");
break;
case 'w':
if (WEST == true) player.y--;
else printf("There is a wall in the way\n\n");
break;
// Processing for STAIRS
case 'u':
if (STAIRS == true)
{
if (player.z != MAXFLOOR) player.z++;
else printf("You cant go up here\n\n");
}
break;
case 'd':
if (STAIRS == true)
{
if (player.z != 0) player.z--;
else printf("You cant go down here here\n\n");
}
else printf("You cant go down here here\n\n");
break;
case 'q':
return;
}
}
void check(void)
{
//check exits
printf("Player is at %d, %d on floor %d\n\n\n",player.x, player.y, player.z );
if (map[player.z][player.x-1][player.y] == WALL) NORTH = false;
else NORTH = true;
if (map[player.z][player.x+1][player.y] == WALL) SOUTH = false;
else SOUTH = true;
if (map[player.z][player.x][player.y+1] == WALL) EAST = false;
else EAST = true;
if (map[player.z][player.x][player.y-1] == WALL) WEST = false;
else WEST = true;
if (map[player.z][player.x][player.y] == DEPTH) STAIRS = true;
else STAIRS = false;
}
void panel()
{
//print exits
printf("Exits are \n");
if (NORTH == true) printf("(N)orth ");
if (SOUTH == true) printf("(S)outh ");
if (EAST == true) printf("(E)ast ");
if (WEST == true) printf("(W)est ");
//print if STAIRS
if (STAIRS == true && player.z < MAXFLOOR) printf("(U)p ");
if (STAIRS == true && player.z > 0) printf("(D)own ");
printf("\n(Q)uit\n\n");
textcolor(DARKGRAY);
cprintf("%s's Stats ",player.name);
textcolor(WHITE);
cprintf("HP: ");
textcolor(RED);
cprintf("%d ",player.hp);
textcolor(WHITE);
cprintf("GOLD: ");
textcolor(YELLOW);
cprintf("%d ", player.gold);
}