Holger's
Java API

com.antelmann.game.puzzle
Class WSPuzzle

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

public class WSPuzzle
extends AbstractGame

WSPuzzle implements the 'Wolf & Sheep Puzzle'. This puzzle is inspired by
http://www.fitzweb.com/brainteasers/wolves.shtml
The solution can be easily found by searching through 6379 game positions.

Author:
Holger Antelmann
See Also:
Serialized Form

Field Summary
static byte SHEEP
           
static byte WOLF
           
 
Constructor Summary
WSPuzzle()
           
 
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.
 int[] getWinner()
          getWinner() returns an array of all winning game roles - as there could be more than one winner.
protected  GameMove[] listLegalMoves()
          lists all legal moves.
static void main(String[] args)
          solves the puzzle and then fires up a GUI to let you examine the solution
 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.
static GamePlay searchSolution()
          tries to solve the puzzle with GameUtilities functions
 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, getResult, isLegalMove, isWinner, loadFromFile, makeMove, numberOfMoves, numberOfPlayers, redoMove, removeObserver, resetLegalMoves, resetLists, saveToFile, spawnChild, undoLastMove, undoMoves
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

WOLF

public static final byte WOLF
See Also:
Constant Field Values

SHEEP

public static final byte SHEEP
See Also:
Constant Field Values
Constructor Detail

WSPuzzle

public WSPuzzle()
Method Detail

main

public static void main(String[] args)
solves the puzzle and then fires up a GUI to let you examine the solution


searchSolution

public static GamePlay searchSolution()
tries to solve the puzzle with GameUtilities functions


listLegalMoves

protected GameMove[] listLegalMoves()
lists all legal moves. To minimize the possibilities, fist place a wolf, then a sheep, then a wolf, etc. until 5 wolfs and 3 sheeps are on the board (the 7th and the 8th move is placing a wolf).

Specified by:
listLegalMoves in class AbstractGame

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.

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.


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.


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)


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