org.moyoman.util.closestpoint
Class ClosestPoints

java.lang.Object
  |
  +--org.moyoman.util.closestpoint.ClosestPoints
All Implemented Interfaces:
Cloneable, Serializable

public class ClosestPoints
extends Object
implements Serializable, Cloneable

This class manages groups of stones, and certain properties of those groups. For each group of stones, it determines which empty points are closest to it, and which other groups are adjacent to it.

See Also:
Serialized Form

Field Summary
private  HashMap adjacencyInfo
          The key is a ClosestPointGroup object, and the value is an ArrayList, which contains ClosestPointGroup objects.
private  ArrayList cpg
          This contains all of the ClosestPointGroup objects.
private  HashMap pointInfo
          The key is a Point object, and the value is a PointInfo object.
private  HashMap stones
          The key is a Stone object, and the value is a ClosestPointGroup object.
 
Constructor Summary
ClosestPoints()
          Create the ClosestPoints object.
 
Method Summary
 void addStone(ClosestPointGroup currcpg, Stone st)
          Add a stone to an existing closest point group.
private  void clearAdjacencyInfo()
          Remove all cached adjacency info.
 Object clone()
          Override the Object.clone() method.
 void combineGroups(ClosestPointGroup[] cpgarr)
          Combine two or more groups into one group.
private  void computeAdjacencyRelationship(ClosestPointGroup currcpg)
          Find the adjacent groups to the specified one, and update the adjacencyInfo variable.
private  Stone[] findClosestStones(Point pt)
          Find the closest stones to the given point.
 ClosestPointGroup[] getAdjacentGroups(ClosestPointGroup currcpg)
          Get all of the groups adjacent to a given group.
private  Point[] getAdjacentPoints(Point pt)
          Get all of the points which are adjacent to the point in question.
private  Point[] getConcentricEmptyPoints(Point pt, int distance)
          Get the empty points that form a square around the point at the specified distance.
 short getDistance(Point pt)
          Return the square of the distance to the closest stone.
 ClosestPointGroup[] getEnclosedGroups(ClosestPointGroup currcpg)
          Get all of the groups enclosed by a given group.
 double getExactDistance(Point pt)
          Return the exact distance to the closest stone.
 ClosestPointGroup getGroup(Stone st)
          Get the ClosestPointGroup that contains the given stone.
 ClosestPointGroup getGroupNoException(Stone st)
          Get the ClosestPointGroup that contains the given stone.
 ClosestPointGroup[] getGroups()
          Get all of the groups.
 ClosestPointGroup[] getGroups(Point pt)
          Get all of the groups which contain the given point.
 HashMap getPointInfo()
          Get the mapping from Point to PointInfo objects.
 int getTotalPoints(ClosestPointGroup cpgrp)
          Get the number of empty points as close to or closer to this group than any other.
 float getTotalUniquePoints(ClosestPointGroup cpgrp)
          Get the total number of unique points for a group.
 Point[] getUniquePoints(ClosestPointGroup cpgrp)
          Get the points which are closer to this group than any other.
 boolean isEmptyPoint(Point pt)
          Determine if the point is an empty point.
 boolean isStone(Point pt)
          Determine if the point contains a stone.
 void newGroup(Stone st)
          Add a stone to its own closest point group.
private  void recomputePoint(Point pt)
          Recompute the PointInfo information for a given point.
private  void recomputePointInfo(ClosestPointGroup oldcpg)
          Remove any information from the PointInfo objects about the old group.
private  ClosestPointGroup refresh(ClosestPointGroup cpgrp)
          This method returns the corresponding ClosestPointGroup object.
 void removeGroup(ClosestPointGroup oldcpg)
          Remove the given group.
private  void removeGroupInfo(ClosestPointGroup oldcpg)
          Remove the group from the global list.
 void removeStones(ClosestPointGroup currcpg, Stone[] starr)
          Remove stones from the given group.
private  void updateEmptyPoints(Stone st, ClosestPointGroup currcpg)
          Update the PointInfo objects based on the last stone added.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cpg

private ArrayList cpg
This contains all of the ClosestPointGroup objects.


stones

private HashMap stones
The key is a Stone object, and the value is a ClosestPointGroup object.


pointInfo

private HashMap pointInfo
The key is a Point object, and the value is a PointInfo object.


adjacencyInfo

private HashMap adjacencyInfo
The key is a ClosestPointGroup object, and the value is an ArrayList, which contains ClosestPointGroup objects.

Constructor Detail

ClosestPoints

public ClosestPoints()
Create the ClosestPoints object.

Method Detail

newGroup

public void newGroup(Stone st)
              throws DataAlreadyExistsException
Add a stone to its own closest point group.

Parameters:
st - The stone.
Throws:
DataAlreadyExistsException - Thrown if the stone is already in a group.

updateEmptyPoints

private void updateEmptyPoints(Stone st,
                               ClosestPointGroup currcpg)
Update the PointInfo objects based on the last stone added.

Parameters:
st - The stone that was added.
currcpg - The group that the stone belongs to.

getDistance

public short getDistance(Point pt)
                  throws NoSuchDataException
Return the square of the distance to the closest stone.

Parameters:
pt - The empty point for which the distance is being retrieved.
Returns:
A short, which is the square of the distance to the closest stone.
Throws:
NoSuchDataException - Thrown if the point is not an empty point.

getExactDistance

public double getExactDistance(Point pt)
                        throws NoSuchDataException
Return the exact distance to the closest stone.

Parameters:
pt - The empty point for which the distance is being retrieved.
Returns:
A double, which is the distance to the closest stone.
Throws:
NoSuchDataException - Thrown if the point is not an empty point.

getConcentricEmptyPoints

private Point[] getConcentricEmptyPoints(Point pt,
                                         int distance)
Get the empty points that form a square around the point at the specified distance.

Parameters:
pt - The point in question.
distance - The radius of the square.
Returns:
An array of Point objects.

addStone

public void addStone(ClosestPointGroup currcpg,
                     Stone st)
              throws DataAlreadyExistsException
Add a stone to an existing closest point group.

Parameters:
currcpg - The existing ClosestPointGroup object.
st - The stone to be added to the group.
Throws:
DataAlreadyExistsException - Thrown if the stone is already in a group.

getTotalUniquePoints

public float getTotalUniquePoints(ClosestPointGroup cpgrp)
Get the total number of unique points for a group. This number is calculated as follows: add one for each empty point only contained by the selected group. If a point is shared by two groups, add one half, by three groups, add one third, and by four groups, add one quarter. This number does not include the stones in the group. Note that the total number of points for all groups should be equal to 361 minus the number of stones on the board.

Parameters:
cpgrp - The ClosestPointGroup in question.
Returns:
The total number of unique points.

getUniquePoints

public Point[] getUniquePoints(ClosestPointGroup cpgrp)
Get the points which are closer to this group than any other. Points which are equally close to this group and other groups are not returned.

Parameters:
cpgrp - THe ClosestPointGroup in question.
Returns:
An array of Point objects.

getTotalPoints

public int getTotalPoints(ClosestPointGroup cpgrp)
Get the number of empty points as close to or closer to this group than any other.

Parameters:
cpgrp - THe ClosestPointGroup in question.
Returns:
The total number of points.

computeAdjacencyRelationship

private void computeAdjacencyRelationship(ClosestPointGroup currcpg)
Find the adjacent groups to the specified one, and update the adjacencyInfo variable. This method is used to recompute the adjacency relationships for the given group. Lazy evaluation is used, so this method isn't called until the results are needed, and after it is needed the results are cached.

Parameters:
currcpg - The ClosestPointGroup in question.

isEmptyPoint

public boolean isEmptyPoint(Point pt)
Determine if the point is an empty point.

Parameters:
pt - The point in question.
Returns:
true if the point is an empty point, or false if it is a stone.

isStone

public boolean isStone(Point pt)
Determine if the point contains a stone.

Parameters:
pt - The point in question.
Returns:
true if there is a stone on the point, or false if it is an empty point.

refresh

private ClosestPointGroup refresh(ClosestPointGroup cpgrp)
This method returns the corresponding ClosestPointGroup object. Because of issues involving cloning, the cpgrp object may not be one of the ones contained by the cpg variable. This method finds the ClosestPointGroup object which contains the same stones, and returns that.

It is not clear if the need for this method is a normal result of cloning, or due to a bug. This should be investigated.

Parameters:
cpgrp - The ClosestPointGroup object.
Returns:
The valid ClosestPointGroup object.

clearAdjacencyInfo

private void clearAdjacencyInfo()
Remove all cached adjacency info.


getAdjacentPoints

private Point[] getAdjacentPoints(Point pt)
Get all of the points which are adjacent to the point in question. Points are returned regardless of whether they contain a stone or not.

Parameters:
pt - The point in question.
Returns:
An array of Point objects.

getPointInfo

public HashMap getPointInfo()
Get the mapping from Point to PointInfo objects.

Returns:
A HashMap where the key is a Point and the value is a PointInfo object.

combineGroups

public void combineGroups(ClosestPointGroup[] cpgarr)
Combine two or more groups into one group.


removeStones

public void removeStones(ClosestPointGroup currcpg,
                         Stone[] starr)
Remove stones from the given group. Points belonging to the group have to be recomputed.

Parameters:
currcpg - - The ClosestPointGroup from which the stones are to be removed.

removeGroupInfo

private void removeGroupInfo(ClosestPointGroup oldcpg)
Remove the group from the global list.

Parameters:
oldcpg - The group to be removed.

removeGroup

public void removeGroup(ClosestPointGroup oldcpg)
Remove the given group.

Parameters:
oldcpg - The group to be removed.

recomputePointInfo

private void recomputePointInfo(ClosestPointGroup oldcpg)
Remove any information from the PointInfo objects about the old group.

Parameters:
oldcpg - The group to be removed.

recomputePoint

private void recomputePoint(Point pt)
Recompute the PointInfo information for a given point.

Parameters:
pt - The point for which the information is to be recomputed.

findClosestStones

private Stone[] findClosestStones(Point pt)
Find the closest stones to the given point.

Parameters:
pt - The point in question.
Returns:
An array of Stone objects.

getGroups

public ClosestPointGroup[] getGroups(Point pt)
Get all of the groups which contain the given point. A point may be in more than one group. For example, if there are four groups, each of which contains a stone on the 4-4 point, then the star points along the sides are each contained by two groups, and the center star point is contained by all four groups.

Parameters:
pt - The point in question.
Returns:
An array of ClosestPointGroup objects.

getGroup

public ClosestPointGroup getGroup(Stone st)
                           throws NoSuchDataException
Get the ClosestPointGroup that contains the given stone. Unlike an empty point, a stone can only belong to one group.

Parameters:
st - The stone in question.
Returns:
A ClosestPointGroup object.
Throws:
NoSuchDataException - Thrown if there is no group for the stone.

getGroupNoException

public ClosestPointGroup getGroupNoException(Stone st)
Get the ClosestPointGroup that contains the given stone. Unlike an empty point, a stone can only belong to one group. This method is useful when the caller does not necessarily know if a stone exists, and so does not want an exception thrown.

Parameters:
st - The stone in question.
Returns:
A ClosestPointGroup object.

getGroups

public ClosestPointGroup[] getGroups()
Get all of the groups.

Returns:
An array of ClosestPointGroup objects.

getAdjacentGroups

public ClosestPointGroup[] getAdjacentGroups(ClosestPointGroup currcpg)
Get all of the groups adjacent to a given group. Groups are adjacent if there exist at least one point in each group where those two points are adjacent. Thus, if there are four groups, each containing a stone on the 4-4 point, then all four groups are adjacent to each other.

Parameters:
currcpg - The ClosestPointGroup object in question.
Returns:
An array of ClosestPointGroup objects.

getEnclosedGroups

public ClosestPointGroup[] getEnclosedGroups(ClosestPointGroup currcpg)
Get all of the groups enclosed by a given group. A group is considered to enclose another group if it is the only adjacent group to the enclosed group. Note that this does not mean that the enclosed group is completely surrounded, as the edge of the board may partially surround the enclosed group. However, the enclosed groups bounding box must also be enclosed by the enclosing groups bounding box.

Parameters:
currcpg - The ClosestPointGroup object in question.
Returns:
An array of ClosestPointGroup objects.

clone

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

Overrides:
clone in class Object
Returns:
A ClosestPoints object which is a clone of this one.