Browse Source

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.
master
djib 16 years ago
parent
commit
6cf9f27892
  1. 21
      src/suicideChess/ComputerPlayer.java
  2. 1
      src/suicideChess/Rules.java
  3. 61
      src/suicideChess/SuicideChess.java
  4. 9
      src/suicideChess/XBoardProtocol.java

21
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<Move> 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();
}
}
}

1
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)

61
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")) {

9
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;

Loading…
Cancel
Save