CMSC 330, Spring 2015
Organization of Programming Languages
Project 3 - Sliding Puzzle
This version of the project now includes these corrections.
For this project you will need to implement a number of functions in OCaml that together can be used to find solutions for a sliding puzzle. This project will provide experience dealing with recursion, lists, and higher order functions, as well OCaml's type inference system.
A sliding puzzle (or n-puzzle) consists of a frame of numbered square tiles in random order with one tile missing. Tiles adjacent to the space may be moved into the space, rearranging tiles until the tiles are all in order.
For this project we represent the tiles in a sliding puzzle as numbers in a list, with 0 as the space. We'll assume all puzzles are square. For this project we'll assume a puzzle is solved when the space is in the top left corner of the puzzle, with tile numbers sorted in order from left to right and top to bottom.
Positions in the 2-D puzzle are linearized to a 1-D list so that rows are contiguous (row-major). For instance, the following solved 3x3 puzzle is linearized as [0;1;2;3;4;5;6;7;8].
In other words, the x,y coordinates of a position in the n-puzzle are assigned as follows:
Importantly: Notice that the origin (0,0) is in the upper left, and the coordinate pair (x,y) indicates the position x moves right and y moves to the down of the origin. If you don't follow this arrangement, some of your position functions below will produce the wrong answers.
Getting StartedDownload the following archive file p3.zip and extract its contents.
Along with files used to make direct submissions to the submit server (submit.jar, .submit, submit.rb), you will find the following project files:
You may use functions from testUtils.ml for printing debugging messages, but do not modify the file. Your actual submission should not print any output.
To test your utility functions and puzzle solver implementation, you can execute the public tests from the command line by typing commands like ocaml testRecursion1.ml.
The puzzle.ml file you downloaded contains a number of utility functions, and comments describing the functions you are required to implement.
Note that you must implement your functions with the exact parameter and return type specified, or else the submit server tests will fail.
In general just assume your code will be invoked only for legal inputs (though note "index (x,v)" not finding v in x and returning -1 is considered to be legal). We haven't reached how OCaml can throw exceptions yet.
For this project the only OCaml libraries you are allowed to use are those defined in the Pervasives module loaded by default. In fact, you will need at least a couple of functions found here. You are not allowed to use library functions found in any other modules, particularly List and Array.
Part 1: RecursionWrite the following recursive functions:
Hint: The OCaml comparison functions (=,<=,>=,<, and >) are polymorphic, so you can give them any two arguments of the same type.
Part 2: Higher order functionsWrite the following functions using the versions of map and/or fold provided in puzzle.ml:
Part 3: Puzzle functions
Write the following helper functions for the puzzle solver:
Part 4: Puzzle solver
Write the following function for solving a sliding puzzle. Your solution must be efficient enough to solve 4x4 puzzles under 10 moves within 3 seconds on the submit server (we will also test it on larger puzzles/solutions).
A solution to solve_board is a list of boards produced by moves starting from b until the solved board is reached. The list is in reverse order: solved board first, b last. The length l of each solution is the number of moves (i.e., one less than the length of the list that represents the solution). Solutions are not permitted to contain the same intermediate board twice. For example, [[[0;1;2;3;4;5;6;7;8];[1;0;2;3;4;5;6;7;8];[0;1;2;3;4;5;6;7;8];[1;0;2;3;4;5;6;7;8];[1;2;0;3;4;5;6;7;8]]] is not a legal length-4 solution to [1;2;0;3;4;5;6;7;8].
Hints for solve_board: as you are required to produce all solutions up to length n, you are essentially doing an exhaustive search with a bit of smarts to prune out paths containing duplicate boards. That is, at each step you will want to enumerate all possible boards produced by legal moves from the current board of each path produced by prior steps. You will prune out paths that would be produced by repeating a previous board position. You should be making good use of the functions you have already defined above. If your solution is not using many of these functions, you are doing too much work!
SubmissionYou can submit your project in two ways:
Hints and Tips
The Campus Senate has adopted a policy asking students to include the following statement on each assignment in every course: "I pledge on my honor that I have not given or received any unauthorized assistance on this assignment." Consequently your program is requested to contain this pledge in a comment near the top.
Please carefully read the academic honesty section of the course syllabus. Any evidence of impermissible cooperation on projects, use of disallowed materials or resources, or unauthorized use of computer accounts, will be submitted to the Student Honor Council, which could result in an XF for the course, or suspension or expulsion from the University. Be sure you understand what you are and what you are not permitted to do in regards to academic integrity when it comes to project assignments. These policies apply to all students, and the Student Honor Council does not consider lack of knowledge of the policies to be a defense for violating them. Full information is found in the course syllabus---please review it at this time.
This course project is copyright of Dr. Michael Hicks. ©Michael Hicks . All rights reserved. Any redistribution or reproduction of part or all of the contents in any form is prohibited without the express consent of the author.