NEW CLASS: Square

Class Square and Move work.

I'm still working on BitBoard.
This commit is contained in:
2006-01-05 13:22:38 +00:00
parent 7ce943724c
commit f737485337
5 changed files with 126 additions and 30 deletions

View File

@ -5,6 +5,10 @@ package suicideChess;
*
* This file contains the board representation.
* The board is represented using bitboards.
* a1 is square 0
* h8 is square 7
* a2 is square 8
* ... and so on
*
* $LastChangedDate$
* $LastChangedRevision$
@ -18,7 +22,9 @@ public class BitBoard {
*************/
//Some constants to make code more readable
public static final int NB_OF_SQUARES = 64;
public static final int NB_OF_RANKS = 8;
public static final int NB_OF_FILES = 8;
public static final int NB_OF_SQUARES = NB_OF_RANKS*NB_OF_FILES;
//Some constants used to access the bitboards
//Note that in suicide chess there may be more than one king
@ -75,12 +81,14 @@ public class BitBoard {
return bitBoards[bitboard_number];
}
/* TODO
public void updateBitboards(String move) {
}
*/
public int squareToBitboardSquare()
public int squareToBitboardSquare(Square square) {
//converts a square ("e2") to a BitboardSquare (
return square.getFileNb() -1 + (square.getRank()-1)*NB_OF_FILES;
}
}

View File

@ -1,5 +1,7 @@
package suicideChess;
import suicideChess.Square.NotAValidSquare;
/**
* @author djib
*
@ -17,12 +19,17 @@ public class Move {
********/
//integers
private String fromSquare;
private String toSquare;
private Square fromSquare;
private Square toSquare;
private String promotionPiece;
private boolean isPromotion;
public class NotAValidMoveException extends Exception {
/**
* Generated by Eclipse
*/
private static final long serialVersionUID = 2194133427162274651L;
NotAValidMoveException(String s) { super(s); };
}
@ -31,7 +38,7 @@ public class Move {
***************/
//The string is of type e2e4 (4 chars), b7b8q (5 chars) for promotions
public Move(String move) throws NotAValidMoveException {
public Move(String move) throws NotAValidMoveException, NotAValidSquare {
isPromotion = false;
promotionPiece = "";
switch (move.length()) {
@ -40,8 +47,8 @@ public class Move {
promotionPiece = move.substring(4,5);
//no break statement here on purpose
case 4:
fromSquare = move.substring(0,2);
toSquare = move.substring(2,4);
fromSquare = new Square(move.substring(0,2));
toSquare = new Square(move.substring(2,4));
break;
default:
throw new NotAValidMoveException("Invalid Move: "+move);
@ -54,20 +61,23 @@ public class Move {
* METHODS *
***********/
public String fromSquare() {
public Square fromSquare() {
return fromSquare;
}
public String toSquare() {
public Square toSquare() {
return toSquare;
}
public boolean isPromotionMove() {
return isPromotion;
}
public String getPromotionPiece() {
return promotionPiece;
}
public void setFromSquare(String square) {
public void setFromSquare(Square square) {
fromSquare = square;
}
public void setToSquare(String square) {
public void setToSquare(Square square) {
toSquare = square;
}
public void setIsPromotionMove(String piece) {
@ -80,17 +90,6 @@ public class Move {
}
public String toString() {
return fromSquare+toSquare+promotionPiece;
}
public void display() {
System.out.println(" ");
System.out.println("==== Move ====");
System.out.println("From square:"+fromSquare);
System.out.println("To square: "+toSquare);
System.out.println((isPromotion ? "Promotion: "+promotionPiece : "No Promotion"));
System.out.println("===============");
System.out.println(" ");
System.out.println(" ");
return fromSquare.toString()+toSquare+promotionPiece;
}
}

View File

@ -0,0 +1,51 @@
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);
}
}

View File

@ -2,7 +2,9 @@ package tests;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import suicideChess.Move;
import suicideChess.*;
import suicideChess.Square.NotAValidSquare;
/**
* @author djib
@ -22,13 +24,46 @@ public class TestMoves {
while (true) {
try {
String move = moveInput.readLine();
(new Move(move)).display();
//(new MoveTest(move)).display();
System.out.println((new BitBoard().squareToBitboardSquare(new Square(move))));
//new SquareTest(move).display();
//System.out.println(new Square(move));
} catch (Exception err) {
System.out.println(err);
break;
}
}
}
}
class MoveTest extends Move {
public MoveTest(String move) throws NotAValidMoveException, NotAValidSquare {
super(move);
}
public void display() {
System.out.println(" ");
System.out.println("==== Move ====");
System.out.println("From square:"+fromSquare());
System.out.println("To square: "+toSquare());
System.out.println((isPromotionMove() ? "Promotion: "+getPromotionPiece() : "No Promotion"));
System.out.println("===============");
System.out.println(" ");
System.out.println(" ");
}
}
class SquareTest extends Square {
public SquareTest(String square) throws NotAValidSquare {
super(square);
}
public void display() {
System.out.println(" ");
System.out.println("==== Square ====");
System.out.println("Rank: "+getRank());
System.out.println("File: "+getFile());
System.out.println("FileNb: "+getFileNb());
System.out.println("===============");
System.out.println(" ");
System.out.println(" ");
}
}