52 lines
1.0 KiB
Java
52 lines
1.0 KiB
Java
package suicideChess;
|
|
|
|
/**
|
|
* @author djib
|
|
*
|
|
* This class avoids the use of strings for squares
|
|
*
|
|
* $LastChangedDate$
|
|
* $LastChangedRevision$
|
|
* $LastChangedBy$
|
|
*/
|
|
|
|
|
|
public class Square {
|
|
private char file;
|
|
private int fileNb;
|
|
private int rank;
|
|
|
|
public class NotAValidSquare extends Exception {
|
|
/**
|
|
* Generated by Eclipse
|
|
*/
|
|
private static final long serialVersionUID = 7586171991212094565L;
|
|
|
|
NotAValidSquare(String s) { super(s); };
|
|
}
|
|
|
|
public Square(String square) throws NotAValidSquare {
|
|
if (square.length()!=2 || !(Character.isLetter(square.toCharArray()[0])) ||
|
|
!(Character.isDigit(square.toCharArray()[1]))) {
|
|
throw new NotAValidSquare(square);
|
|
}
|
|
file = Character.toLowerCase(square.toCharArray()[0]);
|
|
fileNb = ((int)file) - ((int)'a') + 1;
|
|
rank = Integer.parseInt(square.substring(1,2));
|
|
}
|
|
|
|
public char getFile() {
|
|
return file;
|
|
}
|
|
public int getRank() {
|
|
return rank;
|
|
}
|
|
public int getFileNb() {
|
|
return fileNb;
|
|
}
|
|
|
|
public String toString() {
|
|
return String.valueOf(file)+String.valueOf(rank);
|
|
}
|
|
}
|