Holger's
Java API

com.antelmann.game.fourwins
Class FourWinsGame

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

public class FourWinsGame
extends AbstractGame

FourWinsGame represents the game of 4-wins or 4-connects. (other names for the game: four wins, connect four, vier gewinnt)

Author:
Holger Antelmann
See Also:
Serialized Form

Field Summary
protected  int[][] board
           
 int boardHeight
           
 int boardWidth
           
 int MAX_MOVES
           
static int WINNING_LINE_LENGTH
           
 
Constructor Summary
FourWinsGame()
          creates the standard game board with 7x6
FourWinsGame(String name)
           
FourWinsGame(String name, int width, int height)
           
 
Method Summary
protected  int checkPositionWin(int column, int row)
           
 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)
           
protected  int freePositionInColumn(int column)
           
 int getHeight()
           
 int getValueAt(int column, int row)
          Returns the value for a specified position in the game board.
 int getWidth()
           
 int[] getWinner()
          getWinner() returns an array of all winning game roles - as there could be more than one winner.
 int hashCode()
          experimental, but working
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.
 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
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

WINNING_LINE_LENGTH

public static final int WINNING_LINE_LENGTH
See Also:
Constant Field Values

MAX_MOVES

public final int MAX_MOVES

boardWidth

public final int boardWidth

boardHeight

public final int boardHeight

board

protected int[][] board
Constructor Detail

FourWinsGame

public FourWinsGame()
creates the standard game board with 7x6


FourWinsGame

public FourWinsGame(String name)

FourWinsGame

public FourWinsGame(String name,
                    int width,
                    int height)
             throws IllegalArgumentException
Throws:
IllegalArgumentException
Method Detail

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

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.


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.

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.

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.


getValueAt

public int getValueAt(int column,
                      int row)
               throws ArrayIndexOutOfBoundsException
Returns the value for a specified position in the game board. The parameters are directly used to retrieve the array elements; therefore, ArrayIndexOutOfBoundsException may be thrown

Throws:
ArrayIndexOutOfBoundsException

freePositionInColumn

protected int freePositionInColumn(int column)
                            throws IllegalArgumentException
Throws:
IllegalArgumentException

checkPositionWin

protected int checkPositionWin(int column,
                               int row)

getWidth

public int getWidth()

getHeight

public int getHeight()

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)

hashCode

public int hashCode()
experimental, but working

Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals 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