org.moyoman.util
Class Point

java.lang.Object
  |
  +--org.moyoman.util.Point
All Implemented Interfaces:
Cloneable, Serializable
Direct Known Subclasses:
Stone

public class Point
extends Object
implements Cloneable, Serializable

This class represents one point. It has a horizontal and vertical position between 0 and 18.

See Also:
Serialized Form

Field Summary
private  int horizontal
          The horizontal position of the point 0 - 18
private static Point[][] points
           
private  int vertical
          The vertical position of the point 0 - 18
 
Constructor Summary
protected Point(int x, int y)
          Create the Point object.
 
Method Summary
 Point castToPoint()
          Return a Point object.
 Object clone()
          Clone this object.
 boolean equals(Object o)
          Return true if the object is equal to this Point object.
static Point get(int x, int y)
          Get the specified point object.
 Point[] getAdjacentPoints()
          Get the points adjacent to this one.
 Point[] getConcentricPoints(short distance)
          Get the legal Point objects offset horizontally or vertically by the parameter.
 int getX()
          Return the horizontal position of the point.
 int getY()
          Return the vertical position of the point.
 int hashCode()
          Return a unique value for each point.
 boolean isInCorner()
          Determine if the point is in a corner of the board.
 boolean isOnSide()
          Determine if the point is on a side of the board.
 String toString()
          Get a String representation of this object.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

horizontal

private int horizontal
The horizontal position of the point 0 - 18


vertical

private int vertical
The vertical position of the point 0 - 18


points

private static final Point[][] points
Constructor Detail

Point

protected Point(int x,
                int y)
         throws IllegalArgumentException
Create the Point object.

Parameters:
x - The horizontal position, between 0 and 18.
y - The vertical position, between 0 and 18.
Throws:
IllegalArgumentException - Thrown if the x or y parameters are out of range.
Method Detail

get

public static Point get(int x,
                        int y)
                 throws IllegalArgumentException
Get the specified point object. The Point uses the flyweight design pattern, so new Point objects are not created, but references to them are returned.

Parameters:
x - The horizontal value of the Point.
y - The vertical value of the Point.
Returns:
A Point object.
Throws:
IllegalArgumentException - Thrown if x or y is out of range.

castToPoint

public Point castToPoint()
Return a Point object. Sometimes, it is necessary that an object be of Point, and not a derived class, for example, if it is used as the key to a HashMap. This method takes a Point object or derived class, and returns the corresponding Point object.

Returns:
A Point object.

getX

public int getX()
Return the horizontal position of the point. It can be a value between 0 and 18, where 0 is the left edge, and 18 is the right edge.

Returns:
The horizontal value of the point.

getY

public int getY()
Return the vertical position of the point. It can be a value between 0 and 18, where 0 is the lower edge, and 18 is the upper edge.

Returns:
The vertical value of the point.

getAdjacentPoints

public Point[] getAdjacentPoints()
Get the points adjacent to this one.

Returns:
An array of Point objects.

getConcentricPoints

public Point[] getConcentricPoints(short distance)
Get the legal Point objects offset horizontally or vertically by the parameter. These points would form a square, unless the Point is near one or more of the edges, or the distance is too large.

Parameters:
distance - The distance to offset the points.
Returns:
An array of Point objects.

isOnSide

public boolean isOnSide()
Determine if the point is on a side of the board.

Returns:
true if the point is on a side, or false.

isInCorner

public boolean isInCorner()
Determine if the point is in a corner of the board.

Returns:
true if the point is a corner, or false.

hashCode

public int hashCode()
Return a unique value for each point. Two Point objects will have the same hashcode if and only if they have the same x and y values. Since there is only one Point object ever created for a given x and y value, it is not strictly necessary to override this method. However, derived classes that do not use the flyweight pattern may find this method useful.

Overrides:
hashCode in class Object
Returns:
An int which is the hash code for this object.

equals

public boolean equals(Object o)
Return true if the object is equal to this Point object. The two objects are considered equal if the object is a Point object or derived class, and it has the same x and y values.

Overrides:
equals in class Object

clone

public Object clone()
Clone this object. Since the flyweight pattern is used, just return the this pointer.

Overrides:
clone in class Object
Returns:
A Point object.

toString

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

Overrides:
toString in class Object
Returns:
A String.