Version 0.7.9

=============
Added open book
Added a weighted static evaluation function where
  the position of each piece is important
This commit is contained in:
2006-07-02 18:29:29 +00:00
parent b02755be4e
commit 8a641a6e1e
6 changed files with 231 additions and 36 deletions

View File

@ -32,12 +32,6 @@ public class Board {
private static final int NB_OF_BITBOARDS = 14; 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") @SuppressWarnings("serial")
public class NoPieceOnSquare extends Exception { public class NoPieceOnSquare extends Exception {
NoPieceOnSquare(String s) { super(s); }; NoPieceOnSquare(String s) { super(s); };
@ -74,11 +68,29 @@ public class Board {
/** /**
* Importance of real mobility in position evaluation (ie. how many moves can one make compared to the other) * 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) * 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 * * DATA *
@ -422,7 +434,6 @@ public class Board {
return enPassantSquare; return enPassantSquare;
} }
/** /**
* This function returns an integer representing the result of the static evaluation function * This function returns an integer representing the result of the static evaluation function
* for the current board * for the current board
@ -619,7 +630,13 @@ public class Board {
} else { } else {
//System.out.println("Playing midgame"); //System.out.println("Playing midgame");
for (int i = Piece.OFFSET; i<=Piece.MAX_PIECE_NUMBER; i++) { 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))); boardValue += ((mobility(Piece.WHITE)-mobility(Piece.BLACK)));
} }

View File

@ -153,7 +153,7 @@ public class ComputerPlayer {
bestMove = bestMoves.get(generator.nextInt(bestMoves.size())); bestMove = bestMoves.get(generator.nextInt(bestMoves.size()));
if (SuicideChess.postThinkingOutput()) { 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"+((int)(thinkingEndTime.getTime()-thinkingBeginingTime.getTime())/10)+ //search time in centiseconds
"\t"+nodesSearched+"\t"+bestScore.getPrincipalVariation()); "\t"+nodesSearched+"\t"+bestScore.getPrincipalVariation());
} }
@ -244,7 +244,7 @@ public class ComputerPlayer {
if (currentDepth==0) { if (currentDepth==0) {
bestMoves.clear(); bestMoves.clear();
if (SuicideChess.postThinkingOutput()) { 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"+((int)((new Date()).getTime()-thinkingBeginingTime.getTime())/10)+ //search time in centiseconds
"\t"+nodesSearched+"\t"+bestVariationSoFar); "\t"+nodesSearched+"\t"+bestVariationSoFar);
} }
@ -290,7 +290,7 @@ public class ComputerPlayer {
if (currentDepth==0) { if (currentDepth==0) {
bestMoves.clear(); bestMoves.clear();
if (SuicideChess.postThinkingOutput()) { 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"+((int)((new Date()).getTime()-thinkingBeginingTime.getTime())/10)+ //search time in centiseconds
"\t"+nodesSearched+"\t"+bestVariationSoFar); "\t"+nodesSearched+"\t"+bestVariationSoFar);
} }

View File

@ -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;
}
}

View File

@ -77,11 +77,11 @@ public class Piece {
public static final int[] PIECE_VALUE_MIDDLEGAME = new int[Piece.MAX_PIECE_NUMBER+1]; 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]; public static final int[] PIECE_VALUE_ENDGAME = new int[Piece.MAX_PIECE_NUMBER+1];
static { static {
PIECE_VALUE_MIDDLEGAME[WHITE_KING]=500; /* PIECE_VALUE_MIDDLEGAME[WHITE_KING]=500;
PIECE_VALUE_MIDDLEGAME[WHITE_QUEEN]=900; PIECE_VALUE_MIDDLEGAME[WHITE_QUEEN]=900;
PIECE_VALUE_MIDDLEGAME[WHITE_ROOK]=350; PIECE_VALUE_MIDDLEGAME[WHITE_ROOK]=350;
PIECE_VALUE_MIDDLEGAME[WHITE_KNIGHT]=300; PIECE_VALUE_MIDDLEGAME[WHITE_KNIGHT]=300;
PIECE_VALUE_MIDDLEGAME[WHITE_BISHOP]=-400; //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[WHITE_PAWN]= 300; //started with 100
PIECE_VALUE_MIDDLEGAME[BLACK_KING]=-PIECE_VALUE_MIDDLEGAME[WHITE_KING]; PIECE_VALUE_MIDDLEGAME[BLACK_KING]=-PIECE_VALUE_MIDDLEGAME[WHITE_KING];
PIECE_VALUE_MIDDLEGAME[BLACK_QUEEN]=-PIECE_VALUE_MIDDLEGAME[WHITE_QUEEN]; PIECE_VALUE_MIDDLEGAME[BLACK_QUEEN]=-PIECE_VALUE_MIDDLEGAME[WHITE_QUEEN];
@ -100,6 +100,30 @@ public class Piece {
PIECE_VALUE_ENDGAME[BLACK_ROOK]=-PIECE_VALUE_ENDGAME[WHITE_ROOK]; PIECE_VALUE_ENDGAME[BLACK_ROOK]=-PIECE_VALUE_ENDGAME[WHITE_ROOK];
PIECE_VALUE_ENDGAME[BLACK_KNIGHT]=-PIECE_VALUE_ENDGAME[WHITE_KNIGHT]; PIECE_VALUE_ENDGAME[BLACK_KNIGHT]=-PIECE_VALUE_ENDGAME[WHITE_KNIGHT];
PIECE_VALUE_ENDGAME[BLACK_BISHOP]=-PIECE_VALUE_ENDGAME[WHITE_BISHOP]; 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];
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_ENDGAME[BLACK_PAWN]=-PIECE_VALUE_ENDGAME[WHITE_PAWN];
} }

View File

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

View File

@ -5,7 +5,6 @@ import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
public class SuicideProblems {
/** /**
* This class is used to load problems from a file * This class is used to load problems from a file
* *
@ -13,16 +12,16 @@ public class SuicideProblems {
* @version $LastChangedRevision$, $LastChangedDate$ * @version $LastChangedRevision$, $LastChangedDate$
* *
*/ */
public class SuicideProblems {
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static class NoSuchSuicideProblem extends Exception { public static class NoSuchSuicideProblem extends Exception {
NoSuchSuicideProblem(String s) { super(s); }; NoSuchSuicideProblem(String s) { super(s); };
} }
//This array can hold setup for different problems. //This array can hold setup for different problems.
private static String[] suicideProblems; 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 * How many problems are loaded
@ -82,7 +81,7 @@ public class SuicideProblems {
} }
} }
suicideProblems = problemBuffer.toString().split("\n"); suicideProblems = problemBuffer.toString().split("\n");
System.out.println(numberOfProblems()+" loaded"); System.out.println(numberOfProblems()+" problem studies loaded.");
} }