diff --git a/src/suicideChess/ComputerPlayer.java b/src/suicideChess/ComputerPlayer.java index 8d4c09c..3c23aa4 100644 --- a/src/suicideChess/ComputerPlayer.java +++ b/src/suicideChess/ComputerPlayer.java @@ -1,6 +1,7 @@ package suicideChess; import java.util.ArrayList; +import java.util.Random; import suicideChess.Square.NotAValidSquare; @@ -12,17 +13,13 @@ import suicideChess.Square.NotAValidSquare; public class ComputerPlayer { - private int color; - /** - * This constructor creates a computer that plays with the given color - * @param color An integer representing the color. (See {@link Piece}) - * @see Piece + * This constructor creates a computer. */ - public ComputerPlayer(int color) { - this.color = color; + public ComputerPlayer() { + //this.color = color; } - + /** * This asks the computer to compute a move * @param bitboard The current status of the {@link Board} @@ -31,17 +28,19 @@ public class ComputerPlayer { * @see Board * @see Move */ - public Move doMove(Board bitboard) throws NotAValidSquare { + public Move doMove(Board bitboard, int color) throws NotAValidSquare { + Random generator = new Random(); Rules.legalMovesForPlayer(bitboard,color); ArrayList allLegalMoves = Rules.getLegalMovesCapture(); if (allLegalMoves.size()==0) { allLegalMoves = Rules.getLegalMovesNonCapture(); } if (allLegalMoves.size()!=0) { - return allLegalMoves.get(0); + return allLegalMoves.get(generator.nextInt(allLegalMoves.size())); } throw new RuntimeException(); } -} + +} \ No newline at end of file diff --git a/src/suicideChess/Rules.java b/src/suicideChess/Rules.java index eb4504e..2238a38 100644 --- a/src/suicideChess/Rules.java +++ b/src/suicideChess/Rules.java @@ -162,6 +162,7 @@ public class Rules { } if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) { normalMove= false; //cannot move forward if there is a piece + bigJump=false; } if (captureLeft&& ((board.getPiece(toCaptureLeftSquare).getColor()==Piece.NO_COLOR) diff --git a/src/suicideChess/SuicideChess.java b/src/suicideChess/SuicideChess.java index 6c45a1c..bafb6a9 100644 --- a/src/suicideChess/SuicideChess.java +++ b/src/suicideChess/SuicideChess.java @@ -33,13 +33,34 @@ public class SuicideChess { /** * The name to be displayed */ - public static final String NAME = "djib's SuicideChess v0.1.9"; + 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. @@ -51,13 +72,12 @@ public class SuicideChess { BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in)); Board bitboard = new Board(); - if (ASCII_GAME) + if (ASCII_GAME) { bitboard.display(); - int currentPlayerColor = Piece.WHITE; - if (ASCII_GAME) System.out.println("White: "); + } - ComputerPlayer computer = new ComputerPlayer(Piece.BLACK); + ComputerPlayer computer = new ComputerPlayer(); boolean playing = true; @@ -139,20 +159,23 @@ public class SuicideChess { 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: "); - } + 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")) { diff --git a/src/suicideChess/XBoardProtocol.java b/src/suicideChess/XBoardProtocol.java index dbdeb03..5f8274d 100644 --- a/src/suicideChess/XBoardProtocol.java +++ b/src/suicideChess/XBoardProtocol.java @@ -42,6 +42,10 @@ public class XBoardProtocol { * Received an invalid move */ public static final int INVALID = 6; + /** + * XBoard tells the computer to play the current color + */ + public static final int GO = 7; /** * XBoard did not accept sending moves with usermove */ @@ -67,11 +71,12 @@ public class XBoardProtocol { 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(); } + System.out.println(""); System.out.println("feature myname=\"djib's Suicide Chess\""); System.out.println("feature sigint=0 sigterm=0"); System.out.println("feature usermove=1"); //sends moves like 'usermove e2e4' System.out.println("feature variants=\"suicide\""); - System.out.println("feature time=0 draw=0 reuse=0 analyse=0"); + System.out.println("feature time=0 draw=0 reuse=0 analyse=0 colors=0"); System.out.println("feature done=1"); if (SuicideChess.ASCII_GAME) System.out.println(); @@ -110,6 +115,8 @@ public class XBoardProtocol { return NOT_ACCEPTED_SUICIDE; } else if (command.equals("rejected usermove")) { return NOT_ACCEPTED_USERMOVE; + } else if (command.equals("go")) { + return GO; } return UNKNOWN;