Version 0.4

Now plays with a grandpa minmax.
It is much better, though not excellent ;)
This commit is contained in:
2006-01-26 18:05:16 +00:00
parent 6cf9f27892
commit 5266b582bb
5 changed files with 337 additions and 160 deletions

View File

@ -33,13 +33,18 @@ public class SuicideChess {
/**
* The name to be displayed
*/
public static final String NAME = "djib's SuicideChess v0.3 beta 2";
public static final String NAME = "djib's SuicideChess v0.4.1";
/**
* Displays informations in the console.
*/
public static final boolean ASCII_GAME = false;
/**
* Number of Plies the computes searches to
*/
public static final int PLY_DEPTH = 5;
/**
* The color of the current player
*/
@ -60,167 +65,200 @@ public class SuicideChess {
}
}
/**
* If feature usermove has not been accepted by XBoard then consider all unknown commands
* as moves
*/
private static boolean acceptedUsermove = false;
/**
* The main function
* @param args No parameters should be transmitted to this function.
* @throws NotAValidSquare
*/
public static void main(String[] args) {
public static void main(String[] args) throws NotAValidSquare {
System.out.println("Welcome to SuicideChess "+SuicideChess.NAME+"!");
if (!SuicideChess.ASCII_GAME) {
System.out.println("This game was not designed to be played in console. Please use it with XBoard, WinBoard or any compatible program.");
}
System.out.println();
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
Board bitboard = new Board();
if (ASCII_GAME) {
bitboard.display();
System.out.println("White: ");
}
ComputerPlayer computer = new ComputerPlayer();
boolean computerPlaying = true; //the computer does not play in foce mode.
boolean playing = true;
while (playing) {
try {
String whatMove= moveInput.readLine();
boolean playedALegalMove = false;
try {
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
Board bitboard = new Board();
if (ASCII_GAME) {
bitboard.display();
System.out.println("White: ");
}
ComputerPlayer computer = new ComputerPlayer();
boolean playing = true;
while (playing) {
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.NOT_ACCEPTED_USERMOVE:
System.out.println("tellusererror \"XBoard must send moves starting with 'usermove'\"");
playing=false;
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.UNKNOWN:
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: "+computer.doRandomMove(bitboard,currentPlayerColor));
break;
case XBoardProtocol.FORCE:
computerPlaying = false;
break;
case XBoardProtocol.UNKNOWN:
if (acceptedUsermove) {
System.out.println("Error (unknown command): "+whatMove);
break;
case XBoardProtocol.MOVE:
Move theMove = new Move(whatMove.substring(9), bitboard);
boolean needToCapture = false;
int foundMoveIndex = -1;
if(theMove.getMovingPiece().getColor() == currentPlayerColor) {
Rules.legalMovesForPlayer(bitboard,currentPlayerColor);
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));
if (ASCII_GAME) {
allLegalMoves.get(foundMoveIndex).display();
bitboard.display();
}
playedALegalMove=true;
}
}
//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 {
theMove = new Move(whatMove, bitboard);
}
boolean needToCapture = false;
int foundMoveIndex = -1;
if(theMove.getMovingPiece().getColor() == currentPlayerColor) {
Rules.legalMovesForPlayer(bitboard,currentPlayerColor);
ArrayList<Move> allLegalMoves = Rules.getLegalMovesCapture();
if (allLegalMoves.size()==0) {
allLegalMoves = Rules.getLegalMovesNonCapture();
} else {
if (ASCII_GAME)
System.out.println("Please play a piece of the right color.");
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));
if (ASCII_GAME) {
allLegalMoves.get(foundMoveIndex).display();
System.out.println("Board value: "+bitboard.getBoardValue());
bitboard.display();
}
playedALegalMove=true;
}
if (!playedALegalMove) {
break;
}
changeCurrentPlayerColor();
//No break statement here on purpose.
case XBoardProtocol.GO:
Move computerMove = computer.doMove(bitboard,currentPlayerColor);
} else {
if (ASCII_GAME)
System.out.println("Please play a piece of the right color.");
System.out.println("Illegal move: "+theMove.toString());
}
if (!playedALegalMove) {
break;
}
changeCurrentPlayerColor();
//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 (computerPlaying) {
Move computerMove = computer.doMinMaxMove(bitboard,currentPlayerColor);
bitboard.doMove(computerMove);
XBoardProtocol.doMove(computerMove);
if (ASCII_GAME) {
computerMove.display();
System.out.println("Board value: "+bitboard.getBoardValue());
bitboard.display();
}
changeCurrentPlayerColor();
break;
}
// if (whatMove.startsWith("hint")) {
// Rules rules = new Rules();
// rules.legalMovesForPlayer(bitboard,currentPlayerColor);
// ArrayList<Move> allLegalMoves = rules.getLegalMovesCapture();
// if (allLegalMoves.size()==0) {
// allLegalMoves = rules.getLegalMovesNonCapture();
// }
// for(int i = 0; i<allLegalMoves.size(); i++) {
// if(allLegalMoves.get(i).isPromotionMove()) {
// System.out.println(allLegalMoves.get(i).fromSquare().toString() +
// allLegalMoves.get(i).toSquare().toString() +
// allLegalMoves.get(i).getPromotionPiece().toString());
// } else {
// System.out.println(allLegalMoves.get(i).fromSquare().toString() +
// allLegalMoves.get(i).toSquare().toString());
// }
//
// }
// continue;
// }
// if (whatMove.startsWith("force")) {
// Move theMove = new Move(whatMove.substring(6,10), bitboard);
// theMove.display();
// bitboard.doMove(theMove);
// bitboard.display();
// continue;
// }
} catch (NotAValidMoveException err) {
System.out.println(err);
continue;
} catch (NotAValidSquare err) {
System.out.println(err);
continue;
} catch (Exception err) {
System.out.println(err);
}
break;
}
// if (whatMove.startsWith("hint")) {
// Rules rules = new Rules();
// rules.legalMovesForPlayer(bitboard,currentPlayerColor);
// ArrayList<Move> allLegalMoves = rules.getLegalMovesCapture();
// if (allLegalMoves.size()==0) {
// allLegalMoves = rules.getLegalMovesNonCapture();
// }
// for(int i = 0; i<allLegalMoves.size(); i++) {
// if(allLegalMoves.get(i).isPromotionMove()) {
// System.out.println(allLegalMoves.get(i).fromSquare().toString() +
// allLegalMoves.get(i).toSquare().toString() +
// allLegalMoves.get(i).getPromotionPiece().toString());
// } else {
// System.out.println(allLegalMoves.get(i).fromSquare().toString() +
// allLegalMoves.get(i).toSquare().toString());
// }
//
// }
// continue;
// }
// if (whatMove.startsWith("force")) {
// Move theMove = new Move(whatMove.substring(6,10), bitboard);
// theMove.display();
// bitboard.doMove(theMove);
// bitboard.display();
// continue;
// }
} catch (NotAValidMoveException err) {
System.out.println(err);
continue;
} catch (NotAValidSquare err) {
System.out.println(err);
continue;
} catch (Exception err) {
System.out.println(err);
break;
}
} catch (NotAValidSquare e) {
e.printStackTrace();
}
}