Artificial Intelligence Homework 2
Due date: 2/8 by 11:59pm


In this assignment, you will use A* to solve instances of the sliding puzzle. This is a programming assignment that should be done individually in Java.

Starting Files: Click here to download the two Java files provided for this assignment: GraphicsWindow.java and SlidingPuzzle.java (both courtesy of Professor Smith). The GraphicsWindow.java file is used to create GUIs. You can ignore this file. The SlidingPuzzle.java file contains routines for reading in puzzles from file, calling your solver method, and interacting with the GUI components. The only relevant part of this file is in the main method where your solver method is called.

For this assignment, you will create two Java classes: Solver and Node.

  • Solver Class: The Solver class should contain a single public static method solvePuzzle(). This method should take as input a 2-dimensional array of integers which represent the arrangment of the tiles on the board. The blank "tile" is represented using the integer 0. This method should return null if the puzzle cannot be solved. If the puzzle can be solved, it should return a string of directions indicating a solution. The string should be composed of the letters "U", "D", "L", and "R". Here, I am using "L" to indicate that the blank tile should be moved to the left (or, said differently, that the blank tile and the tile to its left should be swapped).

  • Node Class: Since you are implementing a search algorithm, it makes sense to create a Node class where instances of this class represent nodes in the search tree. Section 3.3 in the textbook discusses practical details for implementing a Node class and for GraphSearch.

This assignment requires you to think carefully about class design: what methods will you need, in which class should a method be placed, etc. Good object-oriented design says that you should not leak, or expose, unnecessary details about the Node class in the Solver class.

Additional Information/Hints:

  • A* requires the use of a minimum priority queue. I recommend using Java's PriorityQueue class. If you do then your Node class must implement the Comparable interface. If you use any sort of hash structure, or you need to test equality between two nodes, then it would be smart to override the equals() and hashCode() method in the Node class.

  • It might be useful to give your Node class two constructors: one that creates the node given a 2-dimensional array of tile positions, and one that creates the node from a parent node object along with some modification.

  • See https://en.wikipedia.org/wiki/15_puzzle for determining whether or not a given puzzle is actually solvable

  • Forget Wikipedia...here is a better link with a more informed answer on determining solvability

  • The goal state is always a puzzle with the numbers 1 through the maximum number arranged row-wise and the blank tile being the very last spot (i.e. the bottom rightmost spot).

Running Your Code
The SlidingPuzzle class takes care of reading puzzles from file. Puzzle files are passed in to the program via the command line (with the extension .puz). Each puzzle file contains space delimited tokens: the numbers 1 through the maximum number and a period (.) to represent the blank tile. Here's an example of a puzzle file:

1 2 3 4
. 5 6 7
9 10 11 8

The width and the height of a puzzle is arbitrary but it will always be rectangular (i.e. each row has the same number of tiles). To run your code, you should create a .puz file and pass it in to the main method of the SlidingPuzzle class via the command line.

Grading and Submission
Your assignment will be graded based upon correctness (i.e. I will run your code on various puzzle files), efficiency (i.e. the time/space complexity of your implementation), as well as its adherence to the Java style guide.

To submit, rename your directory hw2_FirstName_LastName. Then zip your directory and upload it to Moodle. Please remember to rename your directory before you zip it.


Last modified: Tue Jul 15 13:34:17 PDT 2014