Class PuzzleModel

java.lang.Object
  extended by PuzzleModel

public class PuzzleModel
extends java.lang.Object

Yuriy Kozlov
PuzzleModel.java -- Final Project

There is a virtual environment where the chain of cubes is. The chain is represented by a linked list of the coordinates [x,y,z] of the cubes, where x points down, y points to the right, and z points into the screen.
This class is used to solve the puzzle.


Constructor Summary
PuzzleModel()
          Creates a new PuzzleModel, with an empty to-try queue, empty process, blank puzzle state, empty tried hashset, 0 size, and a blank result.
PuzzleModel(PuzzleState state)
          Creates a new PuzzleModel with the given PuzzleState, a to-try queue containing it, an empty process, an empty tried hashset, the size of the given state, and a blank result.
 
Method Summary
 java.util.LinkedList bruteSolve()
          Recursive brute solve.
 boolean checkTooLong()
          Checks if there are any straight rows of cubes that are longer than the size of the puzzle.
 int countBends()
          Counts the number of cubes that are corners.
 int countStraights()
          Counts the number of cubes that go straight through (are not corners).
 PuzzleState getCurrent()
          Returns the current PuzzleState.
 java.lang.String getResult()
          Returns the result, a string which says whether the puzzle was solved.
static void main(java.lang.String[] args)
          Called when the file is run.
static void printState2d(PuzzleState state)
          This prints out the state to the screen by printing the z coordinate of the visible cubes in the XY plane.
static void printState2d2(PuzzleState state)
          This prints out the state to the screen by printing the z coordinate of the visible cubes in the XZ plane.
 java.util.LinkedList puzzleNotSolved()
          Called when the puzzle cannot be solved.
 java.util.LinkedList puzzleSolved(PuzzleState solved)
          Called when the puzzle has been solved.
 java.util.LinkedList smartSolve()
          Does some checks to see if the puzzle is unsolvable before calling bruteSolve.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PuzzleModel

public PuzzleModel()
Creates a new PuzzleModel, with an empty to-try queue, empty process, blank puzzle state, empty tried hashset, 0 size, and a blank result.


PuzzleModel

public PuzzleModel(PuzzleState state)
Creates a new PuzzleModel with the given PuzzleState, a to-try queue containing it, an empty process, an empty tried hashset, the size of the given state, and a blank result.

Parameters:
state - A starting PuzzleState.
Method Detail

getCurrent

public PuzzleState getCurrent()
Returns the current PuzzleState.

Returns:
Returns the current PuzzleState.

getResult

public java.lang.String getResult()
Returns the result, a string which says whether the puzzle was solved.

Returns:
Returns the result.

bruteSolve

public java.util.LinkedList bruteSolve()
Recursive brute solve. Create a LinkedList Q containing the initial start State Create an empty HashSet H Repeat the following operations until a solution is found: 1) take an element x from front of the LinkedList Q 2) generate the set of all next moves from x (L = x.nextMoves();) 3) for each y in L do the following a) if y is a solution, then you are done!! call some procedure to finish up and then return b) if y is in the HashSet H, then do nothing with y (as it has already been processed) c) otherwise, add y to the HashSet H, and also add it to the end of the LinkedList

Returns:
Returns a linked list of the steps used to solve the puzzle.

smartSolve

public java.util.LinkedList smartSolve()
Does some checks to see if the puzzle is unsolvable before calling bruteSolve.

Returns:
Returns a linked list of the steps used to solve the puzzle.

checkTooLong

public boolean checkTooLong()
Checks if there are any straight rows of cubes that are longer than the size of the puzzle. A state like this cannot be solved.

Returns:
Returns true if there is a section of the chain that is too long for the puzzle size, false otherwise.

countStraights

public int countStraights()
Counts the number of cubes that go straight through (are not corners). Ends are not included.

Returns:
Returns the number of cubes that go straight through.

countBends

public int countBends()
Counts the number of cubes that are corners. Ends are not included.

Returns:
Returns the number of cubes that are a bend in the chain.

puzzleSolved

public java.util.LinkedList puzzleSolved(PuzzleState solved)
Called when the puzzle has been solved. Returns the process used to solve the puzzle, and sets the result to say it was solved.

Parameters:
solved - The solved state.
Returns:
Returns a linked list of the steps used to solve the puzzle.

puzzleNotSolved

public java.util.LinkedList puzzleNotSolved()
Called when the puzzle cannot be solved.

Returns:
Returns a null linkedlist.

printState2d

public static void printState2d(PuzzleState state)
This prints out the state to the screen by printing the z coordinate of the visible cubes in the XY plane.

Parameters:
state - A state to print out.

printState2d2

public static void printState2d2(PuzzleState state)
This prints out the state to the screen by printing the z coordinate of the visible cubes in the XZ plane.

Parameters:
state - A state to print out.

main

public static void main(java.lang.String[] args)
Called when the file is run. Used for testing.

Parameters:
args - Not used.