From f737485337caad66fe683bf027b933d44c9d780c Mon Sep 17 00:00:00 2001 From: djib Date: Thu, 5 Jan 2006 13:22:38 +0000 Subject: [PATCH] NEW CLASS: Square Class Square and Move work. I'm still working on BitBoard. --- TODO | 3 ++ src/suicideChess/BitBoard.java | 18 ++++++++---- src/suicideChess/Move.java | 41 +++++++++++++-------------- src/suicideChess/Square.java | 51 ++++++++++++++++++++++++++++++++++ src/tests/TestMoves.java | 43 +++++++++++++++++++++++++--- 5 files changed, 126 insertions(+), 30 deletions(-) create mode 100644 TODO create mode 100644 src/suicideChess/Square.java diff --git a/TODO b/TODO new file mode 100644 index 0000000..3023aa2 --- /dev/null +++ b/TODO @@ -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 ?) \ No newline at end of file diff --git a/src/suicideChess/BitBoard.java b/src/suicideChess/BitBoard.java index d3234e2..43bba7f 100644 --- a/src/suicideChess/BitBoard.java +++ b/src/suicideChess/BitBoard.java @@ -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; + } } \ No newline at end of file diff --git a/src/suicideChess/Move.java b/src/suicideChess/Move.java index b3368c2..21f9eb0 100644 --- a/src/suicideChess/Move.java +++ b/src/suicideChess/Move.java @@ -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; } } diff --git a/src/suicideChess/Square.java b/src/suicideChess/Square.java new file mode 100644 index 0000000..7274d3e --- /dev/null +++ b/src/suicideChess/Square.java @@ -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); + } +} diff --git a/src/tests/TestMoves.java b/src/tests/TestMoves.java index a20e700..7fb6251 100644 --- a/src/tests/TestMoves.java +++ b/src/tests/TestMoves.java @@ -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(" "); + } +} \ No newline at end of file