The computer plays with minmax and detects the end of games
No known bugs
This commit is contained in:
2006-01-27 23:34:09 +00:00
parent 5266b582bb
commit 213b1e3bf0
8 changed files with 225 additions and 193 deletions

11
TODO
View File

@ -2,3 +2,14 @@ Have a look at all the checks that are done.
Add constants for useless ones. Add constants for useless ones.
Look at what should be static Look at what should be static
Detect end of game.
This will be done with heuristics because number of pieces on the games will be a parameter of the heuristic function.
Be able to let the computer play against himself.
I haven't implemented the draw at all !!!
Allow undo.
Improve speed by adding a function : is there a legal move ?

View File

@ -31,12 +31,8 @@ public class Board {
private static final int NB_OF_BITBOARDS = 14; private static final int NB_OF_BITBOARDS = 14;
@SuppressWarnings("serial")
public class NoPieceOnSquare extends Exception { public class NoPieceOnSquare extends Exception {
/*
* Added by Eclipse
*/
private static final long serialVersionUID = -2750943856086117656L;
NoPieceOnSquare(String s) { super(s); }; NoPieceOnSquare(String s) { super(s); };
} }
@ -88,6 +84,9 @@ public class Board {
*/ */
public Board() throws NotAValidSquare { public Board() throws NotAValidSquare {
//the following line makes sure that enPassantSquare is defined at some point.
enPassantSquare = new Square("a1");
bitBoards = new long[NB_OF_BITBOARDS]; bitBoards = new long[NB_OF_BITBOARDS];
addPiece(new Square("a1"),new Piece(Piece.WHITE_ROOK)); addPiece(new Square("a1"),new Piece(Piece.WHITE_ROOK));
addPiece(new Square("b1"),new Piece(Piece.WHITE_KNIGHT)); addPiece(new Square("b1"),new Piece(Piece.WHITE_KNIGHT));
@ -141,7 +140,7 @@ public class Board {
this.bitBoards[i] = bitboard.bitBoards[i]; this.bitBoards[i] = bitboard.bitBoards[i];
} }
this.enPassant = bitboard.enPassant; this.enPassant = bitboard.enPassant;
this.enPassantSquare = bitboard.enPassantSquare; this.enPassantSquare = new Square(bitboard.enPassantSquare);
} }
/*================* /*================*
@ -163,8 +162,6 @@ public class Board {
} else { } else {
removePiece(move.toSquare(), move.getCapturedPiece()); removePiece(move.toSquare(), move.getCapturedPiece());
} }
//capture moves change the value of the board
evaluateNewBoardValue(move);
} }
removePiece(move.fromSquare(), move.getMovingPiece()); removePiece(move.fromSquare(), move.getMovingPiece());
if (move.isPromotionMove()) { if (move.isPromotionMove()) {
@ -179,7 +176,7 @@ public class Board {
enPassantSquare=move.getEnPassantSquare(); enPassantSquare=move.getEnPassantSquare();
} }
evaluateNewBoardValue(move);
} }
/** /**
@ -421,6 +418,15 @@ public class Board {
boardValue = numberOfBlackPieces - numberOfWhitePieces; boardValue = numberOfBlackPieces - numberOfWhitePieces;
} }
} }
if ((Rules.getLegalMovesCapture().size()==0)&&(Rules.getLegalMovesNonCapture().size()==0)) {
// The following line is NOT an error !!!
// After move from WHITE, if there is no moves for BLACK, BLACK won.
if (move.getMovingPiece().getColor()==Piece.WHITE) {
boardValue = BLACK_WINS;
} else {
boardValue = WHITE_WINS;
}
}
} }
} }

View File

@ -16,13 +16,6 @@ public class ComputerPlayer {
/**
* This constructor creates a computer.
*/
public ComputerPlayer() {
//this.color = color;
}
/** /**
* This asks the computer to compute a move * This asks the computer to compute a move
* @param bitboard The current status of the {@link Board} * @param bitboard The current status of the {@link Board}
@ -32,7 +25,7 @@ public class ComputerPlayer {
* @see Board * @see Board
* @see Move * @see Move
*/ */
public Move doRandomMove(Board bitboard, int color) throws NotAValidSquare { public static Move doRandomMove(Board bitboard, int color) throws NotAValidSquare {
Random generator = new Random(); Random generator = new Random();
Rules.legalMovesForPlayer(bitboard,color); Rules.legalMovesForPlayer(bitboard,color);
ArrayList<Move> allLegalMoves = Rules.getLegalMovesCapture(); ArrayList<Move> allLegalMoves = Rules.getLegalMovesCapture();
@ -57,14 +50,14 @@ public class ComputerPlayer {
* @see Board * @see Board
* @see Move * @see Move
*/ */
public Move doMinMaxMove(Board bitboard, int color) throws NotAValidSquare, NoPieceOnSquare { public static Move doMinMaxMove(Board bitboard, int color) throws NotAValidSquare, NoPieceOnSquare {
bestMove = null; bestMove = null;
MinMax(bitboard, color, 0); MinMax(bitboard, color, 0);
return bestMove; return bestMove;
} }
private int MinMax(Board bitboard, int color, int currentDepth) throws NotAValidSquare, NoPieceOnSquare { private static int MinMax(Board bitboard, int color, int currentDepth) throws NotAValidSquare, NoPieceOnSquare {
if (currentDepth >= SuicideChess.PLY_DEPTH) { if (currentDepth >= SuicideChess.PLY_DEPTH) {
return bitboard.getBoardValue(); return bitboard.getBoardValue();
} }

View File

@ -32,12 +32,8 @@ public class Move {
private boolean isEnPassant=false; private boolean isEnPassant=false;
private Square enPassantSquare; private Square enPassantSquare;
@SuppressWarnings("serial")
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); };
} }

View File

@ -66,40 +66,39 @@ public class Rules {
* @see Square * @see Square
* @see Board * @see Board
*/ */
public static void legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare { public static void legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare {
Move validMove; Move validMove;
Square toSquare; 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"
Piece movingPiece = board.getPiece(fromSquare); boolean captureRight=true;
switch (movingPiece.getPieceType()) { boolean captureLeft=true;
case Piece.NONE: Square toCaptureLeftSquare=null;
break; Square toCaptureRightSquare=null;
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 captureEnPassantRight=board.isEnPassant();
boolean captureEnPassantLeft=board.isEnPassant(); boolean captureEnPassantLeft=board.isEnPassant();
Square captureEnPassantLeftSquare=null; Square captureEnPassantLeftSquare=null;
Square captureEnPassantRightSquare=null; Square captureEnPassantRightSquare=null;
switch (movingPiece.getColor()) { switch (movingPiece.getColor()) {
case Piece.WHITE: case Piece.WHITE:
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+1); toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+1);
if (fromSquare.getRank()==2) { if (fromSquare.getRank()==2) {
bigJump=true; bigJump=true;
toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+2); toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+2);
} else if (toSquare.getRank()==Board.NB_OF_RANKS) { } else if (toSquare.getRank()==Board.NB_OF_RANKS) {
promotion=true; promotion=true;
} }
if(fromSquare.getFileNb()>1) { //make sure not to go out of the board if(fromSquare.getFileNb()>1) { //make sure not to go out of the board
toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()+1); toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()+1);
if(captureEnPassantLeft) { if(captureEnPassantLeft) {
captureEnPassantLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()); captureEnPassantLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank());
@ -123,15 +122,15 @@ public class Rules {
captureRight = false; captureRight = false;
captureEnPassantRight=false; captureEnPassantRight=false;
} }
break; break;
case Piece.BLACK: case Piece.BLACK:
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-1); toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-1);
if (fromSquare.getRank()==(Board.NB_OF_RANKS-1)) { if (fromSquare.getRank()==(Board.NB_OF_RANKS-1)) {
bigJump=true; bigJump=true;
toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-2); toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-2);
} else if (toSquare.getRank()==1) { } else if (toSquare.getRank()==1) {
promotion=true; promotion=true;
} }
if(fromSquare.getFileNb()>1) { //make sure not to go out of the board if(fromSquare.getFileNb()>1) { //make sure not to go out of the board
toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()-1); toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()-1);
if(captureEnPassantLeft) { if(captureEnPassantLeft) {
@ -156,89 +155,89 @@ public class Rules {
captureRight = false; captureRight = false;
captureEnPassantRight=false; captureEnPassantRight=false;
} }
break; break;
default: default:
throw new RuntimeException("ERROR 01."); throw new RuntimeException("ERROR 01.");
} }
if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) { if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) {
normalMove= false; //cannot move forward if there is a piece normalMove= false; //cannot move forward if there is a piece
bigJump=false; bigJump=false;
} }
if (captureLeft&& if (captureLeft&&
((board.getPiece(toCaptureLeftSquare).getColor()==Piece.NO_COLOR) ((board.getPiece(toCaptureLeftSquare).getColor()==Piece.NO_COLOR)
|| (board.getPiece(toCaptureLeftSquare).getColor()==movingPiece.getColor()))) { || (board.getPiece(toCaptureLeftSquare).getColor()==movingPiece.getColor()))) {
captureLeft = false; captureLeft = false;
} }
if (captureRight&& if (captureRight&&
((board.getPiece(toCaptureRightSquare).getColor()==Piece.NO_COLOR) ((board.getPiece(toCaptureRightSquare).getColor()==Piece.NO_COLOR)
|| (board.getPiece(toCaptureRightSquare).getColor()==movingPiece.getColor()))) { || (board.getPiece(toCaptureRightSquare).getColor()==movingPiece.getColor()))) {
captureRight = false; captureRight = false;
} }
if (bigJump && (board.getPiece(toBigJumpSquare).getPieceNumber()!=Piece.NONE)) { if (bigJump && (board.getPiece(toBigJumpSquare).getPieceNumber()!=Piece.NONE)) {
bigJump=false; bigJump=false;
} }
if (promotion) { if (promotion) {
if (normalMove) { if (normalMove) {
//to Queen //to Queen
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
legalMovesNonCapture.add(validMove); legalMovesNonCapture.add(validMove);
//to King //to King
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KING_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
legalMovesNonCapture.add(validMove); legalMovesNonCapture.add(validMove);
//to Bishop //to Bishop
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
legalMovesNonCapture.add(validMove); legalMovesNonCapture.add(validMove);
//to Knight //to Knight
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
legalMovesNonCapture.add(validMove); legalMovesNonCapture.add(validMove);
//to Rook //to Rook
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.ROOK_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
legalMovesNonCapture.add(validMove); legalMovesNonCapture.add(validMove);
} }
if (captureLeft) { if (captureLeft) {
//to Queen //to Queen
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
//to King //to King
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
//to Bishop //to Bishop
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
//to Knight //to Knight
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
//to Rook //to Rook
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
} }
if (captureRight) { if (captureRight) {
//to Queen //to Queen
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
//to King //to King
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
//to Knight //to Knight
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
//to Bishop //to Bishop
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
//to Rook //to Rook
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
} }
} else { } else {
if (normalMove) { if (normalMove) {
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.NONE)); validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.NONE));
legalMovesNonCapture.add(validMove); legalMovesNonCapture.add(validMove);
} }
if (bigJump) { if (bigJump) {
//create an 'en-passant' status //create an 'en-passant' status
validMove = new Move(fromSquare, toBigJumpSquare, toSquare, movingPiece, false); validMove = new Move(fromSquare, toBigJumpSquare, toSquare, movingPiece, false);
legalMovesNonCapture.add(validMove); legalMovesNonCapture.add(validMove);
} }
if (captureEnPassantLeft) { if (captureEnPassantLeft) {
//create an 'en-passant' move //create an 'en-passant' move
validMove = new Move(fromSquare, toCaptureLeftSquare, captureEnPassantLeftSquare, movingPiece, true); validMove = new Move(fromSquare, toCaptureLeftSquare, captureEnPassantLeftSquare, movingPiece, true);
@ -251,49 +250,50 @@ public class Rules {
} }
if (captureLeft) { if (captureLeft) {
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.NONE)); validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.NONE));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
} }
if (captureRight) { if (captureRight) {
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.NONE)); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.NONE));
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
} }
} }
break; break;
default: default:
//look for all valid moves int pieceType = movingPiece.getPieceType();
for (int ray=0; int bitboardSquare = Board.squareToBitBoardSquare(fromSquare);
ray<movesAllowed[movingPiece.getPieceType()][Board.squareToBitBoardSquare(fromSquare)].length; //look for all valid moves
ray++) { for (int ray=0; ray<movesAllowed[pieceType][bitboardSquare].length; ray++) {
for (int moveNumber=0; try {
moveNumber<movesAllowed[movingPiece.getPieceType()][Board.squareToBitBoardSquare(fromSquare)][ray].length; for (int moveNumber=0; moveNumber<movesAllowed[pieceType][bitboardSquare][ray].length; moveNumber++) {
moveNumber++) {
toSquare = toSquare =
new Square(movesAllowed[movingPiece.getPieceType()] new Square(movesAllowed[pieceType][bitboardSquare][ray][moveNumber]);
[Board.squareToBitBoardSquare(fromSquare)][ray][moveNumber]);
//first check if there is a piece of the same color on that square //first check if there is a piece of the same color on that square
if (board.getPiece(toSquare).getColor()==movingPiece.getColor()) { if (board.getPiece(toSquare).getColor()==movingPiece.getColor()) {
break; break;
} }
validMove = new Move(fromSquare, toSquare, movingPiece, board.getPiece(toSquare), new Piece(Piece.NONE)); validMove = new Move(fromSquare, toSquare, movingPiece, board.getPiece(toSquare), new Piece(Piece.NONE));
//add move to list of legal moves //add move to list of legal moves
if (validMove.isCaptureMove()) { if (validMove.isCaptureMove()) {
legalMovesCapture.add(validMove); legalMovesCapture.add(validMove);
break; //don't go further in the ray break; //don't go further in the ray
} else { } else {
legalMovesNonCapture.add(validMove); 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
}//end for moveNumber }
}//end for ray
} //end case
}
/** /**
* This function return the current status of the ArrayList<Move> of all legal {@link Move} * 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 ][ 2 ][ 4 ] = 5;
movesAllowed[Piece.QUEEN][ 45 ][ 3 ][ 0 ] = 53; movesAllowed[Piece.QUEEN][ 45 ][ 3 ][ 0 ] = 53;
movesAllowed[Piece.QUEEN][ 45 ][ 3 ][ 1 ] = 61; 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 ][ 4 ] = new int[ 5 ];
movesAllowed[Piece.QUEEN][ 45 ][ 5 ] = new int[ 2 ]; movesAllowed[Piece.QUEEN][ 45 ][ 5 ] = new int[ 2 ];
movesAllowed[Piece.QUEEN][ 45 ][ 6 ] = new int[ 2 ]; movesAllowed[Piece.QUEEN][ 45 ][ 6 ] = new int[ 2 ];

View File

@ -13,12 +13,8 @@ public class Square {
private int fileNb; private int fileNb;
private int rank; private int rank;
public class NotAValidSquare extends Exception { @SuppressWarnings("serial")
/* public class NotAValidSquare extends Exception {
* Generated by Eclipse
*/
private static final long serialVersionUID = 7586171991212094565L;
NotAValidSquare(String s) { super(s); }; NotAValidSquare(String s) { super(s); };
} }
@ -47,6 +43,15 @@ public class Square {
} }
} }
/**
* Constructs a Square by copying another square
*/
public Square(Square square) {
this.file = square.file;
this.fileNb = square.fileNb;
this.rank = square.rank;
}
/** /**
* Construcs a Square given a bitboard square (number from 0 to 63) * Construcs a Square given a bitboard square (number from 0 to 63)
* @param bitboardSquare An integer representing the bitboard square * @param bitboardSquare An integer representing the bitboard square

View File

@ -65,6 +65,23 @@ public class SuicideChess {
} }
} }
/**
* Test and display if the board is in a winning state.
* @param bitboard A Board
* @return True or False
*/
private static boolean testWinningPosition (Board bitboard) {
if (bitboard.getBoardValue()==Board.BLACK_WINS) {
System.out.println("0-1 {Black mates}");
return true;
} else if (bitboard.getBoardValue()==Board.WHITE_WINS) {
System.out.println("1-0 {White mates}");
return true;
}
return false;
}
/** /**
* If feature usermove has not been accepted by XBoard then consider all unknown commands * If feature usermove has not been accepted by XBoard then consider all unknown commands
* as moves * as moves
@ -95,7 +112,6 @@ public class SuicideChess {
System.out.println("White: "); System.out.println("White: ");
} }
ComputerPlayer computer = new ComputerPlayer();
boolean computerPlaying = true; //the computer does not play in foce mode. boolean computerPlaying = true; //the computer does not play in foce mode.
boolean playing = true; boolean playing = true;
@ -132,7 +148,7 @@ public class SuicideChess {
System.out.println("variant suicide"); System.out.println("variant suicide");
break; break;
case XBoardProtocol.HINT: case XBoardProtocol.HINT:
System.out.println("Hint: "+computer.doRandomMove(bitboard,currentPlayerColor)); System.out.println("Hint: "+ComputerPlayer.doRandomMove(bitboard,currentPlayerColor));
break; break;
case XBoardProtocol.FORCE: case XBoardProtocol.FORCE:
computerPlaying = false; computerPlaying = false;
@ -193,6 +209,10 @@ public class SuicideChess {
System.out.println("Illegal move: "+theMove.toString()); System.out.println("Illegal move: "+theMove.toString());
} }
if (testWinningPosition(bitboard)) {
computerPlaying=false;
}
if (!playedALegalMove) { if (!playedALegalMove) {
break; break;
} }
@ -205,7 +225,7 @@ public class SuicideChess {
if (xBoardCommand==XBoardProtocol.GO) if (xBoardCommand==XBoardProtocol.GO)
computerPlaying = true; computerPlaying = true;
if (computerPlaying) { if (computerPlaying) {
Move computerMove = computer.doMinMaxMove(bitboard,currentPlayerColor); Move computerMove = ComputerPlayer.doMinMaxMove(bitboard,currentPlayerColor);
bitboard.doMove(computerMove); bitboard.doMove(computerMove);
XBoardProtocol.doMove(computerMove); XBoardProtocol.doMove(computerMove);
if (ASCII_GAME) { if (ASCII_GAME) {
@ -215,8 +235,11 @@ public class SuicideChess {
} }
changeCurrentPlayerColor(); changeCurrentPlayerColor();
} }
break; if (testWinningPosition(bitboard)) {
computerPlaying=false;
}
break;
} }
// if (whatMove.startsWith("hint")) { // if (whatMove.startsWith("hint")) {
@ -255,9 +278,8 @@ public class SuicideChess {
} catch (NotAValidSquare err) { } catch (NotAValidSquare err) {
System.out.println(err); System.out.println(err);
continue; continue;
} catch (Exception err) { } catch (Exception e) {
System.out.println(err); e.printStackTrace();
break;
} }
} }
} }

View File

@ -78,7 +78,7 @@ public class XBoardProtocol {
System.out.println("feature sigint=0 sigterm=0"); System.out.println("feature sigint=0 sigterm=0");
System.out.println("feature usermove=1"); //sends moves like 'usermove e2e4' System.out.println("feature usermove=1"); //sends moves like 'usermove e2e4'
System.out.println("feature variants=\"suicide\""); System.out.println("feature variants=\"suicide\"");
System.out.println("feature time=0 draw=0 reuse=0 analyse=0 colors=0"); System.out.println("feature time=0 draw=0 reuse=0 analyze=0 colors=0");
System.out.println("feature done=1"); System.out.println("feature done=1");
if (SuicideChess.ASCII_GAME) if (SuicideChess.ASCII_GAME)
System.out.println(); System.out.println();