Here let me post the whole program. You guys are really helpful and I know I'm getting close to making this work, I appreciate it.
Code:
#include <stdio.h>
#include <time.h>
#define MAX_FILE_LENGTH 30
#define PUZZLE_SIDE 4
#define EMPTY_SLOT 0
void loadPuzzle(int puzzle[][PUZZLE_SIDE], FILE *fin);
int getMove();
void printPuzzle(int puzzle[][PUZZLE_SIDE]); // Prototypes for the needed functions of the game
int doMove(int puzzle[][PUZZLE_SIDE], int move);
void swap(int *a, int *b);
int solved(int puzzle[][PUZZLE_SIDE]);
int main(void)
{
int puzzle[PUZZLE_SIDE][PUZZLE_SIDE];
char filename[MAX_FILE_LENGTH+1];
int ans;
srand(time(0));
printf("Welcome to the PUZZLE-15 game!\n");
// Get the puzzle file.
printf("Enter the file storing all of the puzzle configurations.\n");
scanf("%s", filename);
while (ans != 2) {
FILE *fin;
fin = fopen(filename, "r");
// Load the puzzle.
loadPuzzle(puzzle, fin);
fclose(fin);
// Let's play!
int move;
printPuzzle(puzzle);
move = getMove();
// Keep on playing until the user gives up.
while (move!=0) {
// Execute this move, seeing if it's okay.
int okay = doMove(puzzle, move);
// Print an error message for an invalid move.
if (!okay)
{
printf("Sorry, that is not a valid square to slide into ");
printf("the open slot.\nNo move has been executed.\n");
}
// Get out of the game for a winning move!
else if (solved(puzzle))
break;
// Go get the next move.
printPuzzle(puzzle);
move = getMove();
}
// Output an appropriate puzzle ending message.
if (move != 0)
printf("Great, you solved the puzzle!!!\n");
else
printf("Sorry, looks like you gave up on the puzzle.\n");
// Get their next selection.
printf("Which selection would you like?\n");
printf("1. Load a new puzzle.\n");
printf("2. Quit.\n");
scanf("%d", &ans);
}
}
// Pre-conditions: fin is pointed to the beginning of a file with a valid
// file format for this problem.
// Post-conditions: A random puzzle from the file pointed to by fin will be
// stored in puzzle.
void loadPuzzle(int puzzle[][PUZZLE_SIDE], FILE *fin)
{
int i = 0;
int j = 0;
int k = 0;
int puzzlenumber = 0;
int num_puzzles = 0;
fscanf(fin, "%d", &num_puzzles); // Reads the first integer from the file, the number of puzzles
printf("There are %d puzzles.\n", num_puzzles);
puzzlenumber = rand() % num_puzzles; // Sets up a random puzzle number from 0 - (num_puzzles - 1) inclusive
for(i = 0; i <= puzzlenumber; i++) // Loops through the entire file, only storing the random puzzle
{
for(j = 0; j < 4; j++)
{
for(k = 0; k < 4; k++)
{
fscanf(fin, "%d", &puzzle[j][k]);
}
}
}
}
// Pre-conditions: none.
// Post-conditions: A basic menu will be prompted and the user's result returned.
int getMove()
{
int move;
printf("Enter the number to move into the empty slot\n"); // Asks the user for the number they wish to move into the empty slot
scanf("%d", &move);
return move;
}
// Pre-conditions: A valid puzzle is stored in puzzle.
// Post-conditions: A depiction of the puzzle will be printed out.
void printPuzzle(int puzzle[][PUZZLE_SIDE])
{
int j = 0;
int k = 0;
for(j = 0; j < 4; j++)
{ // Loop through the puzzle
for(k = 0; k < 4; k++)
{
if(puzzle[j][k] == EMPTY_SLOT) // When the EMPTY_SLOT (0) is found, an underscore is printed out
{
printf(" _");
}
else
{
printf("%3d", puzzle[j][k]); // Otherwise, the remaining elements of the puzzle array are printed out
}
}
printf("\n");
}
}
// Pre-conditions: puzzle stores a valid puzzle configuration.
// Post-conditions: If move is a valid square to slide into the open slot,
// the move is executed and 1 is returned. Otherwise, 0
// is returned and no change is made to puzzle.
int doMove(int puzzle[][PUZZLE_SIDE], int move)
{
int j = 0;
int k = 0;
int a = 0;
int b = 0;
for(j = 0; j < 4; j++)
{
for(k = 0; k < 4; k++) // Loop through the puzzle
{
if(puzzle[j][k] == move) // If the position in the puzzle array is equal to the number to user wants to move, set a and b to the row
{ // and column of this position
a = j;
b = k;
}
}
}
if(puzzle[a-1][b] == 0 || puzzle[a+1][b] == 0 || puzzle[a][b-1] == 0 || puzzle[a][b+1] == 0) // If the row immediately above or below
{ // the number to move is 0 (in the same column)
swap(&a,&b); // or if the column to the immediate
return 1; // left or right of the number (in the same row)
} // is 0, the move is valid and 1 is returned.
else
{
return 0; // Otherwise the move is invalid and 0 is returned.
}
}
// Pre-condition: none
// Post-condition: swaps the values in the variables pointed to by a and b.
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
// Pre-condition: puzzle stores a valid puzzle configuration.
// Post-condition: Returns 1 if puzzles is solved, 0 otherwise.
int solved(int puzzle[][PUZZLE_SIDE])
{
if(puzzle[0][0] == 1 && puzzle[0][1] == 2 && puzzle[0][2] == 3 && puzzle[0][3] == 4
&& puzzle[1][0] == 5 && puzzle[1][1] == 6 && puzzle[1][2] == 7 && puzzle[1][3] == 8
&& puzzle[2][0] == 9 && puzzle[2][1] == 10 && puzzle[2][2] == 11 && puzzle[2][3] == 12
&& puzzle[3][0] == 13 && puzzle[3][1] == 14 && puzzle[3][2] == 15 && puzzle[3][3] == 0)
{
return 1;
}
else
{
return 0;
}
}