diff --git a/src/suicideChess/Board.java b/src/suicideChess/Board.java index e5c298d..a90a70d 100644 --- a/src/suicideChess/Board.java +++ b/src/suicideChess/Board.java @@ -160,6 +160,17 @@ public class Board { return new Piece(Piece.NONE); } + /** + * This function converts a {@link Square} to a number representing a bitboard square + * @param square The Square to be converted + * @return int + * @see Square + */ + public static int squareToBitBoardSquare(Square square) { + //converts a square ("e2") to a BitboardSquare ( + return square.getFileNb() -1 + (square.getRank()-1)*NB_OF_FILES; + } + /** * This function can be used to display the board @@ -228,7 +239,7 @@ public class Board { } System.out.println("+---+---+---+---+---+---+---+---+"); } - + /*=================* * PRIVATE METHODS * @@ -239,13 +250,7 @@ public class Board { return bitBoards[bitboard_number]; } */ - - - private int squareToBitBoardSquare(Square square) { - //converts a square ("e2") to a BitboardSquare ( - return square.getFileNb() -1 + (square.getRank()-1)*NB_OF_FILES; - } - + protected void addPiece(Square square, Piece piece) { //OR : // 0 OR a = a diff --git a/src/suicideChess/Move.java b/src/suicideChess/Move.java index 946b139..8ecac73 100644 --- a/src/suicideChess/Move.java +++ b/src/suicideChess/Move.java @@ -78,6 +78,42 @@ public class Move { } } + /** + * This is the constructor of the class. + * + * @param fromSquare The {@link Square} to move from. + * @param toSquare The {@link Square} to move to. + * @param movingPiece The {@link Piece} to be moved. + * @param capturePiece The {@link Piece} to be captured. Piece.NONE if none. + * @param promotionPiece The {link Piece} to be promoted to. Piece.NONE if none. + * @throws NotAValidSquare If Squares are not valid. + * @see Square + * @see Piece + */ + public Move(Square fromSquare, Square toSquare, Piece movingPiece, Piece capturePiece, Piece promotionPiece) + throws NotAValidSquare { + + this.fromSquare = fromSquare; + this.toSquare = toSquare; + + this.movingPiece = movingPiece; + + this.capturePiece = capturePiece; + if (capturePiece.getPieceNumber()!=Piece.NONE) { + isCapture = true; + } else { + isCapture = false; + } + + this.promotionPiece = promotionPiece; + if (promotionPiece.getPieceNumber()!=Piece.NONE) { + isPromotion = true; + } else { + isPromotion = false; + } + + } + /*===========================================* * Those two constructors are no longer used * diff --git a/src/suicideChess/Piece.java b/src/suicideChess/Piece.java index e408030..8093532 100644 --- a/src/suicideChess/Piece.java +++ b/src/suicideChess/Piece.java @@ -21,6 +21,7 @@ public class Piece { * Class BitBoard makes intensive use of those */ public static final int NONE=-1; + public static final int NO_COLOR = -1; //Contants used to detect color public static final int WHITE=0; public static final int BLACK=1; @@ -116,14 +117,25 @@ public class Piece { * @return An integer. */ public int getColor() { - return pieceNumber%2; //cf declaration of BLACK and WHITE above and the pieces above. + if (pieceNumber!=Piece.NONE) { + return pieceNumber%2; //cf declaration of BLACK and WHITE above and the pieces above. + } else { + return Piece.NO_COLOR; + } } /** - * Returns an integer representing the Piece + * Returns an integer representing the Piece (Piece.BLACK_PAWN or Piece.WHITE_QUEEN) * @return An integer. */ public int getPieceNumber() { return pieceNumber; } + + /** + * Returns an integer representing the Piece type (Piece.QUEEN or Piece.PAWN) + */ + public int getPieceType() { + return ((pieceNumber/2)-1); + } } diff --git a/src/suicideChess/Rules.java b/src/suicideChess/Rules.java index bb9a486..7b7d672 100644 --- a/src/suicideChess/Rules.java +++ b/src/suicideChess/Rules.java @@ -1,5 +1,9 @@ package suicideChess; +import java.util.ArrayList; + +import suicideChess.Square.NotAValidSquare; + /** * This class is used to compute legal moves. * @@ -9,19 +13,26 @@ package suicideChess; public class Rules { + private ArrayList legalMoves; + + public Rules () { + legalMoves = new ArrayList(); + } + /** * Computes the possible moves from a given {@link Square} * according to the current status of the {@link Board} * @param square The square from which the move must start * @param board The current board position. + * @throws NotAValidSquare If the program throws this exception then there is a bug. * @see Square * @see Board */ - public Move[] legalMovesFromSquare(Square square, Board board) { - Piece piece = board.getPiece(square); - switch (piece.getPieceNumber()) { + public ArrayList legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare { + Piece movingPiece = board.getPiece(fromSquare); + switch (movingPiece.getPieceType()) { case Piece.NONE: - return null; + break; case Piece.WHITE_PAWN: //@TODO break; @@ -29,9 +40,36 @@ public class Rules { //@TODO break; default: - } + + //look for all valid moves + for (int ray=0; + ray= Board.NB_OF_SQUARES)) { + throw new NotAValidSquare(Integer.toString(bitboardSquare)); + } + fileNb = bitboardSquare%Board.NB_OF_FILES + 1; + file = (char)(fileNb + ((int)'a') - 1); + rank = bitboardSquare/Board.NB_OF_FILES + 1; + } + /** * Returns the file of a Square. * @return char representing the file diff --git a/src/suicideChess/SuicideChess.java b/src/suicideChess/SuicideChess.java index cc8fa27..3356545 100644 --- a/src/suicideChess/SuicideChess.java +++ b/src/suicideChess/SuicideChess.java @@ -2,6 +2,7 @@ package suicideChess; import java.io.BufferedReader; import java.io.InputStreamReader; +import java.util.ArrayList; import suicideChess.Move.NotAValidMoveException; import suicideChess.Square.NotAValidSquare; @@ -46,6 +47,7 @@ public class SuicideChess { /*int playerColor = Piece.WHITE; System.out.println("White: ");*/ + while (true) { try { String whatMove= moveInput.readLine(); @@ -55,6 +57,23 @@ public class SuicideChess { break; } + if (whatMove.startsWith("move")) { + Move move = new Move(new Square(0), new Square(1), new Piece(2), new Piece(3), new Piece(Piece.NONE)); + move.display(); + continue; + } + + + if (whatMove.startsWith("hint")) { + Rules rules = new Rules(); + ArrayList allLegalMoves = + rules.legalMovesFromSquare(new Square(whatMove.substring(5,7)),bitboard); + for(int i = 0; i