Files
Sushi/src/suicideChess/SuicideChess.java
djib 6cf9f27892 Version 0.3 beta 3:
Computer can now play black and white
For some reason the computer cannot play against himself.
I think it must be because of force that is not understood.
2006-01-24 16:16:04 +00:00

227 lines
9.4 KiB
Java

package suicideChess;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import suicideChess.Move.NotAValidMoveException;
import suicideChess.Square.NotAValidSquare;
/**
* Main file (the game in itself)
* @author Jean-Baptiste Hétier
* @version $LastChangedRevision$, $LastChangedDate$
*/
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;
/**
* The name to be displayed
*/
public static final String NAME = "djib's SuicideChess v0.3 beta 2";
/**
* Displays informations in the console.
*/
public static final boolean ASCII_GAME = false;
/**
* The color of the current player
*/
private static int currentPlayerColor = Piece.WHITE;
/**
* Change the color of the current player
*/
private static void changeCurrentPlayerColor() {
if(currentPlayerColor==Piece.WHITE) {
currentPlayerColor=Piece.BLACK;
if (ASCII_GAME)
System.out.println("Black: ");
} else {
currentPlayerColor=Piece.WHITE;
if (ASCII_GAME)
System.out.println("White: ");
}
}
/**
* 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();
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:
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) {
break;
}
changeCurrentPlayerColor();
//No break statement here on purpose.
case XBoardProtocol.GO:
Move computerMove = computer.doMove(bitboard,currentPlayerColor);
bitboard.doMove(computerMove);
XBoardProtocol.doMove(computerMove);
if (ASCII_GAME) {
computerMove.display();
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;
}
}
} catch (NotAValidSquare e) {
e.printStackTrace();
}
}
}