CS 115 Program 4

Sliding puzzle

Design (individual) Due: Monday, July 22th by Midnight
Design (Team) Due: Tuesday, July 21th during Lab
Source Code (individual) Due: Saturday July 13th by Midnight

Educational Goals: The educational goals of this program are that the student should use the concepts of

·  design with pseudocode

·  using the objects of the graphics library

·  using while loops to repeat code

·  Using the If statement

·  Using the random library

·  Using lists and Boolean data type

·  Using functions

·  documentation

·  testing

Introduction:

(Source: the following paragraphs are taking from Wikipedia, through this link)

A sliding puzzle, sliding block puzzle, or sliding tile puzzle is a puzzle that challenges a player to slide usually flat pieces along certain routes (usually on a board) to establish a certain end-configuration.

The fifteen puzzle is the oldest type of sliding block puzzle. It was invented by Noyes Chapman and created a puzzle craze in 1880. Sam Loyd is often wrongly credited with making sliding puzzles popular based on his false claim that he invented the fifteen puzzle.

Unlike other tour puzzles, a sliding block puzzle prohibits lifting any piece off the board. This property separates sliding puzzles from rearrangement puzzles. Hence, finding moves and the paths opened up by each move within the two-dimensional confines of the board are important parts of solving sliding block puzzles.

Sliding puzzles are essentially two-dimensional in nature, even if the sliding is facilitated by mechanically interlinked pieces (like partially encaged marbles) or three-dimensional tokens. As this example shows, some sliding puzzles are mechanical puzzles. However, the mechanical fixtures are usually not essential to these puzzles; the parts could as well be tokens on a flat board that are moved according to certain rules.

This type of puzzle has been computerized (as puzzle video games) and is available to play for free on-line from many Web pages. It is a descendant of the jigsaw puzzle in that its point is to form a picture on-screen. The last square of the puzzle is then displayed automatically once the other pieces have been lined up.

Program 4 Requirements:

Your rule here is to write test case, design and implement this game. You have to come up with the test cases and the design by yourself, but you can do the implementation by yourself or with a partner.

What is required from you in this project is to simulate this game with the following rules.

1.  Using the graphical part that we covered in the class, draw a windows 800*800 rectangle.

2.  Load 15 out of 16 different pictures stored along your program code and Show them in a random way.

3.  You have to use the 2 Dimensional array that will be covered this week to simulate this game, you can use the 2 Dim array to store the names the of pictures files. We are using a 2 Dim array with size 4 * 4.

4.  To fill the 2 Dim array, you have to txt file that contains the names of the pictures file (all on one line), this is similar to the function ReadPicturesNames that you used in your program 3.

5.  You have to use the random function to choose a number between 1 and 16 and then use this number to hide one of the 16 pictures (So we can make a missing piece in the game).

6.  If the user click on a picture which is not of the direct neighbor of the missing piece, then you have to warn the user that he made a mistake, otherwise, switch the missing piece with the clicked picture. The only movable pictures are those who are the right, left, above or below neighbors of the missing pieces (No diagonal neighbors are allowed).

7.  The game is over when you organized the picture in the proper order, once you do this then show the missing piece (an example of a finished game for the picture above is the following).

You have to apply the concepts of if, while, functions, random and arrays that we have studied in the last few weeks on this program.

The functions which are required from you to design in this program are the following:

You have to write a function called DecideWhichPicture that accept two parameters (x and y –coordinators of the clicked mouse). The function’s purpose is to return back the picture’s index in the 2 Dim arrays which represents the user clicked on the screen, or returned -1 in case he/she clicked outside the pictures areas

You have to write a function called DrawPictures that accept four parameters; the first one is a string that represents the name of a picture file. The second and the third parameters are integers which represent the location where you need to center the picture, and the last one represents the GraphWin object which was previously defined in the main function. The function’s purpose is to load the picture at the specified location.

Write a function called ReadPicturesNames that receive a string parameter that represents a file name (which contains the names of all pictures files names) and it will returned back a list that contains that names of the pictures’ files names, the best way to write the files names inside that file is by writing them all on one line separated by space.

Write a function called WhatIsTheCurrentIndices that receive one parameter that represents the 2 Dim arrays and returned the indices of the picture which is missing.

Write a function called ChangePlace which accept two parameter: The first one represents the index of the clicked picture at the array, while the second one represent the current position of the missing piece, the function exchange the places of the 2 pictures in case the exchange is allowed according the rules above and returned true, while it returns False in case the exchange is not allowed.

Write a function called GameOvere which accept two parameter: The first one represents the current 2 Dim situation and the optimal 2 Dim array, and it check if the current 2 Dim arrays is the correct order of the pictures, if so, then show the missing picture and returns True, otherwise, returns False.

Test Cases

First, read the assignmentcarefully. Look for how the program is supposed to behave. You do not know what the code looks like - that is fine. The assignment gives some examples of normal runs. Come up with 10 different cases for the program as individual and submit by Monday, July 22th by Midnight using this link (Choose “ TestCases Program 4” ). We will do the team part by Tuesday July 23th.Submit by that time the file with the linkhere. Choose “ TestCase Team part Program 3”. Test cases should include but not limited the following:

What happened when the user click on a neighbors of the missing picture, either if it is valid neighbor and not?

What happened when you click on the missing card?!

What happened when you click outside the 16 pictures?

When is the game over?

Design

Make a numbered list and put it in Python file as comments. Save this Python file as "design4.py".

# supply program prolog

# import the required libraries.

# main function

# 1. prepare for graphics

# 3. Initialize the required variables.

# 4. more design here

# N. a loop that controls the game

# (your design here)

# M. Game is over and show the summary

Fill individually the missing steps in the design. There should be at least 10 steps and no more than 20 steps in the design. Submit this file with the link (Choose “ Design Program 4” ). We will do the team part by Tuesday July 23th.Submit by that time the file with the linkhere. Choose “Desing Team part Program 4”

Implement the design

Write a Python program to implement your design. Make sure you eliminate any syntax and semantics errors. Submit your individual source code with the linkhere. Choose the menu choices of "Program 4 Code". This is due Saturday July 27th midnight.

There are several specifications about how your program should be written.

·  This program uses input from the keyboard using the graphics part, and not the shell; remember also to show on the window and not the shell.

·  Use assignment statements to perform the calculations. Not output statements.

·  Youmustuse the while, 2 Dim arrays and the if statements to control the game.

·  You must implement and use all of the functions described above.

·  Obviously you must use several graphics functions.

·  Don't forget to pause the program at the end and ask the user for a click, then close the graphics window!

Please read the documentation standard on the class web page.

Remember that the programmustrun with NO errors or warnings. If it does not, it will not be graded!