v0.4.1
The computer plays with minmax and detects the end of games No known bugs
This commit is contained in:
@ -12,7 +12,7 @@ import suicideChess.Square.NotAValidSquare;
|
||||
*/
|
||||
|
||||
public class Rules {
|
||||
|
||||
|
||||
private static ArrayList<Move> legalMovesNonCapture;
|
||||
private static ArrayList<Move> legalMovesCapture;
|
||||
|
||||
@ -66,40 +66,39 @@ public class Rules {
|
||||
* @see Square
|
||||
* @see Board
|
||||
*/
|
||||
public static void legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare {
|
||||
public static void legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare {
|
||||
Move validMove;
|
||||
Square toSquare;
|
||||
|
||||
Piece movingPiece = board.getPiece(fromSquare);
|
||||
switch (movingPiece.getPieceType()) {
|
||||
case Piece.NONE:
|
||||
break;
|
||||
case Piece.PAWN:
|
||||
boolean normalMove=true; //is a normal (straight) move allowed
|
||||
boolean promotion=false;
|
||||
boolean bigJump=false; //if the pawn is allowed to move two squares
|
||||
Square toBigJumpSquare=null; //the square for the "big jump"
|
||||
|
||||
boolean captureRight=true;
|
||||
boolean captureLeft=true;
|
||||
Square toCaptureLeftSquare=null;
|
||||
Square toCaptureRightSquare=null;
|
||||
Square toSquare;
|
||||
Piece movingPiece = board.getPiece(fromSquare);
|
||||
switch (movingPiece.getPieceType()) {
|
||||
case Piece.NONE:
|
||||
break;
|
||||
case Piece.PAWN:
|
||||
boolean normalMove=true; //is a normal (straight) move allowed
|
||||
boolean promotion=false;
|
||||
boolean bigJump=false; //if the pawn is allowed to move two squares
|
||||
Square toBigJumpSquare=null; //the square for the "big jump"
|
||||
|
||||
boolean captureRight=true;
|
||||
boolean captureLeft=true;
|
||||
Square toCaptureLeftSquare=null;
|
||||
Square toCaptureRightSquare=null;
|
||||
|
||||
boolean captureEnPassantRight=board.isEnPassant();
|
||||
boolean captureEnPassantLeft=board.isEnPassant();
|
||||
Square captureEnPassantLeftSquare=null;
|
||||
Square captureEnPassantRightSquare=null;
|
||||
|
||||
switch (movingPiece.getColor()) {
|
||||
case Piece.WHITE:
|
||||
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+1);
|
||||
if (fromSquare.getRank()==2) {
|
||||
bigJump=true;
|
||||
toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+2);
|
||||
} else if (toSquare.getRank()==Board.NB_OF_RANKS) {
|
||||
promotion=true;
|
||||
}
|
||||
if(fromSquare.getFileNb()>1) { //make sure not to go out of the board
|
||||
|
||||
switch (movingPiece.getColor()) {
|
||||
case Piece.WHITE:
|
||||
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+1);
|
||||
if (fromSquare.getRank()==2) {
|
||||
bigJump=true;
|
||||
toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+2);
|
||||
} else if (toSquare.getRank()==Board.NB_OF_RANKS) {
|
||||
promotion=true;
|
||||
}
|
||||
if(fromSquare.getFileNb()>1) { //make sure not to go out of the board
|
||||
toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()+1);
|
||||
if(captureEnPassantLeft) {
|
||||
captureEnPassantLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank());
|
||||
@ -123,15 +122,15 @@ public class Rules {
|
||||
captureRight = false;
|
||||
captureEnPassantRight=false;
|
||||
}
|
||||
break;
|
||||
case Piece.BLACK:
|
||||
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-1);
|
||||
if (fromSquare.getRank()==(Board.NB_OF_RANKS-1)) {
|
||||
bigJump=true;
|
||||
toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-2);
|
||||
} else if (toSquare.getRank()==1) {
|
||||
promotion=true;
|
||||
}
|
||||
break;
|
||||
case Piece.BLACK:
|
||||
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-1);
|
||||
if (fromSquare.getRank()==(Board.NB_OF_RANKS-1)) {
|
||||
bigJump=true;
|
||||
toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-2);
|
||||
} else if (toSquare.getRank()==1) {
|
||||
promotion=true;
|
||||
}
|
||||
if(fromSquare.getFileNb()>1) { //make sure not to go out of the board
|
||||
toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()-1);
|
||||
if(captureEnPassantLeft) {
|
||||
@ -156,89 +155,89 @@ public class Rules {
|
||||
captureRight = false;
|
||||
captureEnPassantRight=false;
|
||||
}
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("ERROR 01.");
|
||||
}
|
||||
if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) {
|
||||
normalMove= false; //cannot move forward if there is a piece
|
||||
}
|
||||
if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) {
|
||||
normalMove= false; //cannot move forward if there is a piece
|
||||
bigJump=false;
|
||||
}
|
||||
if (captureLeft&&
|
||||
}
|
||||
if (captureLeft&&
|
||||
((board.getPiece(toCaptureLeftSquare).getColor()==Piece.NO_COLOR)
|
||||
|| (board.getPiece(toCaptureLeftSquare).getColor()==movingPiece.getColor()))) {
|
||||
captureLeft = false;
|
||||
}
|
||||
|| (board.getPiece(toCaptureLeftSquare).getColor()==movingPiece.getColor()))) {
|
||||
captureLeft = false;
|
||||
}
|
||||
if (captureRight&&
|
||||
((board.getPiece(toCaptureRightSquare).getColor()==Piece.NO_COLOR)
|
||||
|| (board.getPiece(toCaptureRightSquare).getColor()==movingPiece.getColor()))) {
|
||||
captureRight = false;
|
||||
}
|
||||
if (bigJump && (board.getPiece(toBigJumpSquare).getPieceNumber()!=Piece.NONE)) {
|
||||
bigJump=false;
|
||||
}
|
||||
if (promotion) {
|
||||
if (normalMove) {
|
||||
//to Queen
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
//to King
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
//to Bishop
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
|| (board.getPiece(toCaptureRightSquare).getColor()==movingPiece.getColor()))) {
|
||||
captureRight = false;
|
||||
}
|
||||
if (bigJump && (board.getPiece(toBigJumpSquare).getPieceNumber()!=Piece.NONE)) {
|
||||
bigJump=false;
|
||||
}
|
||||
if (promotion) {
|
||||
if (normalMove) {
|
||||
//to Queen
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
//to King
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
//to Bishop
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
//to Knight
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
//to Rook
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
if (captureLeft) {
|
||||
//to Queen
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to King
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Bishop
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Rook
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
if (captureLeft) {
|
||||
//to Queen
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to King
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Bishop
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Knight
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Rook
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
if (captureRight) {
|
||||
//to Queen
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to King
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Knight
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Bishop
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Rook
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
} else {
|
||||
if (normalMove) {
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.NONE));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
if (bigJump) {
|
||||
//create an 'en-passant' status
|
||||
//to Rook
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
if (captureRight) {
|
||||
//to Queen
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to King
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Knight
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Bishop
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Rook
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
} else {
|
||||
if (normalMove) {
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.NONE));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
if (bigJump) {
|
||||
//create an 'en-passant' status
|
||||
validMove = new Move(fromSquare, toBigJumpSquare, toSquare, movingPiece, false);
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
if (captureEnPassantLeft) {
|
||||
//create an 'en-passant' move
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, captureEnPassantLeftSquare, movingPiece, true);
|
||||
@ -249,51 +248,52 @@ public class Rules {
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, captureEnPassantRightSquare, movingPiece, true);
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
|
||||
|
||||
if (captureLeft) {
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.NONE));
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
if (captureRight) {
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.NONE));
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
//look for all valid moves
|
||||
for (int ray=0;
|
||||
ray<movesAllowed[movingPiece.getPieceType()][Board.squareToBitBoardSquare(fromSquare)].length;
|
||||
ray++) {
|
||||
for (int moveNumber=0;
|
||||
moveNumber<movesAllowed[movingPiece.getPieceType()][Board.squareToBitBoardSquare(fromSquare)][ray].length;
|
||||
moveNumber++) {
|
||||
|
||||
toSquare =
|
||||
new Square(movesAllowed[movingPiece.getPieceType()]
|
||||
[Board.squareToBitBoardSquare(fromSquare)][ray][moveNumber]);
|
||||
|
||||
//first check if there is a piece of the same color on that square
|
||||
if (board.getPiece(toSquare).getColor()==movingPiece.getColor()) {
|
||||
break;
|
||||
}
|
||||
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, board.getPiece(toSquare), new Piece(Piece.NONE));
|
||||
|
||||
//add move to list of legal moves
|
||||
if (validMove.isCaptureMove()) {
|
||||
legalMovesCapture.add(validMove);
|
||||
break; //don't go further in the ray
|
||||
} else {
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
|
||||
}//end for moveNumber
|
||||
}//end for ray
|
||||
} //end case
|
||||
|
||||
}
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.NONE));
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
if (captureRight) {
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.NONE));
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
int pieceType = movingPiece.getPieceType();
|
||||
int bitboardSquare = Board.squareToBitBoardSquare(fromSquare);
|
||||
//look for all valid moves
|
||||
for (int ray=0; ray<movesAllowed[pieceType][bitboardSquare].length; ray++) {
|
||||
try {
|
||||
for (int moveNumber=0; moveNumber<movesAllowed[pieceType][bitboardSquare][ray].length; moveNumber++) {
|
||||
|
||||
toSquare =
|
||||
new Square(movesAllowed[pieceType][bitboardSquare][ray][moveNumber]);
|
||||
|
||||
//first check if there is a piece of the same color on that square
|
||||
if (board.getPiece(toSquare).getColor()==movingPiece.getColor()) {
|
||||
break;
|
||||
}
|
||||
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, board.getPiece(toSquare), new Piece(Piece.NONE));
|
||||
|
||||
//add move to list of legal moves
|
||||
if (validMove.isCaptureMove()) {
|
||||
legalMovesCapture.add(validMove);
|
||||
break; //don't go further in the ray
|
||||
} else {
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
}//end for moveNumber
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("pieceType:"+pieceType+" bitboardSquare:"+bitboardSquare+
|
||||
" ray:"+ray+" movesAllowed[pieceType][bitboardSquare][ray]:"+movesAllowed[pieceType][bitboardSquare][ray]);
|
||||
}
|
||||
}//end for ray
|
||||
} //end case
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This function return the current status of the ArrayList<Move> of all legal {@link Move}
|
||||
@ -4843,7 +4843,6 @@ public class Rules {
|
||||
movesAllowed[Piece.QUEEN][ 45 ][ 2 ][ 4 ] = 5;
|
||||
movesAllowed[Piece.QUEEN][ 45 ][ 3 ][ 0 ] = 53;
|
||||
movesAllowed[Piece.QUEEN][ 45 ][ 3 ][ 1 ] = 61;
|
||||
movesAllowed[Piece.QUEEN][ 45 ] = new int[ 8 ][];
|
||||
movesAllowed[Piece.QUEEN][ 45 ][ 4 ] = new int[ 5 ];
|
||||
movesAllowed[Piece.QUEEN][ 45 ][ 5 ] = new int[ 2 ];
|
||||
movesAllowed[Piece.QUEEN][ 45 ][ 6 ] = new int[ 2 ];
|
||||
|
Reference in New Issue
Block a user