Browse Source

Version 0.7.9

=============
Added open book
Added a weighted static evaluation function where
  the position of each piece is important
master
djib 16 years ago
parent
commit
8a641a6e1e
  1. 43
      src/suicideChess/Board.java
  2. 6
      src/suicideChess/ComputerPlayer.java
  3. 125
      src/suicideChess/OpeningBook.java
  4. 30
      src/suicideChess/Piece.java
  5. 44
      src/suicideChess/SuicideChess.java
  6. 19
      src/suicideChess/SuicideProblems.java

43
src/suicideChess/Board.java

@ -32,12 +32,6 @@ public class Board {
private static final int NB_OF_BITBOARDS = 14;
//with less than that many pawns on one side, the computer will enter endgame mode
public static final int ENDGAME_PAWNS = 3;
//with less than that many pieces on one side, the computer will enter endgame mode
public static final int ENDGAME_PIECES = 6;
@SuppressWarnings("serial")
public class NoPieceOnSquare extends Exception {
NoPieceOnSquare(String s) { super(s); };
@ -74,16 +68,34 @@ public class Board {
/**
* Importance of real mobility in position evaluation (ie. how many moves can one make compared to the other)
*/
public static final int REAL_MOBILITY_VALUE = 10;
public static final int REAL_MOBILITY_VALUE = 1000; //10;
/**
* Importance of relative mobility (mobility of other pieces that may not be able to play because of a compulsory move)
*/
public static final int RELATIVE_MOBILITY_VALUE = 5;
public static final int RELATIVE_MOBILITY_VALUE = 1000; //5;
//with less than that many pawns on one side, the computer will enter endgame mode
public static final int ENDGAME_PAWNS = 3;
//with less than that many pieces on one side, the computer will enter endgame mode
public static final int ENDGAME_PIECES = 8;
public static final int[] SQUARE_WEIGHT = {
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 0, 3, 5, 5, 3, 0, -10,
-10, 2, 15, 15, 15, 15, 2, -10,
-10, 7, 15, 25, 25, 15, 7, -10,
-10, 7, 15, 25, 25, 15, 7, -10,
-10, 2, 15, 15, 15, 15, 2, -10,
-10, 0, 3, 5, 5, 3, 0, -10,
-20, -10, -10, -10, -10, -10, -10, -20
};
/*======*
* DATA *
*======*/
//The following table is used to map squares to bits
protected static long mapSquaresToBits[];
@ -421,8 +433,7 @@ public class Board {
public Square getEnPassantSquare() {
return enPassantSquare;
}
/**
* This function returns an integer representing the result of the static evaluation function
* for the current board
@ -619,7 +630,13 @@ public class Board {
} else {
//System.out.println("Playing midgame");
for (int i = Piece.OFFSET; i<=Piece.MAX_PIECE_NUMBER; i++) {
boardValue += numberOfPieces[i]*Piece.PIECE_VALUE_MIDDLEGAME[i];
//boardValue += numberOfPieces[i]*Piece.PIECE_VALUE_MIDDLEGAME[i];
for(int squareNb = 0; squareNb<NB_OF_SQUARES; squareNb++) {
Piece pieceOnSquare = getPiece(new Square(squareNb));
if(pieceOnSquare.getPieceNumber()!=Piece.NONE) {
boardValue += SQUARE_WEIGHT[squareNb]*Piece.PIECE_VALUE_MIDDLEGAME[pieceOnSquare.getColor()];
}
}
}
boardValue += ((mobility(Piece.WHITE)-mobility(Piece.BLACK)));
}

6
src/suicideChess/ComputerPlayer.java

@ -153,7 +153,7 @@ public class ComputerPlayer {
bestMove = bestMoves.get(generator.nextInt(bestMoves.size()));
if (SuicideChess.postThinkingOutput()) {
System.out.println(maxDepth+"\t"+bestScore.getBranchValue()*100+
System.out.println(maxDepth+"\t"+bestScore.getBranchValue()+
"\t"+((int)(thinkingEndTime.getTime()-thinkingBeginingTime.getTime())/10)+ //search time in centiseconds
"\t"+nodesSearched+"\t"+bestScore.getPrincipalVariation());
}
@ -244,7 +244,7 @@ public class ComputerPlayer {
if (currentDepth==0) {
bestMoves.clear();
if (SuicideChess.postThinkingOutput()) {
System.out.println(maxDepth+"\t"+returnValue.getBranchValue()*100+
System.out.println(maxDepth+"\t"+returnValue.getBranchValue()+
"\t"+((int)((new Date()).getTime()-thinkingBeginingTime.getTime())/10)+ //search time in centiseconds
"\t"+nodesSearched+"\t"+bestVariationSoFar);
}
@ -290,7 +290,7 @@ public class ComputerPlayer {
if (currentDepth==0) {
bestMoves.clear();
if (SuicideChess.postThinkingOutput()) {
System.out.println(maxDepth+"\t"+returnValue.getBranchValue()*100+
System.out.println(maxDepth+"\t"+returnValue.getBranchValue()+
"\t"+((int)((new Date()).getTime()-thinkingBeginingTime.getTime())/10)+ //search time in centiseconds
"\t"+nodesSearched+"\t"+bestVariationSoFar);
}

125
src/suicideChess/OpeningBook.java

@ -0,0 +1,125 @@
package suicideChess;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import suicideChess.Move.NotAValidMoveException;
import suicideChess.Square.NotAValidSquare;
/**
* This class is used to play the first few moves of a game
*
* @author Jean-Baptiste H&eacute;tier
* @version $LastChangedRevision$, $LastChangedDate$
*
*/
public class OpeningBook {
@SuppressWarnings("serial")
public static class NoOpeningMovesLeft extends Exception {
NoOpeningMovesLeft(String s) { super(s); };
}
//This array will hold all book variants.
private static ArrayList<String[]> book; //each arrayList is an array of one possible opening moves (index 0 is the first move).
static void load() { openingBookLoad("openbook"); reset();} //initialise with file 'problems' if found in current directory
/**
* This function is used to load an openingbook file
* @param file
*/
public static void openingBookLoad(String file) {
//declared here only to make visible to finally clause
BufferedReader problemReader = null;
book = new ArrayList<String[]>();
try {
problemReader = new BufferedReader(new FileReader(file));
String line = null; //not declared within while loop
while ((line = problemReader.readLine()) != null) {
if (!line.startsWith("#")) { //ignore lines starting with # (comments)
book.add(line.split("\\s")); //each space defines a new move
}
}
}
catch (FileNotFoundException e) {
System.out.println("File '"+file+"' not found. Opening book won't be available.");
}
catch (IOException e){
System.out.println("Error reading file '"+file+"'.");
}
finally {
try {
if (problemReader!= null) {
problemReader.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println(book.size()+" opening variants loaded.");
}
// this variable is the currentBook
private static boolean[] validMoves;
/**
* This function should be called everytime a game is reset
*/
public static void reset() {
validMoves = new boolean[book.size()];
for (int i=0; i<validMoves.length; i++)
validMoves[i]=true; //all moves are valid
nbOfMovesThatHaveBeenPlayed = 0;
}
//used to access the correct index in the list of moves
private static int nbOfMovesThatHaveBeenPlayed = 0;
/**
* everytime a move is played by the opposite player, the computer should be notified
* using this function. He will update the list of moves that can be played
*/
public static void played(Move move) {
for (int i=0; i<validMoves.length; i++) {
if(validMoves[i] && (book.get(i).length>=nbOfMovesThatHaveBeenPlayed)) {
if (!(book.get(i)[nbOfMovesThatHaveBeenPlayed].equals(move.toString()))) {
validMoves[i]=false; //this branch is not valid anymore
}
}
}
nbOfMovesThatHaveBeenPlayed++;
}
public static Move getMove(Board bitboard) throws NotAValidMoveException, NotAValidSquare, NoOpeningMovesLeft {
ArrayList<Move> possibleMoves = new ArrayList<Move>();
for (int i=0; i<validMoves.length; i++) {
if(validMoves[i] && (book.get(i).length>nbOfMovesThatHaveBeenPlayed)) {
possibleMoves.add(new Move(book.get(i)[nbOfMovesThatHaveBeenPlayed],bitboard));
if (SuicideChess.postThinkingOutput()) {
String formatVariation = "";
for(int j=nbOfMovesThatHaveBeenPlayed; j<book.get(i).length; j++) {
formatVariation += book.get(i)[j]+" ";
}
System.out.println(1+"\t"+0+"\t"+0+"\t"+validMoves.length+"\t"+formatVariation+" [Book Move]");
}
}
}
//select one of the possible moves randomly
if(possibleMoves.size()==0) {
throw new NoOpeningMovesLeft("No moves left in opening book.");
}
Random generator = new Random();
Move chosenMove = possibleMoves.get(generator.nextInt(possibleMoves.size()));
played(chosenMove);
return chosenMove;
}
}

30
src/suicideChess/Piece.java

@ -77,12 +77,36 @@ public class Piece {
public static final int[] PIECE_VALUE_MIDDLEGAME = new int[Piece.MAX_PIECE_NUMBER+1];
public static final int[] PIECE_VALUE_ENDGAME = new int[Piece.MAX_PIECE_NUMBER+1];
static {
PIECE_VALUE_MIDDLEGAME[WHITE_KING]=500;
/* PIECE_VALUE_MIDDLEGAME[WHITE_KING]=500;
PIECE_VALUE_MIDDLEGAME[WHITE_QUEEN]=900;
PIECE_VALUE_MIDDLEGAME[WHITE_ROOK]=350;
PIECE_VALUE_MIDDLEGAME[WHITE_KNIGHT]=300;
PIECE_VALUE_MIDDLEGAME[WHITE_BISHOP]=-400; //started with -100
PIECE_VALUE_MIDDLEGAME[WHITE_PAWN]=300; //started with 100
PIECE_VALUE_MIDDLEGAME[WHITE_BISHOP]= -100; //started with -100, -400, -200
PIECE_VALUE_MIDDLEGAME[WHITE_PAWN]= 300; //started with 100
PIECE_VALUE_MIDDLEGAME[BLACK_KING]=-PIECE_VALUE_MIDDLEGAME[WHITE_KING];
PIECE_VALUE_MIDDLEGAME[BLACK_QUEEN]=-PIECE_VALUE_MIDDLEGAME[WHITE_QUEEN];
PIECE_VALUE_MIDDLEGAME[BLACK_ROOK]=-PIECE_VALUE_MIDDLEGAME[WHITE_ROOK];
PIECE_VALUE_MIDDLEGAME[BLACK_KNIGHT]=-PIECE_VALUE_MIDDLEGAME[WHITE_KNIGHT];
PIECE_VALUE_MIDDLEGAME[BLACK_BISHOP]=-PIECE_VALUE_MIDDLEGAME[WHITE_BISHOP];
PIECE_VALUE_MIDDLEGAME[BLACK_PAWN]=-PIECE_VALUE_MIDDLEGAME[WHITE_PAWN];
PIECE_VALUE_ENDGAME[WHITE_KING]=-400;
PIECE_VALUE_ENDGAME[WHITE_QUEEN]=-400;
PIECE_VALUE_ENDGAME[WHITE_ROOK]=-100;
PIECE_VALUE_ENDGAME[WHITE_KNIGHT]=-700;
PIECE_VALUE_ENDGAME[WHITE_BISHOP]=-400;
PIECE_VALUE_ENDGAME[WHITE_PAWN]=-900;
PIECE_VALUE_ENDGAME[BLACK_KING]=-PIECE_VALUE_ENDGAME[WHITE_KING];
PIECE_VALUE_ENDGAME[BLACK_QUEEN]=-PIECE_VALUE_ENDGAME[WHITE_QUEEN];
PIECE_VALUE_ENDGAME[BLACK_ROOK]=-PIECE_VALUE_ENDGAME[WHITE_ROOK];
PIECE_VALUE_ENDGAME[BLACK_KNIGHT]=-PIECE_VALUE_ENDGAME[WHITE_KNIGHT];
PIECE_VALUE_ENDGAME[BLACK_BISHOP]=-PIECE_VALUE_ENDGAME[WHITE_BISHOP];
PIECE_VALUE_ENDGAME[BLACK_PAWN]=-PIECE_VALUE_ENDGAME[WHITE_PAWN]; */
PIECE_VALUE_MIDDLEGAME[WHITE_KING]=100;
PIECE_VALUE_MIDDLEGAME[WHITE_QUEEN]=100;
PIECE_VALUE_MIDDLEGAME[WHITE_ROOK]=100;
PIECE_VALUE_MIDDLEGAME[WHITE_KNIGHT]=100;
PIECE_VALUE_MIDDLEGAME[WHITE_BISHOP]= 100; //started with -100, -400, -200
PIECE_VALUE_MIDDLEGAME[WHITE_PAWN]= 100; //started with 100
PIECE_VALUE_MIDDLEGAME[BLACK_KING]=-PIECE_VALUE_MIDDLEGAME[WHITE_KING];
PIECE_VALUE_MIDDLEGAME[BLACK_QUEEN]=-PIECE_VALUE_MIDDLEGAME[WHITE_QUEEN];
PIECE_VALUE_MIDDLEGAME[BLACK_ROOK]=-PIECE_VALUE_MIDDLEGAME[WHITE_ROOK];

44
src/suicideChess/SuicideChess.java

@ -8,6 +8,7 @@ import java.util.ArrayList;
import suicideChess.Board.NoPieceOnSquare;
import suicideChess.Board.UnableToParseFENStringException;
import suicideChess.Move.NotAValidMoveException;
import suicideChess.OpeningBook.NoOpeningMovesLeft;
import suicideChess.Square.NotAValidSquare;
import suicideChess.SuicideProblems.NoSuchSuicideProblem;
@ -36,7 +37,7 @@ public class SuicideChess {
/**
* The name to be displayed
*/
public static final String NAME = "djib's SuShi v0.7";
public static final String NAME = "djib's SuShi v0.7.9";
/**
* Displays informations in the console.
@ -118,14 +119,14 @@ public class SuicideChess {
/**
* This variable says whether the computer should display a "thinking output"
*/
private static boolean post = true;
private static boolean postThinkingOutput = true;
/**
* Should computer display thinking output or not ?
* @return boolean
*/
public static boolean postThinkingOutput() {
return post;
return postThinkingOutput;
}
private static void displayPlayer(Board bitboard) {
@ -136,6 +137,13 @@ public class SuicideChess {
}
}
//this variable is used to decide whether or not the computer should use it's opening book
//private static boolean playingWithOpeningBook = true;
//this variable tells if the game is in the opening phase.
private static boolean openingPhase = true;
/**
* The main function
* @param args No parameters should be transmitted to this function.
@ -149,6 +157,9 @@ public class SuicideChess {
System.out.println("If you want a graphical interface, you can use XBoard, WinBoard or any compatible program.");
System.out.println();
OpeningBook.load();
SuicideProblems.load();
System.out.println();
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
Board bitboard = new Board();
@ -215,14 +226,19 @@ public class SuicideChess {
case XBoardProtocol.NEW:
bitboard=new Board();
addPlayedPosition(bitboard);
openingPhase = true;
OpeningBook.reset();
computerPlaying = true; //the computer does not play in foce mode.
bitboard.display();
if(playInACSII())
bitboard.display();
//System.out.println("variant suicide");
break;
case XBoardProtocol.HINT:
System.out.println("Hint: "+ComputerPlayer.doRandomMove(bitboard));
break;
case XBoardProtocol.FORCE:
openingPhase=false; //don't know what will happen next
computerPlaying = false;
break;
case XBoardProtocol.PING:
@ -236,15 +252,17 @@ public class SuicideChess {
if (asciiGame) {
bitboard.display();
}
openingPhase = false; //due to the way I implemented the opening book
break;
case XBoardProtocol.POST:
post=true;
postThinkingOutput=true;
break;
case XBoardProtocol.NOPOST:
post=false;
postThinkingOutput=false;
break;
case XBoardProtocol.SETBOARD:
bitboard=new Board(whatMove.substring(9));
openingPhase = false;
if(asciiGame)
bitboard.display();
break;
@ -305,6 +323,8 @@ public class SuicideChess {
System.out.println("Illegal move: "+theMove.toString());
} else {
bitboard.doMove(allLegalMoves.get(foundMoveIndex));
if(openingPhase)
OpeningBook.played(allLegalMoves.get(foundMoveIndex));
addPlayedPosition(bitboard);
if (asciiGame) {
//allLegalMoves.get(foundMoveIndex).display();
@ -341,7 +361,17 @@ public class SuicideChess {
}
if (computerPlaying) {
Move computerMove = ComputerPlayer.doAlphaBetaMove(bitboard);
Move computerMove;
if(openingPhase) {
try {
computerMove = OpeningBook.getMove(bitboard);
} catch (NoOpeningMovesLeft e) {
openingPhase = false;
computerMove = ComputerPlayer.doAlphaBetaMove(bitboard);
}
} else {
computerMove = ComputerPlayer.doAlphaBetaMove(bitboard);
}
bitboard.doMove(computerMove);
addPlayedPosition(bitboard);
XBoardProtocol.doMove(computerMove);

19
src/suicideChess/SuicideProblems.java

@ -5,24 +5,23 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* This class is used to load problems from a file
*
* @author Jean-Baptiste H&eacute;tier
* @version $LastChangedRevision$, $LastChangedDate$
*
*/
public class SuicideProblems {
/**
* This class is used to load problems from a file
*
* @author Jean-Baptiste H&eacute;tier
* @version $LastChangedRevision$, $LastChangedDate$
*
*/
@SuppressWarnings("serial")
public static class NoSuchSuicideProblem extends Exception {
NoSuchSuicideProblem(String s) { super(s); };
}
//This array can hold setup for different problems.
private static String[] suicideProblems;
static { suicideProblemsLoad("problems"); } //initialise with file 'problems' if found in current directory
static void load() { suicideProblemsLoad("problems"); } //initialise with file 'problems' if found in current directory
/**
* How many problems are loaded
@ -82,7 +81,7 @@ public class SuicideProblems {
}
}
suicideProblems = problemBuffer.toString().split("\n");
System.out.println(numberOfProblems()+" loaded");
System.out.println(numberOfProblems()+" problem studies loaded.");
}

Loading…
Cancel
Save