Version 6.9
=========== Now detects draw (50 moves only) Displays time to find move Can read a library of problems from a file Each problem can be selected independently or all can be played in succession and then the result is displayed.
This commit is contained in:
@ -5,8 +5,11 @@ import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
import suicideChess.Board.NoPieceOnSquare;
|
||||
import suicideChess.Board.UnableToParseFENStringException;
|
||||
import suicideChess.Move.NotAValidMoveException;
|
||||
import suicideChess.Square.NotAValidSquare;
|
||||
import suicideChess.SuicideProblems.NoSuchSuicideProblem;
|
||||
|
||||
/**
|
||||
* Main file (the game in itself)
|
||||
@ -33,7 +36,7 @@ public class SuicideChess {
|
||||
/**
|
||||
* The name to be displayed
|
||||
*/
|
||||
public static final String NAME = "djib's SuShi v0.5.5";
|
||||
public static final String NAME = "djib's SuShi v0.6.9";
|
||||
|
||||
/**
|
||||
* Displays informations in the console.
|
||||
@ -51,13 +54,16 @@ public class SuicideChess {
|
||||
* @return True or False
|
||||
*/
|
||||
|
||||
private static boolean testWinningPosition (Board bitboard) {
|
||||
private static boolean testAndDisplayIfWinningOrDrawPosition (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;
|
||||
} else if (bitboard.isADraw()) {
|
||||
System.out.println("1/2-1/2 {Draw}");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -134,164 +140,184 @@ public class SuicideChess {
|
||||
try {
|
||||
String whatMove= moveInput.readLine();
|
||||
boolean playedALegalMove = false;
|
||||
|
||||
int xBoardCommand = XBoardProtocol.getCommand(whatMove);
|
||||
|
||||
switch (xBoardCommand) {
|
||||
case XBoardProtocol.XBOARD:
|
||||
break;
|
||||
case XBoardProtocol.PROTOVER:
|
||||
XBoardProtocol.initialise();
|
||||
break;
|
||||
case XBoardProtocol.NOT_ACCEPTED_SUICIDE:
|
||||
System.out.println("tellusererror \"This game only plays suicide chess.\"");
|
||||
playing=false;
|
||||
break;
|
||||
case XBoardProtocol.ACCEPTED_USERMOVE:
|
||||
acceptedUsermove=true;
|
||||
break;
|
||||
case XBoardProtocol.NOPROTOVER:
|
||||
System.out.println("tellusererror \"You must use an engine with XBoard protocol 2 or higher.\"");
|
||||
playing=false;
|
||||
break;
|
||||
case XBoardProtocol.QUIT:
|
||||
System.out.println("Goodbye!");
|
||||
playing=false;
|
||||
break;
|
||||
case XBoardProtocol.NEW:
|
||||
//System.out.println("variant suicide");
|
||||
break;
|
||||
case XBoardProtocol.HINT:
|
||||
System.out.println("Hint: "+ComputerPlayer.doRandomMove(bitboard));
|
||||
break;
|
||||
case XBoardProtocol.FORCE:
|
||||
computerPlaying = false;
|
||||
break;
|
||||
case XBoardProtocol.PING:
|
||||
System.out.println("pong "+whatMove.substring(5));
|
||||
break;
|
||||
case XBoardProtocol.REMOVE:
|
||||
removePlayedPosition();
|
||||
//no break here
|
||||
case XBoardProtocol.UNDO:
|
||||
bitboard=new Board(removePlayedPosition());
|
||||
if (ASCII_GAME) {
|
||||
bitboard.display();
|
||||
}
|
||||
break;
|
||||
case XBoardProtocol.POST:
|
||||
post=true;
|
||||
break;
|
||||
case XBoardProtocol.NOPOST:
|
||||
post=false;
|
||||
break;
|
||||
case XBoardProtocol.SETBOARD:
|
||||
bitboard=new Board(whatMove.substring(9));
|
||||
if(ASCII_GAME)
|
||||
bitboard.display();
|
||||
break;
|
||||
case XBoardProtocol.UNKNOWN:
|
||||
if (acceptedUsermove) {
|
||||
System.out.println("Error (unknown command): "+whatMove);
|
||||
break;
|
||||
}
|
||||
//if XBoard did not accept usermove command we try and interpret every unknown command
|
||||
//as a move.
|
||||
case XBoardProtocol.MOVE:
|
||||
Move theMove;
|
||||
if (acceptedUsermove) {
|
||||
theMove = new Move(whatMove.substring(9), bitboard);
|
||||
if (whatMove.startsWith("problem")) { //special case for problems
|
||||
if(whatMove.length()==7) {
|
||||
System.out.println("There are "+SuicideProblems.numberOfProblems()+" problems.");
|
||||
} else if (whatMove.substring(8).equals("auto")) {
|
||||
autoProblem();
|
||||
} else if ((whatMove.length()>=12)&&(whatMove.substring(8,12).equals("load"))) {
|
||||
SuicideProblems.suicideProblemsLoad(whatMove.substring(13));
|
||||
} else {
|
||||
theMove = new Move(whatMove, bitboard);
|
||||
try {
|
||||
int problemNb = Integer.parseInt(whatMove.substring(8));
|
||||
bitboard=new Board(SuicideProblems.getProblemNumber(problemNb));
|
||||
if(ASCII_GAME)
|
||||
bitboard.display();
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Not a valid number: "+ whatMove.substring(8));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int xBoardCommand = XBoardProtocol.getCommand(whatMove);
|
||||
|
||||
if (testWinningPosition(bitboard)) {
|
||||
//if board was set in an illegal position
|
||||
System.out.println("Illegal move: "+theMove.toString());
|
||||
switch (xBoardCommand) {
|
||||
case XBoardProtocol.XBOARD:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
boolean needToCapture = false;
|
||||
int foundMoveIndex = -1;
|
||||
if(theMove.getMovingPiece().getColor() == bitboard.getCurrentPlayer()) {
|
||||
Rules.legalMovesForPlayer(bitboard);
|
||||
ArrayList<Move> allLegalMoves = Rules.getLegalMovesCapture();
|
||||
if (allLegalMoves.size()==0) {
|
||||
allLegalMoves = Rules.getLegalMovesNonCapture();
|
||||
case XBoardProtocol.PROTOVER:
|
||||
XBoardProtocol.initialise();
|
||||
break;
|
||||
case XBoardProtocol.NOT_ACCEPTED_SUICIDE:
|
||||
System.out.println("tellusererror \"This game only plays suicide chess.\"");
|
||||
playing=false;
|
||||
break;
|
||||
case XBoardProtocol.ACCEPTED_USERMOVE:
|
||||
acceptedUsermove=true;
|
||||
break;
|
||||
case XBoardProtocol.NOPROTOVER:
|
||||
System.out.println("tellusererror \"You must use an engine with XBoard protocol 2 or higher.\"");
|
||||
playing=false;
|
||||
break;
|
||||
case XBoardProtocol.QUIT:
|
||||
System.out.println("Goodbye!");
|
||||
playing=false;
|
||||
break;
|
||||
case XBoardProtocol.NEW:
|
||||
//System.out.println("variant suicide");
|
||||
break;
|
||||
case XBoardProtocol.HINT:
|
||||
System.out.println("Hint: "+ComputerPlayer.doRandomMove(bitboard));
|
||||
break;
|
||||
case XBoardProtocol.FORCE:
|
||||
computerPlaying = false;
|
||||
break;
|
||||
case XBoardProtocol.PING:
|
||||
System.out.println("pong "+whatMove.substring(5));
|
||||
break;
|
||||
case XBoardProtocol.REMOVE:
|
||||
removePlayedPosition();
|
||||
//no break here
|
||||
case XBoardProtocol.UNDO:
|
||||
bitboard=new Board(removePlayedPosition());
|
||||
if (ASCII_GAME) {
|
||||
bitboard.display();
|
||||
}
|
||||
break;
|
||||
case XBoardProtocol.POST:
|
||||
post=true;
|
||||
break;
|
||||
case XBoardProtocol.NOPOST:
|
||||
post=false;
|
||||
break;
|
||||
case XBoardProtocol.SETBOARD:
|
||||
bitboard=new Board(whatMove.substring(9));
|
||||
if(ASCII_GAME)
|
||||
bitboard.display();
|
||||
break;
|
||||
case XBoardProtocol.UNKNOWN:
|
||||
if (acceptedUsermove) {
|
||||
System.out.println("Error (unknown command): "+whatMove);
|
||||
break;
|
||||
}
|
||||
//if XBoard did not accept usermove command we try and interpret every unknown command
|
||||
//as a move.
|
||||
case XBoardProtocol.MOVE:
|
||||
Move theMove;
|
||||
if (acceptedUsermove) {
|
||||
theMove = new Move(whatMove.substring(9), bitboard);
|
||||
} else {
|
||||
needToCapture = true;
|
||||
theMove = new Move(whatMove, bitboard);
|
||||
}
|
||||
for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++) {
|
||||
if (allLegalMoves.get(moveIndex).isSimpleEqual(theMove)) {
|
||||
if(theMove.isPromotionMove()&&
|
||||
theMove.getPromotionPiece().getPieceNumber()!=allLegalMoves.get(moveIndex).getPromotionPiece().getPieceNumber()) {
|
||||
continue;
|
||||
}
|
||||
foundMoveIndex=moveIndex;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundMoveIndex == -1) {
|
||||
if (needToCapture) {
|
||||
if (ASCII_GAME)
|
||||
System.out.println("Capturing is mandatory.");
|
||||
}
|
||||
|
||||
if (testAndDisplayIfWinningOrDrawPosition(bitboard)) {
|
||||
//if board was set in an illegal position
|
||||
System.out.println("Illegal move: "+theMove.toString());
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
boolean needToCapture = false;
|
||||
int foundMoveIndex = -1;
|
||||
if(theMove.getMovingPiece().getColor() == bitboard.getCurrentPlayer()) {
|
||||
Rules.legalMovesForPlayer(bitboard);
|
||||
ArrayList<Move> allLegalMoves = Rules.getLegalMovesCapture();
|
||||
if (allLegalMoves.size()==0) {
|
||||
allLegalMoves = Rules.getLegalMovesNonCapture();
|
||||
} else {
|
||||
needToCapture = true;
|
||||
}
|
||||
for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++) {
|
||||
if (allLegalMoves.get(moveIndex).isSimpleEqual(theMove)) {
|
||||
if(theMove.isPromotionMove()&&
|
||||
theMove.getPromotionPiece().getPieceNumber()!=allLegalMoves.get(moveIndex).getPromotionPiece().getPieceNumber()) {
|
||||
continue;
|
||||
}
|
||||
foundMoveIndex=moveIndex;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundMoveIndex == -1) {
|
||||
if (needToCapture) {
|
||||
if (ASCII_GAME)
|
||||
System.out.println("Capturing is mandatory.");
|
||||
}
|
||||
System.out.println("Illegal move: "+theMove.toString());
|
||||
} else {
|
||||
bitboard.doMove(allLegalMoves.get(foundMoveIndex));
|
||||
addPlayedPosition(bitboard);
|
||||
if (ASCII_GAME) {
|
||||
allLegalMoves.get(foundMoveIndex).display();
|
||||
System.out.println("Board value: "+bitboard.getBoardValue());
|
||||
bitboard.display();
|
||||
}
|
||||
playedALegalMove=true;
|
||||
}
|
||||
} else {
|
||||
bitboard.doMove(allLegalMoves.get(foundMoveIndex));
|
||||
addPlayedPosition(bitboard);
|
||||
if (ASCII_GAME)
|
||||
System.out.println("Please play a piece of the right color.");
|
||||
System.out.println("Illegal move: "+theMove.toString());
|
||||
}
|
||||
|
||||
if (testAndDisplayIfWinningOrDrawPosition(bitboard)) {
|
||||
computerPlaying=false;
|
||||
}
|
||||
|
||||
if (!playedALegalMove) {
|
||||
break;
|
||||
}
|
||||
//No break statement here on purpose.
|
||||
case XBoardProtocol.GO:
|
||||
//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
|
||||
//because there is no break statement.
|
||||
if (xBoardCommand==XBoardProtocol.GO)
|
||||
computerPlaying = true;
|
||||
|
||||
if (testAndDisplayIfWinningOrDrawPosition(bitboard)) {
|
||||
//in case an illegal position have been sent
|
||||
computerPlaying=false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (computerPlaying) {
|
||||
Move computerMove = ComputerPlayer.doAlphaBetaMove(bitboard);
|
||||
bitboard.doMove(computerMove);
|
||||
addPlayedPosition(bitboard);
|
||||
XBoardProtocol.doMove(computerMove);
|
||||
if (ASCII_GAME) {
|
||||
allLegalMoves.get(foundMoveIndex).display();
|
||||
computerMove.display();
|
||||
System.out.println("Board value: "+bitboard.getBoardValue());
|
||||
bitboard.display();
|
||||
}
|
||||
playedALegalMove=true;
|
||||
}
|
||||
|
||||
if (testAndDisplayIfWinningOrDrawPosition(bitboard)) {
|
||||
computerPlaying=false;
|
||||
}
|
||||
} else {
|
||||
if (ASCII_GAME)
|
||||
System.out.println("Please play a piece of the right color.");
|
||||
System.out.println("Illegal move: "+theMove.toString());
|
||||
}
|
||||
|
||||
if (testWinningPosition(bitboard)) {
|
||||
computerPlaying=false;
|
||||
}
|
||||
|
||||
if (!playedALegalMove) {
|
||||
|
||||
break;
|
||||
}
|
||||
//No break statement here on purpose.
|
||||
case XBoardProtocol.GO:
|
||||
//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
|
||||
//because there is no break statement.
|
||||
if (xBoardCommand==XBoardProtocol.GO)
|
||||
computerPlaying = true;
|
||||
|
||||
if (testWinningPosition(bitboard)) {
|
||||
//in case an illegal position have been sent
|
||||
computerPlaying=false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (computerPlaying) {
|
||||
Move computerMove = ComputerPlayer.doAlphaBetaMove(bitboard);
|
||||
bitboard.doMove(computerMove);
|
||||
addPlayedPosition(bitboard);
|
||||
XBoardProtocol.doMove(computerMove);
|
||||
if (ASCII_GAME) {
|
||||
computerMove.display();
|
||||
System.out.println("Board value: "+bitboard.getBoardValue());
|
||||
bitboard.display();
|
||||
}
|
||||
}
|
||||
|
||||
if (testWinningPosition(bitboard)) {
|
||||
computerPlaying=false;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// if (whatMove.startsWith("hint")) {
|
||||
@ -330,10 +356,39 @@ public class SuicideChess {
|
||||
} catch (NotAValidSquare err) {
|
||||
System.out.println(err);
|
||||
continue;
|
||||
} catch (NoSuchSuicideProblem err) {
|
||||
System.out.println(err);
|
||||
continue;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//lets the computer try every problem and stop on error.
|
||||
//in the end it says what games where lost by white.
|
||||
private static void autoProblem() throws NotAValidSquare, UnableToParseFENStringException, NoPieceOnSquare, NoSuchSuicideProblem {
|
||||
Board bitboard;
|
||||
boolean[] result=new boolean[SuicideProblems.numberOfProblems()];
|
||||
for(int i=1; i<=SuicideProblems.numberOfProblems(); i++) {
|
||||
System.out.println("\n\nProblem "+i);
|
||||
bitboard=new Board(SuicideProblems.getProblemNumber(i));
|
||||
while(!testAndDisplayIfWinningOrDrawPosition(bitboard)) {
|
||||
Move computerMove = ComputerPlayer.doAlphaBetaMove(bitboard);
|
||||
bitboard.doMove(computerMove);
|
||||
}
|
||||
if (bitboard.getCurrentPlayer()==Piece.BLACK) {
|
||||
result[i-1]=false;
|
||||
} else {
|
||||
result[i-1]=true;
|
||||
}
|
||||
}
|
||||
System.out.println("\n\nLost following games:\n========begin========");
|
||||
for(int i=1; i<=SuicideProblems.numberOfProblems(); i++) {
|
||||
if (!result[i-1])
|
||||
System.out.println("Problem "+i+" lost !");
|
||||
}
|
||||
System.out.println("=========end=========");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user