org.moyoman.module.ladder.ladderimpl
Class LadderImpl

java.lang.Object
  |
  +--org.moyoman.module.Module
        |
        +--org.moyoman.module.ladder.ladderimpl.LadderImpl
All Implemented Interfaces:
Cloneable, Component, Ladder, ModuleInterface, Serializable

public class LadderImpl
extends Module
implements Ladder

This module implements the Ladder interface. This module is meant to be both a working Ladder module and a demonstration of how to write a search based module.

Currently, this module is stateless, that is, ladders are read out after each move even though the last move may not have changed ladders which were already present. Caching of results should result in a marked performance improvement.

See Also:
Serialized Form

Field Summary
private static DebugType[] dt
          The debug types which this module produces.
private  boolean isGenerated
          This is true after generateMove() but before makeMove() is called.
private  Board lastBoard
          The current Board module.
private  Groups lastGroups
          The current Groups module.
private  MoveDescriptorForest mdf
          The sequence of best play for the various ladders on the board.
private static ModuleType[] mt
          The modules upon which this module depends.
private  HashMap results
          The key is a SingleGroup object, and the value is a LadderInfo object.
private  RatedMove[] rm
          The moves suggested by this module.
 
Fields inherited from class org.moyoman.module.Module
 
Fields inherited from interface org.moyoman.module.ladder.Ladder
CONFIDENCE
 
Constructor Summary
LadderImpl(GameId id, ModuleName name)
          Create a new LadderImpl object.
 
Method Summary
private  void calculateLadder(Scheduler sch, MoveDescriptorForest mdf, LadderInfo li, SingleGroup sg, MoveDescriptor last)
          This method adds two moves to a ladder per invocation.
private  LadderInfo calculateLadder(SingleGroup sg)
          Calculate ladder information for a single group.
private  Set calculateLadderBreakers(SingleGroup sg, MoveDescriptorForest mdf)
          Find all moves which are ladder breakers for the given group.
private  Set calculateLadderThreats(SingleGroup sg, MoveDescriptorForest mdf)
          Find all moves which are ladder breakers for the given group.
 boolean canGroupPotentiallyBeCaught(SingleGroup sg)
          Return true if the group is in a potential ladder and would be caught if the opponent moves first.
 Object clone()
          Clone the module.
 Stone[] createLadder(SingleGroup sg)
          Get the stones which would put the group into a ladder.
 void generateMove(Module[] modules)
          Generate moves based on the ladders and potential ladders on the board.
 Debug[] getDebugInformation(DebugType[] types)
          Return Debug objects based on the current analysis of the board.
 DebugType[] getDebugTypes()
          Get the debug types that this module supports.
 Set getLadderBreakers(SingleGroup sg)
          Get all stones which would break the ladder on the group and allow it to escape.
 Set getLadderThreats(SingleGroup sg)
          Get all stones which would threaten to capture the ladder.
 RatedMove[] getMoves()
          Get the moves rated by this module.
private  short getPathLength(MoveDescriptorForest forest, MoveDescriptor md, short len)
          Return the shortest path length of the ladder from this point.
 ModuleType[] getRequiredModuleList()
          Get the module types used by this module.
 SingleGroup[] groupsInLadder()
          Get all of the groups which are currently in a ladder.
 SingleGroup[] groupsInPotentialLadder()
          Get all of the groups which could be put in a ladder if the opponent moves next.
 boolean isGroupCaught(SingleGroup sg)
          Determine if the group in the ladder can be captured.
 boolean isInLadder(SingleGroup sg)
          Determine if the group is in a ladder.
 boolean isInPotentialLadder(SingleGroup sg)
          Determine if the group is in a potential ladder.
private  int libCount(Point pt, Color escapeColor)
           
 void makeMove(Move move, Module[] modules)
          Update this module with the current move.
private  void onePairAnalysis(Point attackerPt, Point escaperPt, Scheduler sch, MoveDescriptorForest mdf, LadderInfo li, SingleGroup sg, MoveDescriptor last)
          Add two moves to the ladder.
private  SingleGroup refresh(SingleGroup sg)
          Return the corresponding SingleGroup object from the current lastGroups.
 short totalMovesToCapture(SingleGroup sg)
          Return the total number of moves to capture the group.
 
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, toString, wait, wait, wait
 
Methods inherited from interface org.moyoman.module.ModuleInterface
getScheduler
 

Field Detail

mt

private static ModuleType[] mt
The modules upon which this module depends.


dt

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


rm

private RatedMove[] rm
The moves suggested by this module.


isGenerated

private boolean isGenerated
This is true after generateMove() but before makeMove() is called.


lastBoard

private Board lastBoard
The current Board module.


lastGroups

private Groups lastGroups
The current Groups module.


results

private HashMap results
The key is a SingleGroup object, and the value is a LadderInfo object.


mdf

private MoveDescriptorForest mdf
The sequence of best play for the various ladders on the board.

Constructor Detail

LadderImpl

public LadderImpl(GameId id,
                  ModuleName name)
           throws InternalErrorException
Create a new LadderImpl object.

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

refresh

private SingleGroup refresh(SingleGroup sg)
                     throws NoSuchDataException
Return the corresponding SingleGroup object from the current lastGroups.

Parameters:
sg - A SingleGroup object.
Returns:
A SingleGroup object corresponding to the sg parameter which is contained by lastGroups.
Throws:
NoSuchDataException - Thrown if there is no single group corresponding to the sg parameter.

isInLadder

public boolean isInLadder(SingleGroup sg)
Determine if the group is in a ladder.

Specified by:
isInLadder in interface Ladder
Parameters:
sg - The SingleGroup in question.
Returns:
true if the group is in a ladder, or false.

isInPotentialLadder

public boolean isInPotentialLadder(SingleGroup sg)
Determine if the group is in a potential ladder. A potential ladder is defined as a situation where the opponent can put the group in a ladder if a stone is not added to the group.

Specified by:
isInPotentialLadder in interface Ladder
Parameters:
sg - The SingleGroup in question.
Returns:
true if the group is in a potential ladder, or false.

groupsInLadder

public SingleGroup[] groupsInLadder()
Get all of the groups which are currently in a ladder. It does not matter if the ladders work or not.

Specified by:
groupsInLadder in interface Ladder
Returns:
An array of SingleGroup objects.

groupsInPotentialLadder

public SingleGroup[] groupsInPotentialLadder()
Get all of the groups which could be put in a ladder if the opponent moves next. It does not matter if the ladders would work or not.

Specified by:
groupsInPotentialLadder in interface Ladder
Returns:
An array of SingleGroup objects.

isGroupCaught

public boolean isGroupCaught(SingleGroup sg)
                      throws IllegalArgumentException
Determine if the group in the ladder can be captured.

Specified by:
isGroupCaught in interface Ladder
Returns:
true if the group is caught, or false if it can escape.
Throws:
IllegalArgumentException - Thrown if isInLadder(sg) would return false.

canGroupPotentiallyBeCaught

public boolean canGroupPotentiallyBeCaught(SingleGroup sg)
Return true if the group is in a potential ladder and would be caught if the opponent moves first. The method returns false otherwise, even if the group is actually in a ladder.

Specified by:
canGroupPotentiallyBeCaught in interface Ladder
Parameters:
sg - The SingleGroup in question.
Returns:
true if the group is in a potential ladder and can be caught.

getLadderBreakers

public Set getLadderBreakers(SingleGroup sg)
                      throws IllegalArgumentException
Get all stones which would break the ladder on the group and allow it to escape. These stones are returned regardless of whether the ladder currently works or not.

Specified by:
getLadderBreakers in interface Ladder
Parameters:
sg - The group in question.
Returns:
An array of Stone objects.
Throws:
IllegalArgumentException - Thrown if isInLadder(sg) would return false.

getLadderThreats

public Set getLadderThreats(SingleGroup sg)
                     throws IllegalArgumentException
Get all stones which would threaten to capture the ladder. These stones are returned regardless of whether the ladder currently works or not.

Specified by:
getLadderThreats in interface Ladder
Parameters:
sg - The group in question.
Returns:
An array of Stone objects.
Throws:
IllegalArgumentException - Thrown if isInLadder(sg) would return false.

createLadder

public Stone[] createLadder(SingleGroup sg)
                     throws IllegalArgumentException
Get the stones which would put the group into a ladder. The group must already be in a potential ladder, so playing any of the stones returned would put the group in a ladder.

Specified by:
createLadder in interface Ladder
Parameters:
sg - The group in question.
Returns:
An array of Stone objects.
Throws:
IllegalArgumentException - Thrown if the group is not in a potential ladder.

totalMovesToCapture

public short totalMovesToCapture(SingleGroup sg)
                          throws IllegalArgumentException
Return the total number of moves to capture the group. The count includes moves by both sides. If there are branches, then the smallest number of moves which results in a capture is used.

Specified by:
totalMovesToCapture in interface Ladder
Parameters:
sg - The group in question.
Returns:
A short which is the smallest number of moves to capture.
Throws:
IllegalArgumentException - Thrown if the group is not in a ladder or potential ladder, or if the group cannot be captured.

getPathLength

private short getPathLength(MoveDescriptorForest forest,
                            MoveDescriptor md,
                            short len)
                     throws IllegalArgumentException
Return the shortest path length of the ladder from this point. This method calls itself recursively to walk the ladder.

Parameters:
forest - The sequences of the ladder.
md - The current position of the ladder being analyzed.
len - The length of the ladder to this position.
Returns:
The shortest path through the ladder.
Throws:
IllegalArgumentException - Thrown if the group is not in a ladder or potential ladder, or if the group cannot be captured.

generateMove

public void generateMove(Module[] modules)
                  throws InternalErrorException
Generate moves based on the ladders and potential ladders on the board.

Specified by:
generateMove in class Module
Parameters:
modules - The modules which this module uses.
Throws:
InternalErrorException - Thrown if the module fails for any reason.

getDebugInformation

public Debug[] getDebugInformation(DebugType[] types)
Return Debug objects based on the current analysis of the board. The following debug objects are returned:

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

getDebugTypes

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

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

getMoves

public RatedMove[] getMoves()
Get the moves rated by this module.

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 used by this module.

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

makeMove

public void makeMove(Move move,
                     Module[] modules)
              throws InternalErrorException
Update this module with the current move.

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

calculateLadder

private LadderInfo calculateLadder(SingleGroup sg)
Calculate ladder information for a single group. This method calculates the information from scratch, without using any information from previous stages of the game.

Parameters:
sg - The SingleGroup in question.
Returns:
a LadderInfo object containing information about this group, or null if the group is not in a ladder.

calculateLadder

private void calculateLadder(Scheduler sch,
                             MoveDescriptorForest mdf,
                             LadderInfo li,
                             SingleGroup sg,
                             MoveDescriptor last)
This method adds two moves to a ladder per invocation. It and onePairAnalysis() recursively call each other until the ladder is completely read out. On entry, the single group has two or less liberties, and it is the attackers turn to move. If either or both of the two possible ataris followed by the escaper playing on the other liberty results in the single group having 2 or less liberties, then it is added to the sequence of moves. Sequences terminate upon the escaping group being captured or having 3 or more liberties.

Parameters:
sch - The Scheduler which represents the state of the board being analyzed.
mdf - The sequences of moves found so far.
li - The LadderInfo object with the results so far.
sg - The single group which is trying to be caught in the ladder.

onePairAnalysis

private void onePairAnalysis(Point attackerPt,
                             Point escaperPt,
                             Scheduler sch,
                             MoveDescriptorForest mdf,
                             LadderInfo li,
                             SingleGroup sg,
                             MoveDescriptor last)
Add two moves to the ladder. On entry, the group under attack has two liberties and it is the attackers turn.

Parameters:
escaperPt - The point on which the escaper is to play after the attackers move.
sch - The Scheduler object representing the current position being analyzed.
mdf - The sequence of moves in the ladder.
li - The LadderInfo object.
sg - The group under attack.
last - The last position analyzed so far.

calculateLadderBreakers

private Set calculateLadderBreakers(SingleGroup sg,
                                    MoveDescriptorForest mdf)
Find all moves which are ladder breakers for the given group. Ladder breakers are found up until the ladder branches. Once the ladder branches, no moves are returned from beyond that point. This method does not currently find all ladder breakers, but finds the vast majority of them.

Parameters:
sg - The group in question.
mdf - The sequences of moves for the ladder.
Returns:
A Set which contains all of the stones which are ladder breakers.

libCount

private int libCount(Point pt,
                     Color escapeColor)

calculateLadderThreats

private Set calculateLadderThreats(SingleGroup sg,
                                   MoveDescriptorForest mdf)
Find all moves which are ladder breakers for the given group.

Parameters:
sg - The group in question.
mdf - The sequences of moves for the ladder.
Returns:
A Set which contains all of the stones which are ladder breakers.

clone

public Object clone()
Description copied from interface: ModuleInterface
Clone the module. This method overrides the one in Object. Cloning modules is an important requirement for the system to function correctly. The derived class of Module must override this method. It may be as straightforward as calling super.clone(). Consult any Java textbook for more details. If you are not sure, ask for help.

Specified by:
clone in interface ModuleInterface
Overrides:
clone in class Module
Returns:
The cloned Module object.