Holger's
Java API

com.antelmann.game.puzzle
Class TilePuzzle

java.lang.Object
  extended by com.antelmann.game.AbstractGame
      extended by com.antelmann.game.puzzle.TilePuzzle
All Implemented Interfaces:
GamePlay, Serializable, Cloneable

public class TilePuzzle
extends AbstractGame

implements a very flexible tile puzzle game with various options

Author:
Holger Antelmann
See Also:
TilePuzzleSamples, TilePuzzlePlayer, Serialized Form

Constructor Summary
TilePuzzle()
          uses TilePuzzleSamples.getNumberPuzzle(3) to initialize the puzzle
TilePuzzle(String name, Object[][] puzzle)
          When constructed, the puzzle's current state equals its solution.
TilePuzzle(String name, Object[][] scrampledPuzzle, 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
 Object clone()
          Any inheriting class with non-primitive members must override this clone() method to provide a full deep copy of the object, which is essential for spawnChild() to work correctly.
 boolean equals(Object obj)
           
 Object[][] getPuzzleMatrix()
           
 double getResult(int playerRole)
          a default implementation for convenience which may suit most games that do not involve betting or any form of measuring how 'big' the win was.
 Object[][] getSolutionMatrix()
           
 int[] getWinner()
          getWinner() returns an array of all winning game roles - as there could be more than one winner.
 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()
          listLegalMoves() returns the legal moves for this game.
 int nextPlayer()
          nextPlayer() returns the integer representing the game role of the next player.
protected  boolean popMove()
          popMove undoes the last move by altering the game board to the stage before the last move happened.
protected  boolean pushMove(GameMove move)
          pushMove takes a GameMove and alters the game according to to the move.
 void randomize(int numberOfMoves)
           
 void randomize(int numberOfMoves, long randomSeed)
           
 void reset()
           
 void setEndWhenSolved(boolean enable)
           
 void setReverseMoveDisabled(boolean enable)
           
 void shuffle()
           
 void shuffle(long randomSeed)
          note that calling shuffle() may make the puzzle unsolvable
 String toString()
          overridden to provide useful information about the game
 
Methods inherited from class com.antelmann.game.AbstractGame
addObserver, clearRedoList, gameOver, getGameName, getLastMove, getLastPlayer, getLegalMoves, getLegalMoves, getMoveHistory, getNumberOfRedoMoves, getObservers, getRedoList, isLegalMove, isWinner, loadFromFile, makeMove, numberOfMoves, numberOfPlayers, redoMove, removeObserver, 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(String name,
                  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(String name,
                      Object[][] scrampledPuzzle,
                      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()

    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.

    Throws:
    com.antelmann.game.PuzzleNotSolvableException - if the puzzle state cannot reach the solution
    PuzzleNotSolvableException

    shuffle

    public void shuffle()

    shuffle

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


    randomize

    public void randomize(int numberOfMoves)

    randomize

    public void randomize(int numberOfMoves,
                          long randomSeed)

    getSolutionMatrix

    public Object[][] getSolutionMatrix()

    getPuzzleMatrix

    public Object[][] getPuzzleMatrix()

    getWinner

    public int[] getWinner()
    Description copied from interface: GamePlay
    getWinner() returns an array of all winning game roles - as there could be more than one winner. If the function returns null, it doesn't necessarily mean that the game is still in progress, but it could also mean that there is no winner in this game even though it is over (alternatively, an empty array could be returned depending on the requirements of the game). Also, if winners are returned, the game could still be in progress. Maybe, a game keeps playing to determine who is the second winner, etc.


    getResult

    public double getResult(int playerRole)
    Description copied from class: AbstractGame
    a default implementation for convenience which may suit most games that do not involve betting or any form of measuring how 'big' the win was.

    Specified by:
    getResult in interface GamePlay
    Overrides:
    getResult in class AbstractGame
    Returns:
    1 if game is won, -1 if game is lost and 0 if no winners are present

    nextPlayer

    public int nextPlayer()
    Description copied from interface: GamePlay
    nextPlayer() returns the integer representing the game role of the next player. If there is no next player, it is assumed that there are no more legal moves left in the game, in which case the value of nextPlayer() should be irrelevant.


    listLegalMoves

    protected GameMove[] listLegalMoves()
    Description copied from class: AbstractGame
    listLegalMoves() returns the legal moves for this game. This function is used by the final method getLegalMoves(), which is a wrapper around this method to avoid the overhead of generating the legal moves over and over again when called multiple times (as calculating legal moves is commonly expensive). Note that implementations of listLegalMoves() must not check for gameOver(), unless gameOver() is overridden not to check for listLegalMoves(). The same applies to isLegalMove().

    Specified by:
    listLegalMoves in class AbstractGame

    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.


    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.


    pushMove

    protected boolean pushMove(GameMove move)
    Description copied from class: AbstractGame
    pushMove takes a GameMove and alters the game according to to the move.

    Specified by:
    pushMove in class AbstractGame
    Returns:
    true only if the move was put on the board successfully.

    popMove

    protected boolean popMove()
    Description copied from class: AbstractGame
    popMove undoes the last move by altering the game board to the stage before the last move happened.

    Specified by:
    popMove in class AbstractGame
    Returns:
    true only if the move was taken back successfully.

    toString

    public String toString()
    Description copied from class: AbstractGame
    overridden to provide useful information about the game

    Overrides:
    toString in class AbstractGame

    clone

    public Object clone()
                 throws CloneNotSupportedException
    Description copied from class: AbstractGame
    Any inheriting class with non-primitive members must override this clone() method to provide a full deep copy of the object, which is essential for spawnChild() to work correctly. Ay overriding of this method should still begin with a call to super.clone(), though. Also, note that this clone() method does not clone the embedded GameMove objects, i.e. those are expected not to change (except for their heuristics, which shouldn't affect game functionality) - or the subclass will have to take care of this.

    Specified by:
    clone in interface GamePlay
    Overrides:
    clone in class AbstractGame
    Throws:
    CloneNotSupportedException
    See Also:
    AbstractGame.spawnChild(GameMove)

    equals

    public boolean equals(Object obj)
    Overrides:
    equals in class Object

    hashCode

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

    Overrides:
    hashCode in class Object


    (c) 2001-2006 Holger Antelmann - all rights reserved (contact: info@antelmann.com)
    see www.antelmann.com/developer for further details and available downloads