By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,726 Members | 1,698 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,726 IT Pros & Developers. It's quick & easy.

15 puzzle

P: 2
Problem: 15 Puzzle
This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle:

The goal is to get the tiles in order, 1 through 15, from left to right, top to bottom, by just sliding tiles into the empty square. In this configuration, the goal would be to get the 14 and 15 to switch places, without affecting any of the other squares.

Your goal will be to write a program that allows the user to play this game.

In particular, you will fill in a scaffold given to you that has descriptions of the different functions you must fill in. The functions you must fill in, along with their specifications are included in the attachment puzzle15scaffold.c.

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
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). 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.

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
Nov 6 '08 #1
Share this Question
Share on Google+
4 Replies

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

int main() {

char filename[MAX_FILE_LENGTH+1];
int ans;


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);

// Let's play!
int move;

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

// Go get the next move.
move = getMove();

// Output an appropriate puzzle ending message.
if (move != 0)
printf("Great, you solved the puzzle!!!\n");
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) {


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

Nov 6 '08 #2

P: 424
Please read the posting guidelines, paying particular attention to Help on posting coursework questions and answers. Then ask a sensible question showing that you made an attempt at the problem.
Nov 6 '08 #3

P: 424
... and your question is? I refer you again to the posting guidelines where you will see you're not supposed to post full-code solutions. Also, please use [code][/code] tags! And please don't double post!
Nov 6 '08 #4

Expert Mod 5K+
P: 8,916
Please DO use code tags [code] ... [/code] when you post code.

Please DO NOT double post your questions, I have now merged your 2 threads.

And as already suggested please do read the posting guidelines where you will find out that we don't write code for you but we will help you with the problems you are having with your own code.
Nov 6 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.