A fully working version of the game.

Works with XBoard only (not eboard).

The program plays the first legal move.
This commit is contained in:
2006-01-17 15:18:53 +00:00
parent 42e56fde4f
commit dbfd428710
6 changed files with 356 additions and 139 deletions

View File

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import suicideChess.Move.NotAValidMoveException;
import suicideChess.Square.NotAValidSquare;
@ -14,142 +15,190 @@ import suicideChess.Square.NotAValidSquare;
*/
public class SuicideChess {
/*
* Those flags are used to perform extra checks during the debugging of the
* program. They may be safely all set to false once the program is stable.
* It should improve performance a lot.
*/
/**
* does BitBoard.class removePiece function checks if removing piece is legal ?
*/
public static final boolean BITBOARD_REMOVEPIECE_CHECK_REMOVE = true;
/**
* does Square.class checks if the strings are valid (is "z9" a valid square ?
*/
public static final boolean SQUARE_CHECK_INVALID = true;
private static final int MAIN_VERSION_NUMBER = 0;
private static final int REVISION_NUMBER = 19;
/**
* The main function
* @param args No parameters should be transmitted to this function.
*/
public static void main(String[] args) {
System.out.println(" Welcome to SuicideChess v"+MAIN_VERSION_NUMBER+"."+REVISION_NUMBER+"!");
System.out.println();
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
try {
Board bitboard = new Board();
bitboard.display();
int playerColor = Piece.WHITE;
System.out.println("White: ");
while (true) {
try {
String whatMove= moveInput.readLine();
/*
* Those flags are used to perform extra checks during the debugging of the
* program. They may be safely all set to false once the program is stable.
* It should improve performance a lot.
*/
/**
* does BitBoard.class removePiece function checks if removing piece is legal ?
*/
public static final boolean BITBOARD_REMOVEPIECE_CHECK_REMOVE = true;
/**
* does Square.class checks if the strings are valid (is "z9" a valid square ?)
*/
public static final boolean SQUARE_CHECK_INVALID = true;
/**
* The name to be displayed
*/
public static final String NAME = "djib's SuicideChess v0.1.9";
/**
* Displays informations in the console.
*/
public static final boolean ASCII_GAME = false;
/**
* The main function
* @param args No parameters should be transmitted to this function.
*/
public static void main(String[] args) {
try {
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
Board bitboard = new Board();
if (ASCII_GAME)
bitboard.display();
int currentPlayerColor = Piece.WHITE;
if (ASCII_GAME)
System.out.println("White: ");
ComputerPlayer computer = new ComputerPlayer(Piece.BLACK);
boolean playing = true;
while (playing) {
try {
String whatMove= moveInput.readLine();
boolean playedALegalMove = false;
if (whatMove.startsWith("quit")) {
System.out.println("Goodbye!");
break;
}
if (whatMove.startsWith("hint")) {
Rules rules = new Rules();
rules.legalMovesForPlayer(bitboard,playerColor);
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;
}
Move theMove = new Move(whatMove, bitboard);
Rules rules = new Rules();
boolean needToCapture = false;
int foundMoveIndex = -1;
if(theMove.getMovingPiece().getColor() == playerColor) {
rules.legalMovesForPlayer(bitboard,playerColor);
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) {
System.out.println("Capturing is mandatory.");
}
System.out.println("This move is not valid. Please type 'hint' to see list of legal moves.");
} else {
allLegalMoves.get(foundMoveIndex).display();
bitboard.doMove(allLegalMoves.get(foundMoveIndex));
bitboard.display();
playedALegalMove=true;
}
} else {
System.out.println("Please play a piece of the right color.");
}
if (playedALegalMove) {
if (playerColor == Piece.WHITE) {
playerColor = Piece.BLACK;
System.out.println("Black: ");
} else {
playerColor = Piece.WHITE;
System.out.println("White: ");
}
}
} 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();
}
}
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:
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;
}
} else {
if (ASCII_GAME)
System.out.println("Please play a piece of the right color.");
System.out.println("Illegal move: "+theMove.toString());
}
if (playedALegalMove) {
currentPlayerColor = Piece.BLACK;
Move computerMove = computer.doMove(bitboard);
bitboard.doMove(computerMove);
XBoardProtocol.doMove(computerMove);
if (ASCII_GAME) {
computerMove.display();
bitboard.display();
}
currentPlayerColor = Piece.WHITE;
if (ASCII_GAME)
System.out.println("White: ");
}
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();
}
}
}