Code:
#include<stdio.h>
#include"display.h"
void bordercheck(int *x, int *y);
int getdir(char ch, int dir);
void getxy(char ch, int dir, int *x, int *y, int *cellcount);
int crashtest(int dir, int *North, int *South, int *West, int *East);
void mcamutil(int dir, int *North, int *South, int *East, int *West, int *left, int *front, int *right);
main(){
int x = 0;
int y = 0;
int dir = NORTH;
int border;
char ch,
ch2;
int level;
int cellcount = 0;
int North,
South,
East,
West;
int left,
right,
front;
int ctest;
char yon;
printf("Please enter the level: \n");
scanf("%d", &level);
init_display(level);
while((ch = getchar()) != 'q'){
dir = getdir(ch, dir);
getxy(ch, dir, &x, &y, &cellcount);
show_cells(cellcount);
lvl0_get_walls(y, x, &North, &East, &West, &South);
put_walls(y, x, North, East, West, South);
ctest = crashtest(dir, &North, &South, &West, &East);
if(ctest == 1){
write_message("Crashed, restart(y/n)?: ", 3);
scanf("%c", &yon);
if(yon == 'y'){
x = 0;
y = 0;
}
if(yon == 'n'){
goto end;
}
}
mcamutil(dir, &North, &South, &East, &West, &left, &front, &right);
mouse_cam(left, front, right);
bordercheck(&x, &y);
show_mouse(dir, y, x);
show_position(y, x);
}
end:
return;
}
int crashtest(int dir, int *North, int *South, int *West, int *East){
int TRUE = 1;
int FALSE = 0;
if(dir == NORTH){
if(*North == 1){
return TRUE;
}
else return FALSE;
}
if(dir == SOUTH){
if(*South == 1){
return TRUE;
}
else return FALSE;
}
if(dir == EAST){
if(*East == 1){
return TRUE;
}
else return FALSE;
}
if(dir == WEST){
if(*West == 1){
return TRUE;
}
else return FALSE;
}
}
void mcamutil(int dir, int *North, int *South, int *East, int *West, int *left, int *front, int *right){
if(dir == NORTH){
*left = *West;
*front = *North;
*right = *East;
}
if(dir == EAST){
*left = *North;
*front = *East;
*right = *South;
}
if(dir == SOUTH){
*left = *East;
*right = *West;
*front = *South;
}
if(dir == WEST){
*left = *South;
*right = *North;
*front = *West;
}
}
void getxy(char ch, int dir, int *x, int *y, int *cellcount){
if(ch == 'w'){
if(dir == NORTH)
*y = *y + 1;
if(dir == SOUTH)
*y = *y - 1;
if(dir == EAST)
*x = *x + 1;
if(dir == WEST)
*x = *x - 1;
*cellcount = *cellcount + 1;
}
else
return;
}
int getdir(char ch, int dir){
if(dir == NORTH){
if(ch == 'a')
dir = WEST;
if(ch == 's')
dir = SOUTH;
if(ch == 'd')
dir = EAST;
return dir;
}
if(dir == WEST){
if(ch == 'a')
dir = SOUTH;
if(ch == 's')
dir = EAST;
if(ch == 'd')
dir = NORTH;
return dir;
}
if(dir == EAST){
if(ch == 'a')
dir = NORTH;
if(ch == 's')
dir = WEST;
if(ch == 'd')
dir = SOUTH;
return dir;
}
if(dir == SOUTH){
if(ch == 'a')
dir = EAST;
if(ch == 's')
dir = NORTH;
if(ch == 'd')
dir = WEST;
return dir;
}
}
void bordercheck(int *x, int *y){
if(*x >= 16){
*x = *x - 1;
}
if(*y >= 16){
*y = *y - 1;
}
if(*x < 0){
*x = *x + 1;
}
if(*y < 0){
*y = *y + 1;
}
}
Also, the header file for display.h is:
Code:
/* NOTE: this file is under construction *//* This file contains the prototypes for useful funcitons in the display.o library */void init_display(int level);/* This function initializes the display environment and clears the screen and draws the template maze from the local file maze.template. The parameter, level, determines the level of difficulty (see the spec). For Level 0 and 1 it also initializes the maze from the local file maze.dat. */void show_mouse(int dir, int row, int col);/* This function shows the mouse at the row and col position headed in the direction dir. Available at all levels. */void write_message(char str[], int line);/* This function writes the given message at one of the lines (0 to 10) on the bottom of the screen. The string is truncated at 64 characters. Available at all levels. */void clear_screen(void);/* This function clears the screen and shuts down the display environment Available at all levels. */void show_position(int y, int x);/* This functions displays the position under the X and Y on the right of the screen. Available at all levels. */void show_cells(int cells);/* This function displays the number of cells explored under CELLS on the right of the screen. Available at Level 1 and higher. */void show_unique_cells(int cells);/* This function displays the number of unique cells explored under UNIQUE CELLS on the right of the screen. Available at Level 2 or higher. */void mouse_cam(int left, int front, int right);/* This functions displays the mouse view of left, front and side walls under MOUSE CAM on the right side of the screen. Available at all levels. */void lvl0_get_walls(int y, int x, int *North, int *East, int *West, int *South);/* This function indirectly returns the walls (1 for present, 0 of absent) in the cell y, x from the internal maze. Avaliable only at Level 0 or 1. */void put_walls(int y, int x, int North, int East, int West, int South);/* This function shows the given North, East, West and South walls (1 for present, 0 for absent) for the cell y, x on the screen. Avaliable only at Level 1 or higher. */