Read .txt file into 2D array.
Ok, I am making a puzzle game. For my function loadPuzzle, I want to read in a random puzzle to a 2d array. The .txt file looks like this:
3
1 0 2 3
5 6 7 4
9 10 11 8
13 14 15 12
15 1 2 3
14 13 12 11
10 9 8 7
6 0 5 4
1 2 3 4
5 6 0 8
9 10 7 11
13 14 15 12
with the first integer being the number of puzzles and each block of 4 lines after that being a puzzle. The function looks like this:
Code:
int num_puzzles = 0;
int i = 0;
int j = 0;
fscanf(fin, "%d", &num_puzzles);
printf("There are %d puzzles\n", num_puzzles);
for(i = 4; i < 8; i++)
{
for(j = 0; j < 4; j++)
{
fscanf(fin, "%d", &puzzle[i][j]);
}
}
for(i = 4; i < 8; i++)
{
for(j = 0; j < 4; j++)
{
if(puzzle[i][j] == 0)
{
printf(" _");
}
else
{
printf("%3d", puzzle[i][j]);
}
}
printf("\n");
}
In this code I'm trying to read in the 2nd puzzle but it just won't work. It always reads in the first puzzle (or some strange behavior). I actually have to make the program select a random puzzle but I haven't gotten that far yet. Can someone help with the logic of this program?