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

3
TODO Normal file
View File

@ -0,0 +1,3 @@
Créer une classe Piece pour mettre dans PromotionPiece dans Move.java
Further testing of the moves : check if squares are valid (function is square ?)

View File

@ -5,6 +5,10 @@ package suicideChess;
* *
* This file contains the board representation. * This file contains the board representation.
* The board is represented using bitboards. * The board is represented using bitboards.
* a1 is square 0
* h8 is square 7
* a2 is square 8
* ... and so on
* *
* $LastChangedDate$ * $LastChangedDate$
* $LastChangedRevision$ * $LastChangedRevision$
@ -18,7 +22,9 @@ public class BitBoard {
*************/ *************/
//Some constants to make code more readable //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 //Some constants used to access the bitboards
//Note that in suicide chess there may be more than one king //Note that in suicide chess there may be more than one king
@ -75,12 +81,14 @@ public class BitBoard {
return bitBoards[bitboard_number]; return bitBoards[bitboard_number];
} }
/* TODO
public void updateBitboards(String move) { 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; package suicideChess;
import suicideChess.Square.NotAValidSquare;
/** /**
* @author djib * @author djib
* *
@ -17,12 +19,17 @@ public class Move {
********/ ********/
//integers //integers
private String fromSquare; private Square fromSquare;
private String toSquare; private Square toSquare;
private String promotionPiece; private String promotionPiece;
private boolean isPromotion; private boolean isPromotion;
public class NotAValidMoveException extends Exception { public class NotAValidMoveException extends Exception {
/**
* Generated by Eclipse
*/
private static final long serialVersionUID = 2194133427162274651L;
NotAValidMoveException(String s) { super(s); }; 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 //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; isPromotion = false;
promotionPiece = ""; promotionPiece = "";
switch (move.length()) { switch (move.length()) {
@ -40,8 +47,8 @@ public class Move {
promotionPiece = move.substring(4,5); promotionPiece = move.substring(4,5);
//no break statement here on purpose //no break statement here on purpose
case 4: case 4:
fromSquare = move.substring(0,2); fromSquare = new Square(move.substring(0,2));
toSquare = move.substring(2,4); toSquare = new Square(move.substring(2,4));
break; break;
default: default:
throw new NotAValidMoveException("Invalid Move: "+move); throw new NotAValidMoveException("Invalid Move: "+move);
@ -54,20 +61,23 @@ public class Move {
* METHODS * * METHODS *
***********/ ***********/
public String fromSquare() { public Square fromSquare() {
return fromSquare; return fromSquare;
} }
public String toSquare() { public Square toSquare() {
return toSquare; return toSquare;
} }
public boolean isPromotionMove() { public boolean isPromotionMove() {
return isPromotion; return isPromotion;
} }
public String getPromotionPiece() {
return promotionPiece;
}
public void setFromSquare(String square) { public void setFromSquare(Square square) {
fromSquare = square; fromSquare = square;
} }
public void setToSquare(String square) { public void setToSquare(Square square) {
toSquare = square; toSquare = square;
} }
public void setIsPromotionMove(String piece) { public void setIsPromotionMove(String piece) {
@ -80,17 +90,6 @@ public class Move {
} }
public String toString() { public String toString() {
return fromSquare+toSquare+promotionPiece; return fromSquare.toString()+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(" ");
} }
} }

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.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import suicideChess.Move;
import suicideChess.*;
import suicideChess.Square.NotAValidSquare;
/** /**
* @author djib * @author djib
@ -22,13 +24,46 @@ public class TestMoves {
while (true) { while (true) {
try { try {
String move = moveInput.readLine(); 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) { } catch (Exception err) {
System.out.println(err); System.out.println(err);
break; 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(" ");
}
} }