I took your level 1 maze and altered it a bit to this:
Code:
##### ##### ##### ######### #
##### #### ##### ######## ##
##### ### ##### ####### ###
##### ## ##### ###### ####
##### # ##### ##### #####
#############################
###### ######
###### ######
###### ######
###### ######
###### ######
###### ######
###### # # ######
######F # # * # * S ######
#############################
########### ###############
########### ###############
########### ###############
########### ###############
abc# = brick/wall/cieling/floor/ground/etc.
' ' = nothing/space/walk in/through
* = block (moveable brick basically)
F = finsih
S = start/player position.
and wrote this program to show one way to transform the array data, back and forth from a 2D array, to a 1D array, and back to a zeroed out 2D array.
It uses the above data in a file named 1SG.txt
/*
SG.c Loads a maze from a file, into a 2D array, then transfers that maze
data into from the 2D array, into the 1D array, then transfers it back into
a zeroed out 2D array.
*/
Code:
#include <stdio.h>
const int Row = 20;
const int Col = 30;
/* In this program, the zero'th row is not used at all */
int main(void) {
char chr;
char ch;
int c, good, i, r;
FILE *in;
printf("\n\n\n");
if ((in = fopen("1SG.txt", "rt")) == NULL) {
fprintf(stderr, "Can't open input file - program terminating\n");
return 1;
}
char maze2d[Row][Col];
char maze1d[Row * Col];
/* load the 2D array up from the file */
printf("\n\n This is the 2D Array, loaded from the file: \n\n");
int cell = 0;
for(r = 1; r < Row; r++) {
for(c = 1; c <= Col; c++) {
if((good = fscanf(in, "%c", &ch)) == 1) {
cell++;
if(c % Col == 0) {
printf("\n");
continue;
}
maze2d[r][c] = ch;
printf("%c", ch);
}
else {
printf("\n Couldn't read the char in the file - terminating\n");
return 1;
}
}
}
fclose(in);
printf("\n\n Transfer the 2D array, into the 1D array: \n\n");
printf(" Hit Enter to Continue\n\n");
while((ch = getchar()) != '\n');
/* (569 array elements to be printed) transfer the 2D array into the 1D */
r = 1;
for(i = 1, c = 1; i < 570; i++) {
c = i % Col;
if(c == 0) {
r++;
printf("\n");
continue;
}
ch = maze2d[r][c];
maze1d[i] = ch;
printf("%c", ch);
if(ch == '9')
printf("\n");
} /* end of for i = 1...*/
printf("\n\n Finally, transfer the 1D array into the 2D array: \n");
printf(" Zeroing out the 2D array\n\n");
printf(" Reloading it from the 1D array. This is the 2D Array: \n");
for(r = 0; r < Row; r++)
for (c = 0; c < Col; c++)
maze2d[r][c] = 0;
while((chr = getchar()) != '\n');
r = 1;
for(i = 1, c = 1; i < 570; i++) {
c = i % Col;
if (c == 0) {
r++;
ch = 32;
maze1d[i] = 32; /* This will give you a blank space, as a char */
printf("\n");
continue;
}
ch = maze1d[i]; /* just for debug */
maze2d[r][c] = maze1d[i];
printf("%c", maze2d[r][c]);
} /* end of for */
printf("\n\n Program Complete - Press Enter to Exit \n");
while((chr = getchar()) != '\n');
return 0;
}
Adak