2 Input File Specification The first part of your program wi


2

Input File Specification The first part of your program will read in possible puzzle configurations from a file and choose one of them randomly. The file format is as follows:

The first line of the input file will contain a single positive integer n, representing the number of puzzles in the file. The puzzles will be contained in the next 5n lines. In particular, each puzzle will be stored in 5 lines. The first line will contain the first row of values in the puzzle separated by spaces. The second line will contain the second row, the third line, the third row and the fourth line, the fourth row. The blank spot will be designated by the integer 0. The last line (fifth) will be a blank line.

Sample Input File 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

Output Specification At the very beginning of the program, you will prompt the user to enter in the name of the file (this has already been done for you in the puzzle_scaffold.c). Then, the program will open the file and load a puzzle into its memory. (You will do this in the loadPuzzle function.)

Once this is done, the puzzle should be displayed to the user (printPuzzle will do the work, but the call to this function is already in main) and the user should choose a tile to move. Roughly the board should print out as follows:

1 2 _ 3 5 6 7 4 9 10 11 8 13 14 15 12

Note that an underscore is to be used to denote the blank square and that internally, this is stored as 0. Prompt the user with the following question after showing them the board:

Which piece would you like to slide into the open slot? Note, answering 0 means you quit the game without winning. This will be executed from the getMove function.


3

If the user chooses a valid square, process the move and print out the board again. If they do not, print out the following message:

Sorry, that is not a valid square to slide into the open slot. No move has been executed.

If the user chooses 0, print out:

Sorry, looks like you gave up on the puzzle.

You\'ll notice that some of this has already been done for you. When the game ends, the user will get prompted with the following menu again:

1. Load a new puzzle. 2. Quit

Output Sample Two samples are provided in the files “puzzle-1.out.txt” and “puzzle-2.out.txt”


Restrictions: the program must be able to compile and run using MS Visual Studio 2015. Please use visual studio to develop your program. Your program should include a header comment with the following information: your name, assignment title, and date. Also, make sure you include comments throughout your code describing the major steps in solving the problem.

Grading Details Your program will be graded upon the following criteria: 1) Your correctness. 2) Your programming style and use of white space. Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor, will result in a significant deduction. 3) Whether or not you adhered to the given function prototypes. 4) Compatibility with MS Visual Studio 2015. If your program does not compile in this environment, you will get a sizable deduction from your grade.

Puzzle scaffold:

// Scaffold for puzzle problem
#include
#include

#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]);
int doMove(int puzzle[][PUZZLE_SIDE], int move);
void swap(int *a, int *b);
int solved(int puzzle[][PUZZLE_SIDE]);

int main() {
   
    int puzzle[PUZZLE_SIDE][PUZZLE_SIDE];
    char filename[MAX_FILE_LENGTH+1];
    int ans;
   
    srand(time(0));
   
    printf(\"Welcome to the PUZZLE game!\ \");
   
    // Get the puzzle file.
    printf(\"Enter the file storing all of the puzzle configurations.\ \");
    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.\ No move has been executed.\ \");    
            }
           
            // 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!!!\ \");
        else
            printf(\"Sorry, looks like you gave up on the puzzle.\ \");
       
        // Get their next selection.
        printf(\"Which selection would you like?\ \");
        printf(\"1. Load a new puzzle.\ \");
        printf(\"2. Quit.\ \");
        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) {
    
}

// Pre-conditions: none.
// Post-conditions: A basic menu will be prompted and the user\'s result returned.
int getMove() {
   
}

// 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]) {

}

// 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) {

}

// Pre-condition: none
// Post-condition: swaps the values in the variables pointed to by a and b.
void swap(int *a, int *b) {
   
}

// Pre-condition: puzzle stores a valid puzzle configuration.
// Post-condition: Returns 1 if puzzles is solved, 0 otherwise.
int solved(int puzzle[][PUZZLE_SIDE]) {
   
}

OUTPUT EXAMPLE 1:

Welcome to the PUZZLE game!
Enter the file storing all of the puzzle configurations.
puzzles.txt

Here is the current state of the puzzle board:

1 _ 2 3
5 6 7 4
9 10 11 8
13 14 15 12

Which piece would you like to slide into the open slot?
Note, answering 0 means you quit the game without winning.
2

Here is the current state of the puzzle board:

1 2 _ 3
5 6 7 4
9 10 11 8
13 14 15 12

Which piece would you like to slide into the open slot?
Note, answering 0 means you quit the game without winning.
3

Here is the current state of the puzzle board:

1 2 3 _
5 6 7 4
9 10 11 8
13 14 15 12

Which piece would you like to slide into the open slot?
Note, answering 0 means you quit the game without winning.
4

Here is the current state of the puzzle board:

1 2 3 4
5 6 7 _
9 10 11 8
13 14 15 12

Which piece would you like to slide into the open slot?
Note, answering 0 means you quit the game without winning.
8

Here is the current state of the puzzle board:

1 2 3 4
5 6 7 8
9 10 11 _
13 14 15 12

Which piece would you like to slide into the open slot?
Note, answering 0 means you quit the game without winning.
12

Great, you solved the puzzle!!!
Which selection would you like?
1. Load a new puzzle.
2. Quit.
2

OUTPUT EXAMPLE 2:

Solution

SOLUTION :::

#header files

#define MAX_FILE_LENG 30

#define PUZZL_SIDE 4
#define EMPTY_SLOT 0

void loadPuzzl(int puzzl[][PUZZL_SIDE], FILE *fin);
int getMove();
void printPuzzl(int puzzl[][PUZZL_SIDE]);
int doMove(int puzzl[][PUZZL_SIDE], int move);
void swap(int *c, int *d);
int solved(int puzzl[][PUZZL_SIDE]);

int main()

{

int puzzl[PUZZL_SIDE][PUZZL_SIDE];
char filename[MAX_FILE_LENG+1];
int ans;

srand(time(0));

printf(\"Welcome to the PUZZL-15 game!\ \");

///// Get puzzle file.
printf(\"Enter the file storing all of the puzzle configurations.\ \");
scanf(\"%s\", filename);

while (ans != 2)

{

FILE *fin;
fin = fopen(filename, \"r\");

///// puzzle.load
loadPuzzl(puzzl, fin);
fclose(fin);

///// start play!!!
int move;

printPuzzl(puzzl);
move = getMove();

///// Keep playing until user give up.
while (move!=0)

{

///// Execute this move,if it is okay.
int okay = doMove(puzzl, move);

////// Print a error msg for an invalid moves.
if (!okay)

{
printf(\"Oops!,this is not a valid square \");
printf(\" open slot.\ No move are executed.\ \");
}

///// Get out from the game for the winning move!!!!
else if (solved(puzzl))
break;
///// Go to get the next moves.
printPuzzl(puzzl);
move = getMove();
}

/////// Output an appropriate puzzle end msg.
if (move != 0)
printf(\"Great, you solved the puzzle!!!\ \");
else
printf(\"Sorry, looks like you gave up puzzle.\ \");

////// Getting next selection.
printf(\"Which selection would you like?\ \");
printf(\"1. Load a new puzzle.\ \");
printf(\"2. Quit.\ \");
scanf(\"%d\", &ans);
}

}
void loadPuzzl(int puzzl[][PUZZL_SIDE], FILE *fin)

{

}
int getMove()

{

}
void printPuzzl(int puzzl[][PUZZL_SIDE])

{

}

.int doMove(int puzzl[][PUZZL_SIDE], int move)

{

}

void swap(int *c, int *d)

{

}
.
int solved(int puzzl[][PUZZL_SIDE])

{

}

 2 Input File Specification The first part of your program will read in possible puzzle configurations from a file and choose one of them randomly. The file for
 2 Input File Specification The first part of your program will read in possible puzzle configurations from a file and choose one of them randomly. The file for
 2 Input File Specification The first part of your program will read in possible puzzle configurations from a file and choose one of them randomly. The file for
 2 Input File Specification The first part of your program will read in possible puzzle configurations from a file and choose one of them randomly. The file for
 2 Input File Specification The first part of your program will read in possible puzzle configurations from a file and choose one of them randomly. The file for
 2 Input File Specification The first part of your program will read in possible puzzle configurations from a file and choose one of them randomly. The file for
 2 Input File Specification The first part of your program will read in possible puzzle configurations from a file and choose one of them randomly. The file for

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site