1
0
UAHCode/CPE455/gameproject/.svn/pristine/f6/f6c516c156e60e7945b5f0aff408753dbc5067d4.svn-base
2022-08-28 16:12:16 -05:00

125 lines
2.5 KiB
Plaintext

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE* mazeFile; /* Maze Input File */
char chold = NULL; /* Holds character for line input from 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; // global array for maze
unsigned int c1 = 0;
unsigned int c2 = 0;
// check to see if
if (argc != 2)
{
printf("An error has ocurred no input file was given.\n");
return 1;
}
mazeFile = fopen(argv[1], "r");
if (mazeFile == NULL)
{
printf("Error in opening the file.\n");
return 1;
}
fgets(shold, 2, mazeFile);
while ( feof(mazeFile) == 0 )
{
switch(shold[0])
{
case 'M':
fscanf(mazeFile, "%3d %3d", &row, &col);
// dynamic allocation of array for maze
unsigned int i = 0, j = 0;
maze = (char**)malloc(row * sizeof(unsigned int *));
for(i = (unsigned) 0; i < row; i++)
{
if (maze==NULL)
{
break;
}
maze[i] = malloc(col * sizeof(unsigned int *));
}
for (i = (unsigned) 0; i < row; i++)
{
for (j = (unsigned) 0; j < col; j++)
{
fscanf(mazeFile, "%c", &maze[i][j]);
if (maze[i][j] == '\n')
{
fscanf(mazeFile, "%c", &maze[i][j]);
}
}
}
break;
case 'P':
fscanf(mazeFile, "%3d %3d", &k, &l);
if(k>= row || l>= col){
break;
}
maze[k][l] = 'P';
break;
case 'Z':
fscanf(mazeFile, "%3d %3d", &k, &l);
if(k>= row || l>= col){
break;
}
maze[k][l] = 'Z';
break;
case 'T':
fscanf(mazeFile, "%3d %3d", &k, &l);
if(k>= row || l>= col){
break;
}
maze[k][l] = 'T';
break;
default:
break;
};
fgets(shold, 2, mazeFile);
if (feof(mazeFile) != 0)
{
break;
}
}
fclose(mazeFile);
for (c1 =(unsigned)0; c1 < row; c1++)
{
printf ("\n");
for (c2=(unsigned)0; c2 < col; c2++)
{
if (maze==NULL)
{
break;
}
printf("%c", maze[c1][c2]);
}
}
//free the memory
unsigned int i = 0;
for(i = (unsigned)0; i < row; i++)
{
free(maze[i]);
maze[i] = NULL;
}
if(maze != NULL){
free(maze);
}
printf("\n");
return 0;
}