Class PuzzleState

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.AbstractSequentialList<E>
              extended by java.util.LinkedList
                  extended by PuzzleState
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable, java.util.Collection, java.util.List, java.util.Queue

public class PuzzleState
extends java.util.LinkedList

Yuriy Kozlov
PuzzleState.java -- Final Project

This class represents one state of the puzzle. 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.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
PuzzleState()
          Default constructor.
PuzzleState(java.util.Collection c, short s)
          Constructor that takes a puzzle size and an existing set of elements.
PuzzleState(java.util.Collection c, short s, PuzzleState prev)
          Constructor that takes a puzzle size and an existing set of elements.
PuzzleState(short s)
          Constructor that takes a puzzle size.
 
Method Summary
 java.lang.Object clone()
          Overrides the clone method so that it returns a new state with the same instance fields as "this" state, but whose values can be changed without affecting the parent state.
 short depth()
          Returns the z-distance between the furthest apart cubes on the z-axis.
 boolean equals(java.lang.Object x)
          Precondition: x is a PuzzleState Returns true if x is a State object representing the same game state (i.e.
static PuzzleState generateRandom(short s)
          Generates a random state of the given size.
static PuzzleState generateSolved(short s)
          Generates a (not the, there are different possibilities) solved state of the given size
 PuzzleState getPrevious()
          Returns the previous state.
 int hashCode()
          Returns a hash number for the State object, usually obtained by combining the Hash code values of the instance fields in some complicated way (e.g.
 short height()
          Returns the y-distance between the furthest apart cubes on the y-axis.
 boolean isSolution()
          Returns true if "this" state is a solution to the puzzle.
 short[] minXY()
          Returns the lowest [x,y] coordinates in the state.
 short[] minXYZ()
          Returns the lowest [x,y,z] coordinates in the state.
 short[] minXZ()
          Returns the lowest [x,z] coordinates in the state.
static boolean myContains(java.util.List lst, short[] pos)
          For SOME reason contains() wouldn't work because short[].equals(short[]) would return false when it should be true, so it is reimplemented here using Arrays.equals
 java.util.LinkedList nextMoves()
          Returns a LinkedList of the States you can get to in one step from "this" state.
 java.util.LinkedList nextMoves2()
          Returns a LinkedList of the States you can get to in one step from "this" state.
 short puzzleSize()
          Returns the puzzle size.
 boolean rotate(short angle, short index, char dir)
          Rotates a cube, returns false if it cannot be rotated to that angle.
static short safeSize()
          Returns the SAFESIZE, an extreme limit on the size of the environment.
 void setPrevious(PuzzleState prev)
          Sets the previous field.
 short[][][] to3dArray()
          Creates a 3-dimensional array, where the "empty" slots are filled with SAFESIZE and the coordinates where the state is located are labeled by the number of the cube.
 short width()
          Returns the x-distance between the furthest apart cubes on the x-axis.
 
Methods inherited from class java.util.LinkedList
add, add, addAll, addAll, addFirst, addLast, clear, contains, element, get, getFirst, getLast, indexOf, lastIndexOf, listIterator, offer, peek, poll, remove, remove, remove, removeFirst, removeLast, set, size, toArray, toArray
 
Methods inherited from class java.util.AbstractSequentialList
iterator
 
Methods inherited from class java.util.AbstractList
listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, isEmpty, iterator, listIterator, removeAll, retainAll, subList
 

Constructor Detail

PuzzleState

public PuzzleState()
Default constructor. Constructs the linked list and sets the puzzleSize to 0.


PuzzleState

public PuzzleState(short s)
Constructor that takes a puzzle size.

Parameters:
s - The size of the puzzle.

PuzzleState

public PuzzleState(java.util.Collection c,
                   short s)
Constructor that takes a puzzle size and an existing set of elements.

Parameters:
c - A set of elements, each element being an array [x,y,z].
s - The size of the puzzle.

PuzzleState

public PuzzleState(java.util.Collection c,
                   short s,
                   PuzzleState prev)
Constructor that takes a puzzle size and an existing set of elements.

Parameters:
c - A set of elements, each element being an array [x,y,z].
s - The size of the puzzle.
prev - The state this state was made from.
Method Detail

puzzleSize

public short puzzleSize()
Returns the puzzle size. This should be the cube root of the length of the list.

Returns:
Returns the puzzle size.

safeSize

public static short safeSize()
Returns the SAFESIZE, an extreme limit on the size of the environment.

Returns:
Returns the SAFESIZE.

setPrevious

public void setPrevious(PuzzleState prev)
Sets the previous field.

Parameters:
prev - The state this state was made from.

getPrevious

public PuzzleState getPrevious()
Returns the previous state.

Returns:
Returns the state this state was made from.

minXY

public short[] minXY()
Returns the lowest [x,y] coordinates in the state.

Returns:
Returns the lowest [x,y] coordinates in the state.

minXZ

public short[] minXZ()
Returns the lowest [x,z] coordinates in the state.

Returns:
Returns the lowest [x,z] coordinates in the state.

minXYZ

public short[] minXYZ()
Returns the lowest [x,y,z] coordinates in the state.

Returns:
Returns the lowest [x,y,z] coordinates in the state.

isSolution

public boolean isSolution()
Returns true if "this" state is a solution to the puzzle. Works by checking if the distance between any two pieces of the chain are more than the size of the cube apart. If any are, this is not a solution.

Returns:
Returns true if "this" state is a solution to the puzzle.

depth

public short depth()
Returns the z-distance between the furthest apart cubes on the z-axis.

Returns:
Returns the depth of the chain.

height

public short height()
Returns the y-distance between the furthest apart cubes on the y-axis.

Returns:
Returns the height of the chain.

width

public short width()
Returns the x-distance between the furthest apart cubes on the x-axis.

Returns:
Returns the width of the chain.

clone

public java.lang.Object clone()
Overrides the clone method so that it returns a new state with the same instance fields as "this" state, but whose values can be changed without affecting the parent state.

Overrides:
clone in class java.util.LinkedList
Returns:
Returns a clone of this state.

nextMoves

public java.util.LinkedList nextMoves()
Returns a LinkedList of the States you can get to in one step from "this" state.

Returns:
Returns a LinkedList of the States you can get to in one step from "this" state.

nextMoves2

public java.util.LinkedList nextMoves2()
Returns a LinkedList of the States you can get to in one step from "this" state. Attempts to use smarter algoritmhs so that less moves are produced.

Returns:
Returns a LinkedList of the States you can get to in one step from "this" state.

rotate

public boolean rotate(short angle,
                      short index,
                      char dir)
Rotates a cube, returns false if it cannot be rotated to that angle. dir is the axis of rotation. Probably could be cleaned up to be shorter, like generateSolved(), but it is complicated enough as it is.

Parameters:
angle - The angle to rotate, must be 90, 180, or 270.
index - The index at which to start rotating. This cube is physically rotated, but is virtually unaffected as it stays in place.
dir - The axis of rotation.
Returns:
Returns true if the state was rotated, false if it cannot be.

equals

public boolean equals(java.lang.Object x)
Precondition: x is a PuzzleState Returns true if x is a State object representing the same game state (i.e. with same values)

Specified by:
equals in interface java.util.Collection
Specified by:
equals in interface java.util.List
Overrides:
equals in class java.util.AbstractList
Parameters:
x - A state to compare with.
Returns:
Returns true if the given state is equal to this state.

hashCode

public int hashCode()
Returns a hash number for the State object, usually obtained by combining the Hash code values of the instance fields in some complicated way (e.g. start with h = 0 and then apply h += 31*z.hashCode() for each Object field z of the State .... For shortegers you can use the shorteger itself as its hashCode .... Notes (actually these are for equals(): 1. relative positions 2. must account for how the chain is linked 3. rigid motions have the same hash code Data to work with for individual hashcodes: 1. x, y, z coordinates 2. previous/next links

Specified by:
hashCode in interface java.util.Collection
Specified by:
hashCode in interface java.util.List
Overrides:
hashCode in class java.util.AbstractList
Returns:
Returns a hash code for this state.

to3dArray

public short[][][] to3dArray()
Creates a 3-dimensional array, where the "empty" slots are filled with SAFESIZE and the coordinates where the state is located are labeled by the number of the cube. This is useful for prshorting.

Returns:
A 3-dimensional array representation of the state.

generateRandom

public static PuzzleState generateRandom(short s)
Generates a random state of the given size.

Parameters:
s - A puzzle size.
Returns:
Returns a random state.

generateSolved

public static PuzzleState generateSolved(short s)
Generates a (not the, there are different possibilities) solved state of the given size

Parameters:
s - A puzzle size.
Returns:
Returns a solved state.

myContains

public static boolean myContains(java.util.List lst,
                                 short[] pos)
For SOME reason contains() wouldn't work because short[].equals(short[]) would return false when it should be true, so it is reimplemented here using Arrays.equals

Parameters:
lst - A list to check if pos is in it.
pos - An array to search for.
Returns:
Returns true if pos is in the list, false otherwise.