fourwin
Class PlayingField

java.lang.Object
  extended by fourwin.PlayingField

public class PlayingField
extends Object

This class represents a situation on the Connect4 playing field. It is used to safe the real situation of the connect4 board and to save the simulated situations used by AI algorithms.

The situation is saved in 7x6 array. (width x height, top left-hand corner is 0,0) Every field represents a position in the connect4 field. The situation is coded as follows:

Using that code makes it easily possible to detect 2,3 or 4 chips in a row for one player by simply adding the values saved for the single fields. The result is unique. Example: 4-in-a-row for player 1 means that the sum of 4 neighbored positions is 1+1+1+1 = 4. The 4 is only reachable with 4x1.

Version:
V1.0 13.01.2010
Author:
Konstantin, Sebastian, Marcel

Field Summary
(package private)  int lastInsertedColumn
           
(package private)  int lastInsertedRow
           
(package private)  int numberOfChips
           
(package private)  byte[][] playingarray
           
 
Constructor Summary
PlayingField()
           
PlayingField(PlayingField pf)
           
 
Method Summary
 int checkWin()
          This method checks if there exists a winner for the actual board.
 int Evaluate(int playerID)
          For backward compatibility.
 int Evaluate(int playerID, int numberInRow)
           
 boolean fullColumn(int column)
          This method checks if a column is full with chips or not.
 boolean fullField()
          This method checks if the playing field is full with chips ( true ) or not ( false ) to check a possible draw.
 byte getField(int row, int column)
           
 byte getFirstEmptyRow(byte column)
          This method returns the first empty row if the column is overgiven.
 int getLastInsertedColumn()
           
 int getNumberOfChips()
           
private  int getNumberOfSumInA4Row(int sumToCheck)
          Returns the number of found sum for each 4-chip possibility.
private  int getNumberOfSumInA4RowDiagonal(int sumToCheck)
          Splitted getNumberOfSumInA4Row() just to check the diagonal possibilities.
private  int getNumberOfSumInA4RowHorizontal(int sumToCheck)
          Splitted getNumberOfSumInA4Row() just to check the horizontal possibilities.
private  int getNumberOfSumInA4RowVertical(int sumToCheck)
          Splitted getNumberOfSumInA4Row() just to check the vertical possibilities.
private  int getNumberOfSumInA5RowHorizontal(int sumToCheck)
          Searches for 5-in-a-row chances (which means two 4-in-a-row chances that cannot be defended)
 ArrayList<Integer> getPossibleMoves()
          This method returns an ArrayList (IEnumerable) with all moves which are possible at the current situation.
 ArrayList<Integer> getPossibleMovesFromRightToLeft()
          This method returns all possible moves beginning from the right going to the left.
 boolean insertChip(byte value, int column)
          This method inserts a chip in a specific column from a specific player if the column is not full.
 void Reset()
          Resets the playing field.
 void setField(int row, int column, byte value)
           
 void UndoLastMove()
          Deprecated. 
 void UndoMove(byte column)
          Removes the topmost chip in the given column.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

playingarray

byte[][] playingarray

lastInsertedColumn

int lastInsertedColumn

lastInsertedRow

int lastInsertedRow

numberOfChips

int numberOfChips
Constructor Detail

PlayingField

public PlayingField()

PlayingField

public PlayingField(PlayingField pf)
Method Detail

Reset

public void Reset()
Resets the playing field. All chips are removed.


fullField

public boolean fullField()
This method checks if the playing field is full with chips ( true ) or not ( false ) to check a possible draw.

Returns:
true/false dependent on the field

setField

public void setField(int row,
                     int column,
                     byte value)

getField

public byte getField(int row,
                     int column)

getLastInsertedColumn

public int getLastInsertedColumn()

getNumberOfChips

public int getNumberOfChips()

fullColumn

public boolean fullColumn(int column)
This method checks if a column is full with chips or not.

Parameters:
column - to check
Returns:
true/false dependent on the column

insertChip

public boolean insertChip(byte value,
                          int column)
This method inserts a chip in a specific column from a specific player if the column is not full.

Parameters:
value - of Player ( similar to Player/Opponent ID )
column - to insert chip
Returns:
true, if Chip was inserted

getFirstEmptyRow

public byte getFirstEmptyRow(byte column)
This method returns the first empty row if the column is overgiven.

Parameters:
column -
Returns:
first empty row

UndoLastMove

@Deprecated
public void UndoLastMove()
Deprecated. 

Removes the last inserted chip. Method is used to undo moves in the simulation. Doesn't meet the needs of the simulation. UndoLastMove(byte column) is used instead.


UndoMove

public void UndoMove(byte column)
Removes the topmost chip in the given column. Method is used to undo moves in the simulation.

Parameters:
column - Column to remove the topmost chip.

checkWin

public int checkWin()
This method checks if there exists a winner for the actual board. It checks all possible 4-rows for the last inserted Chips after every turn. To check this, it checks the sum of the value of the chips ( 1 is equivalent to yellow, 2 is equivalent to red ).

Returns:
the winner of the game: 1 stands for yellow 2 stands for red 0 stands for a draw or even not a winner yet

getPossibleMoves

public ArrayList<Integer> getPossibleMoves()
This method returns an ArrayList (IEnumerable) with all moves which are possible at the current situation. A move is possible if the row is not already full. The moves are presorted to speed up Alpha-Beta algorithm.

Returns:
ArrayList containing all possible moves.

getPossibleMovesFromRightToLeft

public ArrayList<Integer> getPossibleMovesFromRightToLeft()

This method returns all possible moves beginning from the right going to the left. Possible move means that the column is not full.

The method is used by the board scanner which begins its search at the first column on the right-hand side.

Returns:
possible moves beginning from the right to the left

Evaluate

public int Evaluate(int playerID)
For backward compatibility. Method doesen't expect the numberInRow parameter which is set to 3 by default.

Parameters:
playerID - ID of the player for which the evaluation should be done.
Returns:
Evaluation value

Evaluate

public int Evaluate(int playerID,
                    int numberInRow)
Parameters:
playerID - Player-ID of player for which the playing field is evaluated.
numberInRow -
Returns:
Evaluation of current situation

getNumberOfSumInA4Row

private int getNumberOfSumInA4Row(int sumToCheck)
Returns the number of found sum for each 4-chip possibility. The sum specifies how many chips of one player are within that 4-chip chance. See PlayingField class documentation for description of the sum.

Parameters:
sumToCheck -
Returns:
Number of found chances for specified sum

getNumberOfSumInA4RowHorizontal

private int getNumberOfSumInA4RowHorizontal(int sumToCheck)
Splitted getNumberOfSumInA4Row() just to check the horizontal possibilities.

Parameters:
sumToCheck -
Returns:
Number of found chances for specified sum
See Also:
getNumberOfSumInA4Row(int)

getNumberOfSumInA5RowHorizontal

private int getNumberOfSumInA5RowHorizontal(int sumToCheck)
Searches for 5-in-a-row chances (which means two 4-in-a-row chances that cannot be defended)

Parameters:
sumToCheck -
Returns:
Number of found 5-in-a-row chances for specified sum

getNumberOfSumInA4RowVertical

private int getNumberOfSumInA4RowVertical(int sumToCheck)
Splitted getNumberOfSumInA4Row() just to check the vertical possibilities.

Parameters:
sumToCheck -
Returns:
Number of found chances for specified sum
See Also:
getNumberOfSumInA4Row(int)

getNumberOfSumInA4RowDiagonal

private int getNumberOfSumInA4RowDiagonal(int sumToCheck)
Splitted getNumberOfSumInA4Row() just to check the diagonal possibilities.

Parameters:
sumToCheck -
Returns:
Number of found chances for specified sum
See Also:
getNumberOfSumInA4Row(int)