org.jscience.computing.game.puzzle
Class TilePuzzle

java.lang.Object
  extended by org.jscience.computing.game.AbstractGame
      extended by org.jscience.computing.game.puzzle.TilePuzzle
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, GamePlay

public class TilePuzzle
extends AbstractGame

implements a very flexible tile puzzle game with various options

See Also:
TilePuzzleSamples, TilePuzzlePlayer, Serialized Form

Constructor Summary
TilePuzzle()
          uses TilePuzzleSamples.getNumberPuzzle(3) to initialize the puzzle
TilePuzzle(java.lang.String name, java.lang.Object[][] puzzle)
          When constructed, the puzzle's current state equals its solution.
TilePuzzle(java.lang.String name, java.lang.Object[][] scrampledPuzzle, java.lang.Object[][] destinationPuzzle)
          If you want to set the initial state different from the solution puzzle by hand, this is the constructor to do it; be careful that the conventions (see TilePuzzle (String name, Object[][] puzzle)) for the puzzle are maintained for both given object arrays; also, both arrays must have the same dimensions and should contain the same objects.
 
Method Summary
 java.lang.Object clone()
          DOCUMENT ME!
 boolean equals(java.lang.Object obj)
          DOCUMENT ME!
 java.lang.Object[][] getPuzzleMatrix()
          DOCUMENT ME!
 double getResult(int playerRole)
          DOCUMENT ME!
 java.lang.Object[][] getSolutionMatrix()
          DOCUMENT ME!
 int[] getWinner()
          DOCUMENT ME!
 int hashCode()
          rather experimental at this point; needs to be tested for efficiency
 boolean isEndWhenSolved()
          If isEndWhenSolved(), no legal moves are possible anymore once the puzzle is solved or when it has been determined that the game has no solution path.
 boolean isReverseMoveDisabled()
          If isReverseMoveDisabled(), reverting the game status back to the state it came from (examining the last move) is not a legal move.
 boolean isSolved()
          checks whether the puzzle state equals the solution and whether it is impossible to reach the goal - which is done by checking whether there are exactly 2 tiles exchanged.
protected  GameMove[] listLegalMoves()
          DOCUMENT ME!
 int nextPlayer()
          DOCUMENT ME!
protected  boolean popMove()
          DOCUMENT ME!
protected  boolean pushMove(GameMove move)
          DOCUMENT ME!
 void randomize(int numberOfMoves)
          DOCUMENT ME!
 void randomize(int numberOfMoves, long randomSeed)
          DOCUMENT ME!
 void reset()
          DOCUMENT ME!
 void setEndWhenSolved(boolean enable)
           
 void setReverseMoveDisabled(boolean enable)
           
 void shuffle()
          DOCUMENT ME!
 void shuffle(long randomSeed)
          note that calling shuffle() may make the puzzle unsolvable
 java.lang.String toString()
          DOCUMENT ME!
 
Methods inherited from class org.jscience.computing.game.AbstractGame
clearRedoList, gameOver, getGameName, getLastMove, getLastPlayer, getLegalMoves, getLegalMoves, getMoveHistory, getNumberOfRedoMoves, getRedoList, isLegalMove, isWinner, loadFromFile, makeMove, numberOfMoves, numberOfPlayers, redoMove, resetLegalMoves, resetLists, saveToFile, spawnChild, undoLastMove, undoMoves
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TilePuzzle

public TilePuzzle()
uses TilePuzzleSamples.getNumberPuzzle(3) to initialize the puzzle

See Also:
TilePuzzleSamples

TilePuzzle

public TilePuzzle(java.lang.String name,
                  java.lang.Object[][] puzzle)
When constructed, the puzzle's current state equals its solution. You can then use functions like randomize() or shuffle() to actually start the game Instead of building the puzzle array yourself, you can take advantage of the static methods of the class TilePuzzleSamples, which provides convenient standard configurations; if you build a configuration yourself, the convention for int[][] puzzle is as follows.
  • the arrays' width should be the same as its height
  • the array must contain excactly one null value
  • The String name is just used as an identification

    See Also:
    TilePuzzleSamples

    TilePuzzle

    public TilePuzzle(java.lang.String name,
                      java.lang.Object[][] scrampledPuzzle,
                      java.lang.Object[][] destinationPuzzle)
    If you want to set the initial state different from the solution puzzle by hand, this is the constructor to do it; be careful that the conventions (see TilePuzzle (String name, Object[][] puzzle)) for the puzzle are maintained for both given object arrays; also, both arrays must have the same dimensions and should contain the same objects. This constructor does not check whether the puzzle is in fact solvable.

    See Also:
    TilePuzzleSamples
    Method Detail

    reset

    public void reset()
    DOCUMENT ME!


    isSolved

    public boolean isSolved()
                     throws PuzzleNotSolvableException
    checks whether the puzzle state equals the solution and whether it is impossible to reach the goal - which is done by checking whether there are exactly 2 tiles exchanged.

    Returns:
    DOCUMENT ME!
    Throws:
    PuzzleNotSolvableException - if the puzzle state cannot reach the solution

    shuffle

    public void shuffle()
    DOCUMENT ME!


    shuffle

    public void shuffle(long randomSeed)
    note that calling shuffle() may make the puzzle unsolvable

    Parameters:
    randomSeed - DOCUMENT ME!

    randomize

    public void randomize(int numberOfMoves)
    DOCUMENT ME!

    Parameters:
    numberOfMoves - DOCUMENT ME!

    randomize

    public void randomize(int numberOfMoves,
                          long randomSeed)
    DOCUMENT ME!

    Parameters:
    numberOfMoves - DOCUMENT ME!
    randomSeed - DOCUMENT ME!

    getSolutionMatrix

    public java.lang.Object[][] getSolutionMatrix()
    DOCUMENT ME!

    Returns:
    DOCUMENT ME!

    getPuzzleMatrix

    public java.lang.Object[][] getPuzzleMatrix()
    DOCUMENT ME!

    Returns:
    DOCUMENT ME!

    getWinner

    public int[] getWinner()
    DOCUMENT ME!

    Returns:
    DOCUMENT ME!

    getResult

    public double getResult(int playerRole)
    DOCUMENT ME!

    Specified by:
    getResult in interface GamePlay
    Overrides:
    getResult in class AbstractGame
    Parameters:
    playerRole - DOCUMENT ME!
    Returns:
    DOCUMENT ME!

    nextPlayer

    public int nextPlayer()
    DOCUMENT ME!

    Returns:
    DOCUMENT ME!

    listLegalMoves

    protected GameMove[] listLegalMoves()
    DOCUMENT ME!

    Specified by:
    listLegalMoves in class AbstractGame
    Returns:
    DOCUMENT ME!

    setEndWhenSolved

    public void setEndWhenSolved(boolean enable)
    See Also:
    isEndWhenSolved()

    isEndWhenSolved

    public boolean isEndWhenSolved()
    If isEndWhenSolved(), no legal moves are possible anymore once the puzzle is solved or when it has been determined that the game has no solution path.

    Returns:
    DOCUMENT ME!

    setReverseMoveDisabled

    public void setReverseMoveDisabled(boolean enable)
    See Also:
    isReverseMoveDisabled()

    isReverseMoveDisabled

    public boolean isReverseMoveDisabled()
    If isReverseMoveDisabled(), reverting the game status back to the state it came from (examining the last move) is not a legal move.

    Returns:
    DOCUMENT ME!

    pushMove

    protected boolean pushMove(GameMove move)
    DOCUMENT ME!

    Specified by:
    pushMove in class AbstractGame
    Parameters:
    move - DOCUMENT ME!
    Returns:
    DOCUMENT ME!

    popMove

    protected boolean popMove()
    DOCUMENT ME!

    Specified by:
    popMove in class AbstractGame
    Returns:
    DOCUMENT ME!

    toString

    public java.lang.String toString()
    DOCUMENT ME!

    Overrides:
    toString in class AbstractGame
    Returns:
    DOCUMENT ME!

    clone

    public java.lang.Object clone()
                           throws java.lang.CloneNotSupportedException
    DOCUMENT ME!

    Specified by:
    clone in interface GamePlay
    Overrides:
    clone in class AbstractGame
    Returns:
    DOCUMENT ME!
    Throws:
    java.lang.CloneNotSupportedException - DOCUMENT ME!
    See Also:
    AbstractGame.spawnChild(GameMove)

    equals

    public boolean equals(java.lang.Object obj)
    DOCUMENT ME!

    Overrides:
    equals in class java.lang.Object
    Parameters:
    obj - DOCUMENT ME!
    Returns:
    DOCUMENT ME!

    hashCode

    public int hashCode()
    rather experimental at this point; needs to be tested for efficiency

    Overrides:
    hashCode in class java.lang.Object
    Returns:
    DOCUMENT ME!