473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

15 puzzle

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
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). 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
4 19564
#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() {

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

}

// 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
arnaudk
424 256MB
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
arnaudk
424 256MB
... 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
Banfa
9,065 Expert Mod 8TB
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

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

Similar topics

1
by: Developwebsites | last post by:
Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm ...
42
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
5
by: dmf1207 | last post by:
Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.