Quelques modifications mineures
This commit is contained in:
@ -210,8 +210,21 @@ public class Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
boardValue = getBoardValue();
|
|
||||||
|
|
||||||
|
if (numberOfBlackPieces == 0) {
|
||||||
|
boardValue = BLACK_WINS;
|
||||||
|
} else if (numberOfWhitePieces == 0){
|
||||||
|
boardValue = WHITE_WINS;
|
||||||
|
} else if (!Rules.isThereALegalMovesForPlayer(this)) {
|
||||||
|
if (currentPlayer==Piece.WHITE) {
|
||||||
|
boardValue = WHITE_WINS;
|
||||||
|
} else {
|
||||||
|
boardValue = BLACK_WINS;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
boardValue = getBoardValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -493,6 +506,7 @@ public class Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void evaluateNewBoardValue (Move move) throws NotAValidSquare {
|
private void evaluateNewBoardValue (Move move) throws NotAValidSquare {
|
||||||
|
|
||||||
if (move.isCaptureMove()) {
|
if (move.isCaptureMove()) {
|
||||||
if (move.getCapturedPiece().getColor()==Piece.BLACK) {
|
if (move.getCapturedPiece().getColor()==Piece.BLACK) {
|
||||||
numberOfBlackPieces--;
|
numberOfBlackPieces--;
|
||||||
@ -508,7 +522,7 @@ public class Board {
|
|||||||
boardValue = numberOfBlackPieces - numberOfWhitePieces;
|
boardValue = numberOfBlackPieces - numberOfWhitePieces;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Rules.isThereALegalMovesForPlayer(this)) {
|
if (!Rules.isThereALegalMovesForPlayer(this)) {
|
||||||
if (currentPlayer==Piece.WHITE) {
|
if (currentPlayer==Piece.WHITE) {
|
||||||
boardValue = WHITE_WINS;
|
boardValue = WHITE_WINS;
|
||||||
} else {
|
} else {
|
||||||
|
@ -80,7 +80,7 @@ public class ComputerPlayer {
|
|||||||
return Board.WHITE_WINS;
|
return Board.WHITE_WINS;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
int bestMoveIndex=0;
|
ArrayList<Integer> bestMoveIndex=new ArrayList<Integer>();
|
||||||
int currentScore;
|
int currentScore;
|
||||||
int bestScoreSoFar;
|
int bestScoreSoFar;
|
||||||
if (bitboard.getCurrentPlayer()==Piece.BLACK) {
|
if (bitboard.getCurrentPlayer()==Piece.BLACK) {
|
||||||
@ -89,9 +89,12 @@ public class ComputerPlayer {
|
|||||||
Board boardCopy = new Board(bitboard);
|
Board boardCopy = new Board(bitboard);
|
||||||
boardCopy.doMove(allLegalMoves.get(i));
|
boardCopy.doMove(allLegalMoves.get(i));
|
||||||
currentScore=MinMax(boardCopy,currentDepth+1);
|
currentScore=MinMax(boardCopy,currentDepth+1);
|
||||||
if (currentScore < bestScoreSoFar) { //black tries to minimise his score
|
if (currentScore <= bestScoreSoFar) { //black tries to minimise his score
|
||||||
bestScoreSoFar = currentScore;
|
if (currentScore<bestScoreSoFar) {
|
||||||
bestMoveIndex = i;
|
bestScoreSoFar = currentScore;
|
||||||
|
bestMoveIndex.clear();
|
||||||
|
}
|
||||||
|
bestMoveIndex.add(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else { //white piece
|
} else { //white piece
|
||||||
@ -100,13 +103,20 @@ public class ComputerPlayer {
|
|||||||
Board boardCopy = new Board(bitboard);
|
Board boardCopy = new Board(bitboard);
|
||||||
boardCopy.doMove(allLegalMoves.get(i));
|
boardCopy.doMove(allLegalMoves.get(i));
|
||||||
currentScore=MinMax(boardCopy,currentDepth+1);
|
currentScore=MinMax(boardCopy,currentDepth+1);
|
||||||
if (currentScore > bestScoreSoFar) { //white tries to maximise his score
|
if (currentScore >= bestScoreSoFar) { //white tries to maximise his score
|
||||||
bestScoreSoFar = currentScore;
|
if (currentScore>bestScoreSoFar) {
|
||||||
bestMoveIndex = i;
|
bestScoreSoFar = currentScore;
|
||||||
|
bestMoveIndex.clear();
|
||||||
|
}
|
||||||
|
bestMoveIndex.add(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bestMove = allLegalMoves.get(bestMoveIndex);
|
|
||||||
|
//select one of the best moves randomly
|
||||||
|
Random generator = new Random();
|
||||||
|
bestMove = allLegalMoves.get(bestMoveIndex.get(generator.nextInt(bestMoveIndex.size())));
|
||||||
|
|
||||||
return bestScoreSoFar;
|
return bestScoreSoFar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,14 +66,14 @@ public class Rules {
|
|||||||
public static boolean isThereALegalMovesForPlayer(Board board) throws NotAValidSquare {
|
public static boolean isThereALegalMovesForPlayer(Board board) throws NotAValidSquare {
|
||||||
legalMovesNonCapture = new ArrayList<Move>();
|
legalMovesNonCapture = new ArrayList<Move>();
|
||||||
legalMovesCapture = new ArrayList<Move>();
|
legalMovesCapture = new ArrayList<Move>();
|
||||||
|
|
||||||
Square square;
|
Square square;
|
||||||
for(int squareNb = 0; squareNb < Board.NB_OF_SQUARES; squareNb++) {
|
for(int squareNb = 0; squareNb < Board.NB_OF_SQUARES; squareNb++) {
|
||||||
square = new Square(squareNb);
|
square = new Square(squareNb);
|
||||||
if (board.isEmpty(square, new Piece(board.getCurrentPlayer()))) {
|
if (board.isEmpty(square, new Piece(board.getCurrentPlayer()))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ((legalMovesNonCapture.size()!=0)||(legalMovesCapture.size()!=0)) {
|
legalMovesFromSquare(square,board);
|
||||||
|
if ((legalMovesCapture.size()!=0)||(legalMovesNonCapture.size()!=0)) {
|
||||||
return true; //found a legal move
|
return true; //found a legal move
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ public class SuicideChess {
|
|||||||
/**
|
/**
|
||||||
* The name to be displayed
|
* The name to be displayed
|
||||||
*/
|
*/
|
||||||
public static final String NAME = "djib's SuicideChess v0.4.1";
|
public static final String NAME = "djib's SuShi (Suicide Chess) v0.4.3";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Displays informations in the console.
|
* Displays informations in the console.
|
||||||
@ -197,7 +197,7 @@ public class SuicideChess {
|
|||||||
}
|
}
|
||||||
//if XBoard did not accept usermove command we try and interpret every unknown command
|
//if XBoard did not accept usermove command we try and interpret every unknown command
|
||||||
//as a move.
|
//as a move.
|
||||||
case XBoardProtocol.MOVE:
|
case XBoardProtocol.MOVE:
|
||||||
Move theMove;
|
Move theMove;
|
||||||
if (acceptedUsermove) {
|
if (acceptedUsermove) {
|
||||||
theMove = new Move(whatMove.substring(9), bitboard);
|
theMove = new Move(whatMove.substring(9), bitboard);
|
||||||
@ -205,6 +205,13 @@ public class SuicideChess {
|
|||||||
theMove = new Move(whatMove, bitboard);
|
theMove = new Move(whatMove, bitboard);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (testWinningPosition(bitboard)) {
|
||||||
|
//if board was set in an illegal position
|
||||||
|
System.out.println("Illegal move: "+theMove.toString());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
boolean needToCapture = false;
|
boolean needToCapture = false;
|
||||||
int foundMoveIndex = -1;
|
int foundMoveIndex = -1;
|
||||||
if(theMove.getMovingPiece().getColor() == bitboard.getCurrentPlayer()) {
|
if(theMove.getMovingPiece().getColor() == bitboard.getCurrentPlayer()) {
|
||||||
@ -258,9 +265,16 @@ public class SuicideChess {
|
|||||||
case XBoardProtocol.GO:
|
case XBoardProtocol.GO:
|
||||||
//this is not really nice but it avoids having to write twice the code
|
//this is not really nice but it avoids having to write twice the code
|
||||||
//I check if I did really receive a XBoardProtocol.GO or it I just got there
|
//I check if I did really receive a XBoardProtocol.GO or it I just got there
|
||||||
//because there is no break statement.
|
//because there is no break statement.
|
||||||
if (xBoardCommand==XBoardProtocol.GO)
|
if (xBoardCommand==XBoardProtocol.GO)
|
||||||
computerPlaying = true;
|
computerPlaying = true;
|
||||||
|
|
||||||
|
if (testWinningPosition(bitboard)) {
|
||||||
|
//in case an illegal position have been sent
|
||||||
|
computerPlaying=false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (computerPlaying) {
|
if (computerPlaying) {
|
||||||
Move computerMove = ComputerPlayer.doMinMaxMove(bitboard);
|
Move computerMove = ComputerPlayer.doMinMaxMove(bitboard);
|
||||||
bitboard.doMove(computerMove);
|
bitboard.doMove(computerMove);
|
||||||
@ -272,6 +286,7 @@ public class SuicideChess {
|
|||||||
bitboard.display();
|
bitboard.display();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (testWinningPosition(bitboard)) {
|
if (testWinningPosition(bitboard)) {
|
||||||
computerPlaying=false;
|
computerPlaying=false;
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,7 @@ public class XBoardProtocol {
|
|||||||
public static void initialise() throws IOException {
|
public static void initialise() throws IOException {
|
||||||
//done=1 is here to tell that the program has finish requesting features
|
//done=1 is here to tell that the program has finish requesting features
|
||||||
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 myname=\"djib's Suicide Chess\"");
|
System.out.println("feature myname=\""+SuicideChess.NAME+"\"");
|
||||||
System.out.println("feature sigint=0 sigterm=0"); //do not interrupt me
|
System.out.println("feature sigint=0 sigterm=0"); //do not interrupt me
|
||||||
System.out.println("feature variants=\"suicide\"");
|
System.out.println("feature variants=\"suicide\"");
|
||||||
System.out.println("feature ping=1 setboard=1");
|
System.out.println("feature ping=1 setboard=1");
|
||||||
|
Reference in New Issue
Block a user