org.moyoman.module.loosegroups.simplelgimpl
Class LGImpl

java.lang.Object
  |
  +--org.moyoman.module.Module
        |
        +--org.moyoman.module.loosegroups.simplelgimpl.LGImpl
All Implemented Interfaces:
Cloneable, Component, LooseGroups, ModuleInterface, Serializable

public class LGImpl
extends Module
implements LooseGroups

This class is responsible for assigning stones to a loose group. Each stone on the board is a member of exactly one loose group. The total number of loose groups is less than or equal to the number of groups. Any two stones that are in the same group are also in the same loose group.

See Also:
Serialized Form

Field Summary
private  Board board
          The Board object with the current state of the board.
private  ArrayList combinedGroups
          Contains SingleLooseGroup objects which were combined on the current move into one.
private  ClosestPoints cp
          The ClosestPoints class is used to help determine which groups are in the same loose group.
private static DebugType[] dt
          The debug types that this module produces.
private static HashMap edgePoints
          The key is a Long returned by Zobrist, and the value is a Point array.
private  ArrayList fullyCapturedGroups
          Contains SingleLooseGroups which were entirely captured by the last move.
private  boolean groupsCaptured
          This is true if at least one loose group was entirely captured on the last move.
private  boolean groupsSplit
          If any loose groups were split into two or more loose groups on the last move, this is true.
private  Color moveColor
          The color of the player to move.
private  short moveNumber
          The number of the next move.
private  short moveStatus
          One of the LooseGroups constants ADD, UPDATE, or COMBINE.
private  ModuleType[] mt
          The module types that this module is dependent on.
private  SingleLGImpl newGroup
          If status() returns ADD, then this is the new group.
private  ArrayList partiallyCapturedGroups
          Contains SingleLooseGroups which had some stones captured from them by the last move.
private  ArrayList ratedMoves
          The RatedMove objects created by generateMove().
private  ArrayList slggroups
          Each element is a SingleLooseGroup object.
private  ArrayList splitGroups
          If groupsSplit is true, then this contains SingleLooseGroup objects after splitting.
private  SingleLGImpl updatedGroup
          If status() returns UPDATE then this is the updated group.
 
Fields inherited from class org.moyoman.module.Module
 
Fields inherited from interface org.moyoman.module.loosegroups.LooseGroups
ADD, COMBINE, SUICIDE, UPDATE
 
Constructor Summary
LGImpl(GameId id, ModuleName name)
          Create the LGImpl object.
 
Method Summary
private  boolean checkEmptyPoints(ClosestPoints cpts, ArrayList al, Color col, ClosestPointGroup cpg1, ClosestPointGroup cpg2)
          Determine if there are any opponents stones preventing the two groups from combining.
private  void checkOpponentsLooseGroups(Stone stone, ClosestPointGroup cpg, Groups groups, Board board)
          Split opponents groups adjacent to the current loose group if necessary.
private  boolean checkPonnuki(Point center, Color color, Board board)
          Determine if at least three sides of a ponnuki of the given color exist around the center point.
 Object clone()
          Override the Object.clone() method.
private  void combineAdjacentGroupsIfPossible(ClosestPointGroup[] adjgroups, Board board, Groups groups)
          Combine as many of the candidate groups as possible.
private  void combineAdjacentGroupsIfPossible(ClosestPointGroup currcpg, Board board, Groups groups)
          Attempt to combine any friendly groups adjacent to the group containing the last stone.
private  void combineGroups(SingleLGImpl[] grp)
          Combine the single loose groups into one loose group.
private  void combineGroups(SingleLGImpl g1, SingleLGImpl g2)
          Combine the two SingleLooseGroups into one.
private  ClosestPointGroup[] combinePairs(ClosestPoints cpts, ArrayList al, Board board, Groups groups)
          Combine the ClosestPointGroups as much as possible based on criteria for loose groups.
 void generateMove(Module[] modules)
          Generate moves for the loose group module.
 Point[] getAssociatedEdgePoints(SingleLooseGroup slg)
          Get the edge points for the given loose group.
 SingleLooseGroup[] getCombinedSingleLooseGroups()
          Return the loose groups that were combined into a single loose group.
 Debug[] getDebugInformation(DebugType[] types)
          Get the debug information for this module.
 DebugType[] getDebugTypes()
          Get the debug types that this module produces.
 RatedMove[] getMoves()
          Get the rated moves that this module creates.
 SingleLooseGroup getNewSingleLooseGroup()
          Get the new loose group that contains the last move made.
 ModuleType[] getRequiredModuleList()
          Get the module types that this module requires.
 SingleLooseGroup getSingleLooseGroup(SingleGroup sg)
          Get the loose group which contains the single group.
 SingleLooseGroup getSingleLooseGroup(Stone stone)
          Get the loose group which contains the stone.
 SingleLooseGroup[] getSingleLooseGroups()
          This method returns all of the loose groups.
 SingleLooseGroup[] getSingleLooseGroupsFullyCaptured()
          Return loose groups that were completely captured on the last move.
 SingleLooseGroup[] getSingleLooseGroupsPartiallyCaptured()
          Return loose groups that were partially captured on the last move.
 SingleLooseGroup[] getSplitLooseGroups()
          Get the loose groups that were split by the last move.
private  StoneDistance[] getStoneDistances(Stone[] s1, Stone[] s2)
          Get the distances between any two pair of stones.
 SingleLooseGroup[] getSuicidedSingleLooseGroups()
          Return the loose groups that were removed because of suicide.
 SingleLooseGroup getUpdatedSingleLooseGroup()
          Get the loose group that contains the last move made.
private  boolean isSameLooseGroup(ClosestPointGroup cpg1, ClosestPointGroup cpg2, Board board)
          Determine if the two groups should be part of the same loose group.
private  boolean isSameLooseGroup(ClosestPoints cpts, ClosestPointGroup cpg1, ClosestPointGroup cpg2, Board board)
          Determine if the two groups should be part of the same loose group.
 void makeMove(Move move, Module[] modules)
          Update the internal data structures with the last move.
private  boolean process3JumpCase(Stone tent1, Stone tent2, int index, StoneDistance[] sdarr, ArrayList lgstones)
          Determine if the two groups form a three space extension from a two stone wall.
private  void removeGroup(SingleLGImpl slg)
          Remove the group from the list of loose groups.
private  SingleLGImpl[] splitGroupIfNecessary(Stone stone, SingleLGImpl candidate, Groups groups, Board board)
          Determine if the given loose group should be split up based on the last move.
 short status()
          Return the status of the last move made.
 String toString()
          Get a String representation of the object.
private  void updatePartialAndFullCaptureSLG(SingleGroup[] sg)
          Update the list of groups that were fully or partially captured.
 boolean wasLooseGroupSplit()
          Determine if a loose group was split on the last move.
 boolean wereGroupsCaptured()
          Determine if a loose group was completely captured on the last move.
 
Methods inherited from class org.moyoman.module.Module
checkTime, create, createHelper, debug, error, error, fatal, fatal, getAllHelpers, getHelper, getId, getKomi, getModule, getModuleName, getScheduler, information, setTime, warning, warning
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.moyoman.module.ModuleInterface
getScheduler
 

Field Detail

dt

private static DebugType[] dt
The debug types that this module produces.


mt

private ModuleType[] mt
The module types that this module is dependent on.


moveNumber

private short moveNumber
The number of the next move.


moveColor

private Color moveColor
The color of the player to move.


slggroups

private ArrayList slggroups
Each element is a SingleLooseGroup object.


cp

private ClosestPoints cp
The ClosestPoints class is used to help determine which groups are in the same loose group.


board

private Board board
The Board object with the current state of the board.


ratedMoves

private ArrayList ratedMoves
The RatedMove objects created by generateMove().


moveStatus

private short moveStatus
One of the LooseGroups constants ADD, UPDATE, or COMBINE.


groupsSplit

private boolean groupsSplit
If any loose groups were split into two or more loose groups on the last move, this is true.


groupsCaptured

private boolean groupsCaptured
This is true if at least one loose group was entirely captured on the last move.


newGroup

private SingleLGImpl newGroup
If status() returns ADD, then this is the new group.


updatedGroup

private SingleLGImpl updatedGroup
If status() returns UPDATE then this is the updated group.


splitGroups

private ArrayList splitGroups
If groupsSplit is true, then this contains SingleLooseGroup objects after splitting.


combinedGroups

private ArrayList combinedGroups
Contains SingleLooseGroup objects which were combined on the current move into one.


partiallyCapturedGroups

private ArrayList partiallyCapturedGroups
Contains SingleLooseGroups which had some stones captured from them by the last move.


fullyCapturedGroups

private ArrayList fullyCapturedGroups
Contains SingleLooseGroups which were entirely captured by the last move.


edgePoints

private static HashMap edgePoints
The key is a Long returned by Zobrist, and the value is a Point array.

Constructor Detail

LGImpl

public LGImpl(GameId id,
              ModuleName name)
       throws InternalErrorException
Create the LGImpl object.

Parameters:
id - The game id.
name - The ModuleName of this module.
Throws:
InternalErrorException - Thrown if the create fails for any reason.
Method Detail

status

public short status()
Return the status of the last move made. The status is one of three constants indicating that the last move either:
  1. Caused a new loose group to be created.
  2. Updated an existing loose group
  3. Combined two or more existing loose groups.

The status is independent of whether any stones were captured on the last move.

Specified by:
status in interface LooseGroups
Returns:
The status.

wasLooseGroupSplit

public boolean wasLooseGroupSplit()
Determine if a loose group was split on the last move.

Specified by:
wasLooseGroupSplit in interface LooseGroups
Returns:
true if a loose group was split, or false.

wereGroupsCaptured

public boolean wereGroupsCaptured()
Determine if a loose group was completely captured on the last move.

Specified by:
wereGroupsCaptured in interface LooseGroups
Returns:
true if a loose group was completely captured, or false.

getSplitLooseGroups

public SingleLooseGroup[] getSplitLooseGroups()
Get the loose groups that were split by the last move.

Specified by:
getSplitLooseGroups in interface LooseGroups
Returns:
An array of SingleLooseGroup objects that were one loose group prior to the last move.

getSingleLooseGroups

public SingleLooseGroup[] getSingleLooseGroups()
This method returns all of the loose groups.

Specified by:
getSingleLooseGroups in interface LooseGroups
Returns:
An array of SingleLooseGroup objects.

getSingleLooseGroup

public SingleLooseGroup getSingleLooseGroup(SingleGroup sg)
                                     throws NoSuchDataException
Get the loose group which contains the single group.

Specified by:
getSingleLooseGroup in interface LooseGroups
Parameters:
sg - The group in question.
Returns:
The SingleLooseGroup object which contains the single group.
Throws:
NoSuchDataException - Thrown if the loose group cannot be found.

getSingleLooseGroup

public SingleLooseGroup getSingleLooseGroup(Stone stone)
                                     throws NoSuchDataException
Get the loose group which contains the stone.

Specified by:
getSingleLooseGroup in interface LooseGroups
Parameters:
stone - The stone in question.
Returns:
The SingleLooseGroup object which contains the stone.
Throws:
NoSuchDataException - Thrown if the loose group cannot be found.

getSingleLooseGroupsPartiallyCaptured

public SingleLooseGroup[] getSingleLooseGroupsPartiallyCaptured()
Return loose groups that were partially captured on the last move. The groups are as they were before the capture, containing some SingleGroup objects that were captured, and some that remain on the board. Use the Groups.getCapturedSingleGroups() method to determine which stones are still in this loose group.

Specified by:
getSingleLooseGroupsPartiallyCaptured in interface LooseGroups
Returns:
An array of SingleLooseGroup objects.

getSingleLooseGroupsFullyCaptured

public SingleLooseGroup[] getSingleLooseGroupsFullyCaptured()
Return loose groups that were completely captured on the last move. The set of SingleLooseGroup objects returned by this method and getSingleLooseGroupsPartiallyCaptured() are disjoint.

Specified by:
getSingleLooseGroupsFullyCaptured in interface LooseGroups
Returns:
An array of SingleLooseGroup objects.

getNewSingleLooseGroup

public SingleLooseGroup getNewSingleLooseGroup()
                                        throws NoSuchDataException
Get the new loose group that contains the last move made.

Specified by:
getNewSingleLooseGroup in interface LooseGroups
Returns:
a SingleLooseGroup object.
Throws:
NoSuchDataException - Thrown if the last move did not case a new loose group to be created.

getUpdatedSingleLooseGroup

public SingleLooseGroup getUpdatedSingleLooseGroup()
                                            throws NoSuchDataException
Get the loose group that contains the last move made.

Specified by:
getUpdatedSingleLooseGroup in interface LooseGroups
Returns:
A SingleLooseGroup object.
Throws:
NoSuchDataException - Thrown if the last move did not add a stone to an existing loose group.

getCombinedSingleLooseGroups

public SingleLooseGroup[] getCombinedSingleLooseGroups()
                                                throws NoSuchDataException
Return the loose groups that were combined into a single loose group.

Specified by:
getCombinedSingleLooseGroups in interface LooseGroups
Returns:
An array of SingleLooseGroup objects.
Throws:
NoSuchDataException - Thrown if the last move did not cause two or more existing loose groups to be combined.

getSuicidedSingleLooseGroups

public SingleLooseGroup[] getSuicidedSingleLooseGroups()
                                                throws NoSuchDataException
Return the loose groups that were removed because of suicide.

Specified by:
getSuicidedSingleLooseGroups in interface LooseGroups
Returns:
An array of SingleLooseGroup objects.
Throws:
NoSuchDataException - Thrown if the last move did not result in suicide.

removeGroup

private void removeGroup(SingleLGImpl slg)
Remove the group from the list of loose groups.

Parameters:
slg - The loose group to be removed.

generateMove

public void generateMove(Module[] modules)
Generate moves for the loose group module. Currently, this module does not recommend any moves. The makeMove() method is creating the Debug objects, so this method does not need to.

Specified by:
generateMove in class Module
Parameters:
modules - - The modules which this module needs in order to make its move.

getDebugInformation

public Debug[] getDebugInformation(DebugType[] types)
Get the debug information for this module.

Specified by:
getDebugInformation in class Module
Parameters:
types - The debug types that the caller can handle.
Returns:
An array of Debug objects.

getDebugTypes

public DebugType[] getDebugTypes()
Get the debug types that this module produces.

Specified by:
getDebugTypes in class Module
Returns:
An array of DebugType objects.

getMoves

public RatedMove[] getMoves()
Get the rated moves that this module creates. Currently, this module does not rate moves, so the array size will always be zero.

Specified by:
getMoves in interface ModuleInterface
Specified by:
getMoves in class Module
Returns:
An array of RatedMove objects.

getRequiredModuleList

public ModuleType[] getRequiredModuleList()
Get the module types that this module requires.

Specified by:
getRequiredModuleList in class Module
Returns:
An array of ModuleType objects.

makeMove

public void makeMove(Move move,
                     Module[] modules)
              throws InternalErrorException
Update the internal data structures with the last move.

Specified by:
makeMove in class Module
Parameters:
move - The move that was made.
modules - Modules that this module uses.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

updatePartialAndFullCaptureSLG

private void updatePartialAndFullCaptureSLG(SingleGroup[] sg)
                                     throws InternalErrorException
Update the list of groups that were fully or partially captured. The loose groups that are stored are as they were before any stones were removed from them.

Parameters:
sg - An array of SingleGroup objects that were captured on the last move.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

combineAdjacentGroupsIfPossible

private void combineAdjacentGroupsIfPossible(ClosestPointGroup currcpg,
                                             Board board,
                                             Groups groups)
                                      throws InternalErrorException
Attempt to combine any friendly groups adjacent to the group containing the last stone. If any prisoners were captured on the last move, it is also necessary to examine any friendly groups which were adjacent to the prisoners.

Parameters:
currcpg - The ClosestPointGroup which contains the last move.
board - The Board module.
groups - The Groups module.
Throws:
InternalErrorException - Thrown is an error occurred for any reason.

combineAdjacentGroupsIfPossible

private void combineAdjacentGroupsIfPossible(ClosestPointGroup[] adjgroups,
                                             Board board,
                                             Groups groups)
                                      throws InternalErrorException
Combine as many of the candidate groups as possible.

Parameters:
adjgroups - An array of ClosestPointGroup objects corresponding to the SingleLooseGroups.
board - The Board module.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

combineGroups

private void combineGroups(SingleLGImpl[] grp)
Combine the single loose groups into one loose group.

Parameters:
grp - An array of SingleLGImpl groups to be combined into one.

combineGroups

private void combineGroups(SingleLGImpl g1,
                           SingleLGImpl g2)
Combine the two SingleLooseGroups into one. The results of the combined groups are in the first loose group.

Parameters:
g1 - - The SingleLooseGroup to hold the results of the combination.
g2 - - The SingleLooseGroup to be removed.

isSameLooseGroup

private boolean isSameLooseGroup(ClosestPointGroup cpg1,
                                 ClosestPointGroup cpg2,
                                 Board board)
Determine if the two groups should be part of the same loose group. This method is used when the current board is being operated on.

Parameters:
cpg1 - - One of the ClosestPointGroup objects.
cpg2 - - The other ClosestPointGroup object.
board - - The Board module.
Returns:
true if the two groups should be combined, or false.

isSameLooseGroup

private boolean isSameLooseGroup(ClosestPoints cpts,
                                 ClosestPointGroup cpg1,
                                 ClosestPointGroup cpg2,
                                 Board board)
Determine if the two groups should be part of the same loose group. This method is really the intelligence of this module. More sophisticated implementations of LooseGroups could leave the rest of the code unchanged, and just work on improving this method.

This method takes a ClosestPoints object as a parameter so that it can work on different board configurations than the one with the current move.

Parameters:
cpts - - The ClosestPoints object.
cpg1 - - One of the ClosestPointGroup objects.
cpg2 - - The other ClosestPointGroup object.
board - - The Board module.
Returns:
true if the two groups should be combined, or false.

process3JumpCase

private boolean process3JumpCase(Stone tent1,
                                 Stone tent2,
                                 int index,
                                 StoneDistance[] sdarr,
                                 ArrayList lgstones)
Determine if the two groups form a three space extension from a two stone wall. In addition, put the three stones into the lgstones ArrayList variable.

Parameters:
tent1 - The tentative stone from the first group.
index - The index to start searching the sorted sdarr array at.
sdarr - The sorted array of StoneDistance objects.
lgstones - The ArrayList to add the three stones to if they are present.
Returns:
true if the three space jump from a two stone wall is found, or false.

checkPonnuki

private boolean checkPonnuki(Point center,
                             Color color,
                             Board board)
Determine if at least three sides of a ponnuki of the given color exist around the center point.

Parameters:
center - The point around which the ponnuki would exist.
color - The color of the side for which the ponnuki is being checked.
board - The Board module.
Returns:
true if there is a ponnuki, or false.

checkEmptyPoints

private boolean checkEmptyPoints(ClosestPoints cpts,
                                 ArrayList al,
                                 Color col,
                                 ClosestPointGroup cpg1,
                                 ClosestPointGroup cpg2)
Determine if there are any opponents stones preventing the two groups from combining.

Parameters:
cpts - - The ClosestPoints variable which contains all of the groups.
al - - The stones between which the empty points are being checked.
col - - The color of the groups being checked.
cpg1 - - The first group being checked.
cpg2 - - The other group being checked.
Returns:
true if the empty points are clear of opponents stones, or false.

getStoneDistances

private StoneDistance[] getStoneDistances(Stone[] s1,
                                          Stone[] s2)
Get the distances between any two pair of stones. The array is sorted in ascending distance between the two stones. If there are ties, then the order is undefined.

Parameters:
s1 - - An array of stones.
s2 - - A second array of stones.
Returns:
An array of StoneDistance objects.

checkOpponentsLooseGroups

private void checkOpponentsLooseGroups(Stone stone,
                                       ClosestPointGroup cpg,
                                       Groups groups,
                                       Board board)
                                throws InternalErrorException
Split opponents groups adjacent to the current loose group if necessary. The last move made has either added a new group, added a stone to an existing group, or caused two or more groups to be combined into one. In any case, this may cause an opponents loose group to be split into two or more loose groups. Perform this operation if necessary.

Parameters:
stone - - The last move made.
cpg - - The ClosestPointGroup object which contains the stone.
InternalErrorException

splitGroupIfNecessary

private SingleLGImpl[] splitGroupIfNecessary(Stone stone,
                                             SingleLGImpl candidate,
                                             Groups groups,
                                             Board board)
                                      throws InternalErrorException
Determine if the given loose group should be split up based on the last move.

Parameters:
stone - The last move played.
candidate - The loose group in question.
groups - The Groups module.
board - The Board module.
Returns:
An array of SingleLGImpl objects, each of which should be its own loose group.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

combinePairs

private ClosestPointGroup[] combinePairs(ClosestPoints cpts,
                                         ArrayList al,
                                         Board board,
                                         Groups groups)
                                  throws InternalErrorException
Combine the ClosestPointGroups as much as possible based on criteria for loose groups.

Parameters:
cpts - The appropriate ClosestPoints object.
al - Each element is a ClosestPointGroup objects.
board - The Board module.
groups - The Groups module.
Returns:
An array of ClosestPointGroup objects.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

getAssociatedEdgePoints

public Point[] getAssociatedEdgePoints(SingleLooseGroup slg)
Get the edge points for the given loose group. Conceptually, loose groups near the edge of the board may be thought of as having implicit points on the edge of the board. For example, for a two space extension on the third line, each of the points on the edge below the stones could be thought of as a point belonging to the loose group. This method finds those points, if the loose group has any.

Specified by:
getAssociatedEdgePoints in interface LooseGroups
Parameters:
slg - The loose group in question.
Returns:
An array of Point objects, each of which is an edge point.

toString

public String toString()
Get a String representation of the object.

Overrides:
toString in class Object
Returns:
A String which represents the object.

clone

public Object clone()
Override the Object.clone() method.

Specified by:
clone in interface ModuleInterface
Overrides:
clone in class Module
Returns:
An LGImpl object which is a clone of this one.