org.moyoman.module.geometry.geometryimpl
Class GeometryImpl

java.lang.Object
  |
  +--org.moyoman.module.Module
        |
        +--org.moyoman.module.geometry.geometryimpl.GeometryImpl
All Implemented Interfaces:
Cloneable, Component, Geometry, ModuleInterface, Serializable

public class GeometryImpl
extends Module
implements Geometry

Compute geometric information about the board, such as the set of empty points which are closer to a group than any other, which groups are adjacent to which other groups, and the convex hull of each group.

See Also:
Serialized Form

Field Summary
private  Board board
          The Board module.
private  ClosestPoints cp
          This class helps in computing geometrical information.
private static DebugType[] dt
          The debug types that this module generates.
(package private)  GeometryMoveGenerator gmg
          This is used to generate the move.
private  boolean isGenerated
          If true, then the last move was generated.
private  LooseGroups lg
          The LooseGroups module.
private static ModuleType[] mt
          The module types that this module is dependent on.
private  RatedMove[] ratedMoves
          The rated moves that generateMove() generates.
 
Fields inherited from class org.moyoman.module.Module
 
Fields inherited from interface org.moyoman.module.geometry.Geometry
CONFIDENCE
 
Constructor Summary
GeometryImpl(GameId id, ModuleName name)
          Create a new GeometryImpl object.
 
Method Summary
 boolean areLooseGroupsAdjacent(SingleLooseGroup slg1, SingleLooseGroup slg2)
          Determine if the two loose groups are adjacent.
private  int calculateTotalGroups(Color col, LooseGroups lg)
           
private  boolean checkEdge(Point pt, Point[] points)
          Check if the point in question is on an edge of the polygon.
 Object clone()
          Override the Object.clone() method.
protected  int computeDistance(Point pt1, Point pt2)
          Compute the distance between the two points.
protected  void error(Exception e)
          Provide a method for logging errors for other classes in this package.
 void generateMove(Module[] modules)
          Generate moves based on geometric analysis.
 SingleLooseGroup[] getAdjacentGroups(SingleLooseGroup slg)
          Get all of the groups which are adjacent to the specified group.
 Point[] getBorderPointsBetweenGroups(SingleLooseGroup slg1, SingleLooseGroup slg2)
          Get the points midway between the two loose groups.
 Point[] getClosestEmptyPoints(SingleLooseGroup slg)
          Get the empty points that are as close to the specified group as any other.
 HashMap getClosestStones(SingleLooseGroup slg1, SingleLooseGroup slg2)
          Find pairs of stones from the two loose groups that are closer than any others.
 Point[] getClosestUniqueEmptyPoints(SingleLooseGroup slg)
          Get the empty points that are closer to the specified group than any other.
 ConvexHull getConvexHull(Point[] points)
          Get the convex hull for the specified points.
 ConvexHull getConvexHull(SingleLooseGroup slg)
          Return the convex hull of the specified group.
 ConvexHull getConvexHullByColor(Stone stone)
          Get the convex hull for all of the adjacent stones of the same color as the specified stone.
 ConvexHull getConvexHullWithEdgePoints(SingleLooseGroup slg)
          Return the convex hull of the specified group, including implied edge points.
 Debug[] getDebugInformation(DebugType[] types)
          Get the debug information for this module.
 DebugType[] getDebugTypes()
          Get the debug types that this module produces.
 short getDistanceToClosestStone(Point pt)
          Determine the distance from the given point to the nearest stone.
 SingleLooseGroup[] getEnclosedGroups(SingleLooseGroup slg)
          Return all of the groups which are enclosed by the specified group.
 RatedMove[] getMoves()
          Get the rated moves that this module generates.
 Point[] getPointsBetweenGroups(SingleLooseGroup slg1, SingleLooseGroup slg2)
          Get the empty points between the two adjacent loose groups.
 ModuleType[] getRequiredModuleList()
          Get the module types that this module requires.
 SingleLooseGroup[] getSingleLooseGroups(Point pt)
          Get the loose groups which are closest to the given point.
 int getTotalClosestEmptyPoints(SingleLooseGroup slg)
          Get the number of points which are closer to this group than any other.
 float getTotalUniqueClosestEmptyPoints(SingleLooseGroup slg)
          Get the number of points which are closer to this group than any other.
 void information(String msg)
          Provide a method for logging messages for other classes in this package.
 boolean isAdjacentToCorner(SingleLooseGroup slg)
          Determine if the loose group is adjacent to any of the corners of the board.
 boolean isAdjacentToSide(SingleLooseGroup slg)
          Determine if the loose group is adjacent to any of the sides of the board.
 void makeMove(Move move, Module[] modules)
          Update this module with the last move made.
 
Methods inherited from class org.moyoman.module.Module
checkTime, create, createHelper, debug, error, fatal, fatal, getAllHelpers, getHelper, getId, getKomi, getModule, getModuleName, getScheduler, 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 module types that this module is dependent on.


dt

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


ratedMoves

private RatedMove[] ratedMoves
The rated moves that generateMove() generates.


cp

private ClosestPoints cp
This class helps in computing geometrical information.


board

private Board board
The Board module.


lg

private LooseGroups lg
The LooseGroups module.


isGenerated

private boolean isGenerated
If true, then the last move was generated.


gmg

GeometryMoveGenerator gmg
This is used to generate the move.

Constructor Detail

GeometryImpl

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

Parameters:
id - The id of the game.
name - The name of this module.
Throws:
InternalErrorException - Thrown if an error occurs for any reason.
Method Detail

getAdjacentGroups

public SingleLooseGroup[] getAdjacentGroups(SingleLooseGroup slg)
Get all of the groups which are adjacent to the specified group. The definition of adjacent is that, for the given group, if any of its stones or any of the empty points which are closer to it than to the stones of any other group, are adjacent to the stones or empty points of another group, then the two groups are adjacent.

Specified by:
getAdjacentGroups in interface Geometry
Parameters:
slg - The SingleLooseGroup in question.
Returns:
An array of SingleLooseGroup objects.

getEnclosedGroups

public SingleLooseGroup[] getEnclosedGroups(SingleLooseGroup slg)
Return all of the groups which are enclosed by the specified group. A group is considered to be enclosed by the specified group if a call to getAdjacentGroups() for the enclosed group would return an array size of 1, which is the specified group, and if the bounding box for the enclosed group is within the bounding box for the enclosing group.

Specified by:
getEnclosedGroups in interface Geometry
Parameters:
slg - The SingleLooseGroup in question.
Returns:
An array of SingleLooseGroup objects.

getConvexHull

public ConvexHull getConvexHull(SingleLooseGroup slg)
Return the convex hull of the specified group. A convex hull is a standard geometrical concept. Basically, if a wooden board had pins placed in it that represented the stones, and a rubber band was stretched so that all of the pins were inside, the shape that the rubber band assumes is the convex hull. See the book, Algorithms, by Robert Sedgewick for more details.

Specified by:
getConvexHull in interface Geometry
Parameters:
slg - The SingleLooseGroup in question.
Returns:
An array of Stone objects.

getConvexHullWithEdgePoints

public ConvexHull getConvexHullWithEdgePoints(SingleLooseGroup slg)
Return the convex hull of the specified group, including implied edge points. A convex hull is a standard geometrical concept. Basically, if a wooden board had pins placed in it that represented the stones, and a rubber band was stretched so that all of the pins were inside, the shape that the rubber band assumes is the convex hull. See the book, Algorithms, by Robert Sedgewick for more details.

In addition to using the stones in the loose group, points on the edge are also used if they are associated with the group. This is determined by the loose groups module.

Specified by:
getConvexHullWithEdgePoints in interface Geometry
Parameters:
slg - The SingleLooseGroup in question.
Returns:
An array of Point objects.

getConvexHull

public ConvexHull getConvexHull(Point[] points)
Description copied from interface: Geometry
Get the convex hull for the specified points.

Specified by:
getConvexHull in interface Geometry
Returns:
A ConvexHull object.

getConvexHullByColor

public ConvexHull getConvexHullByColor(Stone stone)
                                throws NoSuchDataException
Description copied from interface: Geometry
Get the convex hull for all of the adjacent stones of the same color as the specified stone.

Specified by:
getConvexHullByColor in interface Geometry
Parameters:
stone - The specified stone.
Returns:
A ConvexHull object.
Throws:
NoSuchDataException - Thrown if the stone is not on the board.

getDistanceToClosestStone

public short getDistanceToClosestStone(Point pt)
Determine the distance from the given point to the nearest stone.

Specified by:
getDistanceToClosestStone in interface Geometry
Parameters:
pt - The point in question.
Returns:
The distance to the nearest stone, which is the square of the euclidean distance.

checkEdge

private boolean checkEdge(Point pt,
                          Point[] points)
                   throws IllegalArgumentException
Check if the point in question is on an edge of the polygon.

Parameters:
pt - The point in question.
points - The vertices of the polygon.
Returns:
true if the point is on an edge of the polygon, or false.
Throws:
IllegalArgumentException - Thrown if the point is not on an edge.

getClosestEmptyPoints

public Point[] getClosestEmptyPoints(SingleLooseGroup slg)
Get the empty points that are as close to the specified group as any other. The distance of an empty point from a group is the distance to the closest stone in that group. If the distance to a given group is less than or equal to that of any other group, then the point is considered to be closest to that group. Note that by this definition, an empty point may be part of the array returned by this method for more than one group.

Specified by:
getClosestEmptyPoints in interface Geometry
Parameters:
slg - The loose group in question.
Returns:
An array of Point objects which are as close to this group as any other.

getClosestUniqueEmptyPoints

public Point[] getClosestUniqueEmptyPoints(SingleLooseGroup slg)
Get the empty points that are closer to the specified group than any other. The points returned by this method are a subset of those returned by getClosestEmptyPoints().

Specified by:
getClosestUniqueEmptyPoints in interface Geometry
Parameters:
slg - The loose group in question.
Returns:
An array of Point objects which are closer to this group than any other.

getPointsBetweenGroups

public Point[] getPointsBetweenGroups(SingleLooseGroup slg1,
                                      SingleLooseGroup slg2)
                               throws IllegalArgumentException
Get the empty points between the two adjacent loose groups.

Specified by:
getPointsBetweenGroups in interface Geometry
Parameters:
slg1 - One of the loose groups.
slg2 - The other loose group.
Returns:
An array of Point objects which are empty points between the loose groups.
Throws:
IllegalArgumentException - Thrown if the two loose groups are not adjacent.

getBorderPointsBetweenGroups

public Point[] getBorderPointsBetweenGroups(SingleLooseGroup slg1,
                                            SingleLooseGroup slg2)
                                     throws IllegalArgumentException
Get the points midway between the two loose groups. This method returns a subset of the method getPointsBetweenGroups().

Specified by:
getBorderPointsBetweenGroups in interface Geometry
Parameters:
slg1 - One of the loose groups.
slg2 - The other loose group.
Returns:
An array of Point objects which are empty points midway between the loose groups.
Throws:
IllegalArgumentException - Thrown if the two loose groups are not adjacent.

computeDistance

protected int computeDistance(Point pt1,
                              Point pt2)
Compute the distance between the two points. This is the square of the euclidean distance, and so should just be used for comparison of distances.

Parameters:
pt1 - A point.
pt2 - The other point.
Returns:
An int which is the square of the distance between the two points.

getClosestStones

public HashMap getClosestStones(SingleLooseGroup slg1,
                                SingleLooseGroup slg2)
Find pairs of stones from the two loose groups that are closer than any others. It is possible that more than one pair of stones is equally close, so all equally close pairs are returned.

Specified by:
getClosestStones in interface Geometry
Parameters:
slg1 - A loose group
slg2 - Another loose group
Returns:
A HashMap where the key is a Stone from group 1, and the value is a Stone from group 2.

getSingleLooseGroups

public SingleLooseGroup[] getSingleLooseGroups(Point pt)
Get the loose groups which are closest to the given point.

Specified by:
getSingleLooseGroups in interface Geometry
Parameters:
pt - The point in question.
Returns:
An array of SingleLooseGroup objects which are closest to the point.

getTotalUniqueClosestEmptyPoints

public float getTotalUniqueClosestEmptyPoints(SingleLooseGroup slg)
Get the number of points which are closer to this group than any other. Since some points may be shared by more than one group, the return value is a float. If a point is shared by two groups, then it counts as a half, if by three groups, then a third, and so on.

Specified by:
getTotalUniqueClosestEmptyPoints in interface Geometry
Parameters:
slg - The SingleLooseGroup in question.
Returns:
A float which is the total number of closest empty points.

getTotalClosestEmptyPoints

public int getTotalClosestEmptyPoints(SingleLooseGroup slg)
Get the number of points which are closer to this group than any other. This value is just the size of the array returned by getClosestEmptyPoints(). Each point is counted once, even if it is shared with other groups.

Specified by:
getTotalClosestEmptyPoints in interface Geometry
Parameters:
slg - The SingleLooseGroup in question.
Returns:
An int which is the total number of empty points closest to this group.

isAdjacentToSide

public boolean isAdjacentToSide(SingleLooseGroup slg)
Determine if the loose group is adjacent to any of the sides of the board. The loose group is considered to be adjacent to an edge if either at least one of its stones touches an edge, or if a point which is closest to this group is on an edge.

Specified by:
isAdjacentToSide in interface Geometry
Parameters:
slg - The loose group.
Returns:
true if the loose group is adjacent to a side of the board, or false.

isAdjacentToCorner

public boolean isAdjacentToCorner(SingleLooseGroup slg)
Determine if the loose group is adjacent to any of the corners of the board. The loose group is considered to be adjacent to the corner if either at least one of its stones is in the corner, or if an empty point which is closest to this group is in the corner.

Specified by:
isAdjacentToCorner in interface Geometry
Parameters:
slg - The loose group.
Returns:
true if the loose group is adjacent to a corner of the board, or false.

areLooseGroupsAdjacent

public boolean areLooseGroupsAdjacent(SingleLooseGroup slg1,
                                      SingleLooseGroup slg2)
Determine if the two loose groups are adjacent. The groups are considered to be adjacent if any of the stones or closest empty points of the two groups are the same or adjacent.

Specified by:
areLooseGroupsAdjacent in interface Geometry
Parameters:
slg1 - One of the loose groups.
slg2 - The other loose group.
Returns:
true if the two loose groups are adjacent, or false.

generateMove

public void generateMove(Module[] modules)
Generate moves based on geometric analysis.

Specified by:
generateMove in class Module
Parameters:
modules - The modules required by this module.

calculateTotalGroups

private int calculateTotalGroups(Color col,
                                 LooseGroups lg)

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 process.
Returns:
An array of Debug objects which is the debug information.

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 generates.

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 this module with the last move made.

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

error

protected void error(Exception e)
Provide a method for logging errors for other classes in this package.

Overrides:
error in class Module
Parameters:
e - The exception to be logged.

information

public void information(String msg)
Provide a method for logging messages for other classes in this package.

Overrides:
information in class Module
Parameters:
msg - The message to log.

clone

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

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