/**
* Class Pos.
*
* @author Philipp Bolliger
* Informatik II - FS2009
* Uebungsserie 11
*/
public class Pos implements Comparable {
/* coordinates of a Pos object */
private int x, y;
/* maximal width and height of the board */
private static int max;
/** Sets the maximum width and height of the board. */
public static void setMax( int m ) {
max = m;
}
/**
* Returns the number of valid positions on a board of width and
* height as specified in setMax().
*/
public static int boardPositions( ) {
return max*max;
}
/**
* Creates a Pos object representing the position (x,y) on a
* board. The maximum value for x and y must be set with
* setMax(). Specifying x and y values larger than the max value
* specified in setMax() triggers a RuntimeException.
* */
public Pos( int x, int y ) {
if( Pos.valid( x,y )== true ) {
this.x = x;
this.y = y;
} else {
String str = "Invalid chess position (" + x + "," + y + ").";
throw( new RuntimeException( str ));
}
}
/**
* Checks whether (x,y) is a valid position on a board of width
* and height as specified in setMax().
*
* @return true iff (x,y) is a valid position on a board of width
* and height as specified in setMax().
**/
static boolean valid( int x, int y ) {
return ( x >= 1 && x <= max && y >= 1 && y <= max );
}
/**
* create() is a factory for Pos objects, similar to the
* constructor. However, if an invalid position is specified,
* create() returns null
*
* @param x Coordinate x of a position on the board
* @param y Coordinate y of a position on the board
**/
static Pos create( int x, int y ) {
Pos p;
try {
p = new Pos(x,y);
} catch( RuntimeException e) {
p = null;
}
return p;
} /* end create() */
/**
* compareTo method for Pos Objects
*
* @param o Object to compare
**/
public int compareTo( Object o ) {
Pos p = (Pos)o;
if( equals(o) == true )
return 0;
else if( diagonalNr() < p.diagonalNr() )
return -1;
else
return 1;
} /* end equalTo() */
public int hashCode() {
return diagonalNr();
}
/**
* Computes a unique number for each 2-tupel
* using diagonalization
**/
int diagonalNr() {
return sum(x+y-1)-y+1;
}
/**
* Computes the sum from 1 to n (1 + 2 + ... + n)
*
* @param n The uppermost element of the sum
*/
static int sum( int n ) {
return (int)((1+n) * (float)n/2);
}
/**
* Method equals for Pos Objects
*
* @param o Object to compare
*/
public boolean equals( Object o ) {
Pos p = (Pos)o;
// System.out.println( this + "equals" + p );
return ( (x == p.x) && (y == p.y) );
}
/**
* Returns a String representation of a a Pos Object
*
* @return a String representing of a Pos Object
*/
public String toString() {
return "(" + x + "," + y + ")";
}
/** The relative moves of a knight */
static int deltas[][] ={ { 2,-1},{ 1,-2},{-1,-2},{-2,-1},
{-2, 1},{-1, 2},{ 1, 2},{ 2, 1} };
/**
* Returns a new Pos object representing one move of the eight
* potential moves (specified by n) of a knight starting from
* this. The moves specified by n (0..7) are in the
* following order:
* {2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1}, thus
*
* Pos first = new Pos(4,4);
* Pos next = first.moveTo(0); *