152 lines
3.6 KiB
Plaintext
152 lines
3.6 KiB
Plaintext
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <stdint.h>
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
#include <ncurses.h>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
int main(int argc, char *argv[])
|
||
|
{
|
||
|
/*FILE* mazeFile; Maze Input File */
|
||
|
unsigned int row = 0; /* Holds number of rows for input from file */
|
||
|
unsigned int col = 0; /* Holds number of cols for input from file */
|
||
|
char shold[2000]; /* Holds lines for input from file */
|
||
|
unsigned int l = 0;
|
||
|
unsigned int k = 0;
|
||
|
char **maze; /* array for maze */
|
||
|
unsigned int c1 = 0;
|
||
|
unsigned int c2 = 0;
|
||
|
unsigned int i = 0;
|
||
|
unsigned int j = 0;
|
||
|
unsigned int x = 0;
|
||
|
unsigned int y = 0;
|
||
|
WINDOW* W = 0; /* Curses Window handle */
|
||
|
|
||
|
/* check to see if argument */
|
||
|
if (argc != 2)
|
||
|
{
|
||
|
printw("An error has ocurred no input file was given.\n");
|
||
|
return 1;
|
||
|
}
|
||
|
W = fopen(argv[1], "r");
|
||
|
if (W == NULL)
|
||
|
{
|
||
|
printw("Error in opening the file.\n");
|
||
|
return 1;
|
||
|
}
|
||
|
fgets(shold, 2, W);
|
||
|
while ( feof(W) == 0 )
|
||
|
{
|
||
|
|
||
|
switch(shold[0])
|
||
|
{
|
||
|
case 'M':
|
||
|
/* read array values, dynamically allocate array */
|
||
|
wscanw(W, "%3u %3u", &row, &col);
|
||
|
maze = (char**)malloc(row * 8);
|
||
|
for(i = (unsigned) 0; i < row; i++)
|
||
|
{
|
||
|
if (maze==NULL)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
maze[i] = malloc(col * 8);
|
||
|
}
|
||
|
for (x = (unsigned) 0; x < row; x++)
|
||
|
{
|
||
|
for (j = (unsigned) 0; j < col; j++)
|
||
|
{
|
||
|
wscanw(W, "%c", &maze[x][j]);
|
||
|
if (maze[x][j] == '\n')
|
||
|
{
|
||
|
wscanw(W, "%c", &maze[x][j]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
case 'P':
|
||
|
wscanw(W, "%3u %3u", &k, &l);
|
||
|
/* check maze bounds */
|
||
|
if(k>= row || l>= col){
|
||
|
break;
|
||
|
}
|
||
|
maze[k][l] = 'P';
|
||
|
break;
|
||
|
|
||
|
case 'Z':
|
||
|
wscanw(W, "%3u %3u", &k, &l);
|
||
|
/* check maze bounds */
|
||
|
if(k>= row || l>= col){
|
||
|
break;
|
||
|
}
|
||
|
maze[k][l] = 'Z';
|
||
|
break;
|
||
|
|
||
|
case 'T':
|
||
|
wscanw(W, "%3u %3u", &k, &l);
|
||
|
/* check maze bounds */
|
||
|
if(k>= row || l>= col){
|
||
|
break;
|
||
|
}
|
||
|
maze[k][l] = 'T';
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
};
|
||
|
fgets(shold, 2, W);
|
||
|
if (feof(W) != 0)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
fclose(W);
|
||
|
/* Initialize ncurses -- you will want to use the following settings */
|
||
|
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||
|
start_color(); /* Enable use of color with ncurses */
|
||
|
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||
|
noecho(); /* Disable echo printing of chars type by user */
|
||
|
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||
|
keypad(W, true); /* Enable keypad and arrow keys */
|
||
|
curs_set(0); /* Set cursor to be invisible - 0 */
|
||
|
getmaxyx(W, row, col); /* Query terminal dimensions */
|
||
|
|
||
|
/* Define color pallete foreground-background color pairs */
|
||
|
/* PairID#, Foreground Color, Background Color */
|
||
|
/* Foreground color == Background color ==> solid color block */
|
||
|
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||
|
init_pair(2, COLOR_RED, COLOR_BLACK); /* Red text on Black background */
|
||
|
init_pair(3, COLOR_WHITE, COLOR_BLUE); /* White text on Blue background */
|
||
|
init_pair(4, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||
|
|
||
|
|
||
|
for (c1 =(unsigned)0; c1 < row; c1++)
|
||
|
{
|
||
|
printw ("\n");
|
||
|
for (c2=(unsigned)0; c2 < col; c2++)
|
||
|
{
|
||
|
if (maze==NULL)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
printw("%c", maze[c1][c2]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* free the memory */
|
||
|
for(y = (unsigned)0; y < row; y++)
|
||
|
{
|
||
|
free(maze[y]);
|
||
|
maze[y] = NULL;
|
||
|
}
|
||
|
if(maze != NULL){
|
||
|
free(maze);
|
||
|
}
|
||
|
printw("\n");
|
||
|
return 0;
|
||
|
}
|