
CMSC 330, Spring 2012Organization of Programming LanguagesProject 3  Sliding Puzzle
IntroductionFor 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. Sliding PuzzleA sliding puzzle (or npuzzle) 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 2D puzzle are linearized to a 1D list so that rows are contiguous (rowmajor). 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 npuzzle 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 down and y moves to the right 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. Hint: For a couple of the functions above, you might find it useful to write a helper function mem with type 'a * 'a list > bool, such that mem (x,l) returns true if and only if x is contained in list l (false otherwise). Part 2: Higher order functionsWrite the following functions using the versions of map and/or fold provided in puzzle.ml:
Part 3: Puzzle functionsWrite the following helper functions for the puzzle solver:
Part 4: Puzzle solverWrite 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 length4 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 TipsAcademic IntegrityThe 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 syllabusplease review it at this time. 