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.
This commit is contained in:
2006-01-24 16:16:04 +00:00
parent dbfd428710
commit 6cf9f27892
4 changed files with 61 additions and 31 deletions

View File

@ -1,6 +1,7 @@
package suicideChess; package suicideChess;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random;
import suicideChess.Square.NotAValidSquare; import suicideChess.Square.NotAValidSquare;
@ -12,15 +13,11 @@ import suicideChess.Square.NotAValidSquare;
public class ComputerPlayer { public class ComputerPlayer {
private int color;
/** /**
* This constructor creates a computer that plays with the given color * This constructor creates a computer.
* @param color An integer representing the color. (See {@link Piece})
* @see Piece
*/ */
public ComputerPlayer(int color) { public ComputerPlayer() {
this.color = color; //this.color = color;
} }
/** /**
@ -31,17 +28,19 @@ public class ComputerPlayer {
* @see Board * @see Board
* @see Move * @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); Rules.legalMovesForPlayer(bitboard,color);
ArrayList<Move> allLegalMoves = Rules.getLegalMovesCapture(); ArrayList<Move> allLegalMoves = Rules.getLegalMovesCapture();
if (allLegalMoves.size()==0) { if (allLegalMoves.size()==0) {
allLegalMoves = Rules.getLegalMovesNonCapture(); allLegalMoves = Rules.getLegalMovesNonCapture();
} }
if (allLegalMoves.size()!=0) { if (allLegalMoves.size()!=0) {
return allLegalMoves.get(0); return allLegalMoves.get(generator.nextInt(allLegalMoves.size()));
} }
throw new RuntimeException(); throw new RuntimeException();
} }
} }

View File

@ -162,6 +162,7 @@ public class Rules {
} }
if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) { if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) {
normalMove= false; //cannot move forward if there is a piece normalMove= false; //cannot move forward if there is a piece
bigJump=false;
} }
if (captureLeft&& if (captureLeft&&
((board.getPiece(toCaptureLeftSquare).getColor()==Piece.NO_COLOR) ((board.getPiece(toCaptureLeftSquare).getColor()==Piece.NO_COLOR)

View File

@ -33,13 +33,34 @@ public class SuicideChess {
/** /**
* The name to be displayed * 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. * Displays informations in the console.
*/ */
public static final boolean ASCII_GAME = false; 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 * The main function
* @param args No parameters should be transmitted to this 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)); BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
Board bitboard = new Board(); Board bitboard = new Board();
if (ASCII_GAME) if (ASCII_GAME) {
bitboard.display(); bitboard.display();
int currentPlayerColor = Piece.WHITE;
if (ASCII_GAME)
System.out.println("White: "); System.out.println("White: ");
}
ComputerPlayer computer = new ComputerPlayer(Piece.BLACK); ComputerPlayer computer = new ComputerPlayer();
boolean playing = true; boolean playing = true;
@ -139,20 +159,23 @@ public class SuicideChess {
System.out.println("Illegal move: "+theMove.toString()); System.out.println("Illegal move: "+theMove.toString());
} }
if (playedALegalMove) { if (!playedALegalMove) {
currentPlayerColor = Piece.BLACK; break;
Move computerMove = computer.doMove(bitboard); }
changeCurrentPlayerColor();
//No break statement here on purpose.
case XBoardProtocol.GO:
Move computerMove = computer.doMove(bitboard,currentPlayerColor);
bitboard.doMove(computerMove); bitboard.doMove(computerMove);
XBoardProtocol.doMove(computerMove); XBoardProtocol.doMove(computerMove);
if (ASCII_GAME) { if (ASCII_GAME) {
computerMove.display(); computerMove.display();
bitboard.display(); bitboard.display();
} }
currentPlayerColor = Piece.WHITE; changeCurrentPlayerColor();
if (ASCII_GAME)
System.out.println("White: ");
}
break; break;
} }
// if (whatMove.startsWith("hint")) { // if (whatMove.startsWith("hint")) {

View File

@ -42,6 +42,10 @@ public class XBoardProtocol {
* Received an invalid move * Received an invalid move
*/ */
public static final int INVALID = 6; 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 * 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("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("");
System.out.println("feature myname=\"djib's Suicide Chess\""); System.out.println("feature myname=\"djib's Suicide Chess\"");
System.out.println("feature sigint=0 sigterm=0"); System.out.println("feature sigint=0 sigterm=0");
System.out.println("feature usermove=1"); //sends moves like 'usermove e2e4' System.out.println("feature usermove=1"); //sends moves like 'usermove e2e4'
System.out.println("feature variants=\"suicide\""); 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"); System.out.println("feature done=1");
if (SuicideChess.ASCII_GAME) if (SuicideChess.ASCII_GAME)
System.out.println(); System.out.println();
@ -110,6 +115,8 @@ public class XBoardProtocol {
return NOT_ACCEPTED_SUICIDE; return NOT_ACCEPTED_SUICIDE;
} else if (command.equals("rejected usermove")) { } else if (command.equals("rejected usermove")) {
return NOT_ACCEPTED_USERMOVE; return NOT_ACCEPTED_USERMOVE;
} else if (command.equals("go")) {
return GO;
} }
return UNKNOWN; return UNKNOWN;