1
0
UAHCode/CPE455/gameproject/.svn/pristine/9a/9ab75e624d15e88ed0d743153bc886d43e06d9e5.svn-base

119 lines
2.6 KiB
Plaintext
Raw Normal View History

2022-08-28 21:12:16 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
// global array for maze
char **maze;
int main(int argc, char *argv[])
{
FILE* mazeFile; /* Maze Input File */
char chold; /* Holds character for line input from file */
int row; /* Holds number of rows for input from file */
int col; /* Holds number of cols for input from file */
char shold[2000]; /* Holds lines for input from file */
int l;
int k;
int c1;
int c2;
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) )
{
switch(shold[0])
{
case 'M':
//printf ("Maze encountered\n");
fscanf(mazeFile, "%d %d", &row, &col);
// printf("%d\n", row);
// possible dynamic allocation of 2D array
// dynamic allocation of array for maze
int i = 0, j = 0;
maze = malloc(row * sizeof(char *));
for(i = 0; i < row; i++)
{
maze[i] = malloc(col * sizeof(char));
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
//fscanf(mazeFile, "%c", &chold);
fscanf(mazeFile, "%c", &maze[i][j]);
if (maze[i][j] == '\n')
{
printf ("\n");
fscanf(mazeFile, "%c", &maze[i][j]);
}
printf("%c", maze[i][j]);
}
}
break;
case 'P':
fscanf(mazeFile, "%d %d", &k, &l);
maze[k][l] = 'P';
printf("%c" , maze[k][l]);
break;
case 'Z':
fscanf(mazeFile, "%d %d", &k, &l);
maze[k][l] = 'Z';
printf("%c" , maze[k][l]);
break;
case 'T':
//case for treasure
printf ("Treasure\n");
fscanf(mazeFile, "%d %d", &k, &l);
maze[k][l] = 'T';
printf("%c" , maze[k][l]);
break;
case '\n':
printf("\n");
break;
default:
//printf ("Basic error\n");
break;
};
fgets(shold, 2, mazeFile);
if (feof(mazeFile))
{
break;
}
}
fclose(mazeFile);
for (c1 =0; c1 < row; c1++)
{
printf ("\n");
for (c2=0; c2 < col; c2++)
{
printf("%c", maze[c1][c2]);
}
}
//printf ("So this line won't print without an argument.\n");
//free the memory
int i = 0;
for(i = 0; i < row; i++)
free(maze[i]);
free(maze);
printf("\n");
return 0;
}