Version 0.8.9

=============
Added external config file -> the resulting program seems a lot faster !!!
Added help.
This commit is contained in:
2006-07-04 11:55:00 +00:00
parent a0ee66638d
commit f67d82d129
5 changed files with 319 additions and 204 deletions

View File

@ -4,7 +4,6 @@ import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* This class is used to read configuration settings for the AI
@ -15,95 +14,169 @@ import java.util.ArrayList;
*/
public class ConfigFile {
private static int[] pieceValuesMiddle;
private static int[] pieceValuesEnd;
private static int[] squareWeightMiddle;
private static int[] squareWeightEnd;
private static int primaryMobilityValueMiddle;
private static int primaryMobilityValueEnd;
private static int secondaryMobilityValueMiddle;
private static int secondaryMobilityValueEnd;
private static int[] pieceValuesMidgame;
private static int[] pieceValuesEndgame;
private static int[] squareWeightMidgame;
private static int[] squareWeightEndgame;
private static int primaryMobilityValueMidgame;
private static int primaryMobilityValueEndgame;
private static int secondaryMobilityValueMidgame;
private static int secondaryMobilityValueEndgame;
private static int endGamePawns;
private static int endGamePieces;
/**
* The pieces value in the middlegame
*/
public static int[] getPieceValuesMiddle() {
return pieceValuesMiddle;
public static int[] getPieceValuesMidgame() {
return pieceValuesMidgame;
}
/**
* The pieces value in the end
*/
public static int[] getPieceValuesEnd() {
return pieceValuesEnd;
}
/**
* The weight of each square in the middle game
*/
public static int[] getSquareWeightMiddle() {
return squareWeightMiddle;
}
/**
* The weight of each square in the endgame
*/
public static int[] getSquareWeightEnd() {
return squareWeightEnd;
}
/**
* The primary mobility value (nb of possible legal moves) in the midgame
*/
public static int getPrimaryMobilityValueMiddle() {
return primaryMobilityValueMiddle;
}
/**
* The primary mobility value (nb of possible legal moves) in the endgame
*/
public static int getPrimaryMobilityValueEnd() {
return primaryMobilityValueEnd;
}
/**
* The secondary mobility value (nb of possible but not legal moves) in the midgame
*/
public static int getSecondaryMobilityValueMiddle() {
return secondaryMobilityValueMiddle;
}
/**
* The secondary mobility value (nb of possible but not legal moves)
*/
public static int getScondaryMobilityValueEnd() {
return secondaryMobilityValueEnd;
}
/**
* Number of pawns on the opposite side before entering endgame
*/
public static int getEndGamePawns() {
return endGamePawns;
}
/**
* Number of pieces on the opposite site before entering endgame
*/
public static int getEndGamePieces() {
return endGamePieces;
}
public static int[] getPieceValuesEndgame() {
return pieceValuesEndgame;
}
/**
* The weight of each square in the middle game
*/
public static int[] getSquareWeightMidgame() {
return squareWeightMidgame;
}
/**
* The weight of each square in the endgame
*/
public static int[] getSquareWeightEndgame() {
return squareWeightEndgame;
}
/**
* The primary mobility value (nb of possible legal moves) in the midgame
*/
public static int getPrimaryMobilityValueMidgame() {
return primaryMobilityValueMidgame;
}
/**
* The primary mobility value (nb of possible legal moves) in the endgame
*/
public static int getPrimaryMobilityValueEndgame() {
return primaryMobilityValueEndgame;
}
/**
* The secondary mobility value (nb of possible but not legal moves) in the midgame
*/
public static int getSecondaryMobilityValueMidgame() {
return secondaryMobilityValueMidgame;
}
/**
* The secondary mobility value (nb of possible but not legal moves)
*/
public static int getScondaryMobilityValueEndgame() {
return secondaryMobilityValueEndgame;
}
/**
* Number of pawns on the opposite side before entering endgame
*/
public static int getEndGamePawns() {
return endGamePawns;
}
/**
* Number of pieces on the opposite site before entering endgame
*/
public static int getEndGamePieces() {
return endGamePieces;
}
public static void load(String file) {
/**
* Loads the default configfile
*/
public static void load() {
loadFile("config");
}
/**
* Loads a configuration file for the AI
* @param configFile
*/
public static void loadFile(String configFile) {
String configMessage = "Loaded custom : ";
// for displaying errors
String currentLine="";
int currentLineNumber=0;
//declared here only to make visible to finally clause
BufferedReader problemReader = null;
try {
problemReader = new BufferedReader(new FileReader(file));
problemReader = new BufferedReader(new FileReader(configFile));
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
}
currentLine=line;
currentLineNumber++;
if(line.startsWith("pvm")) {
String[] result=line.split("\t");
//start from 1 not to include "pvm"
for(int i=1; i<result.length; i++) {
pieceValuesMidgame[2*i]=Integer.parseInt(result[i]); //white pieces
pieceValuesMidgame[2*i+1]=-Integer.parseInt(result[i]); //black pieces
}
configMessage += "pvm ";
} else if(line.startsWith("pve")) {
String[] result=line.split("\t");
//start from 1 not to include "pvm"
for(int i=1; i<result.length; i++) {
pieceValuesEndgame[2*i]=Integer.parseInt(result[i]); //white pieces
pieceValuesEndgame[2*i+1]=-Integer.parseInt(result[i]); //black pieces
}
configMessage += "pve ";
} else if(line.startsWith("swm")) {
for(int rank=0; rank<Board.NB_OF_RANKS; rank++) {
if ((line = problemReader.readLine()) != null) {
String [] result=line.split("\t");
for (int file=0; file<Board.NB_OF_FILES; file++) {
squareWeightMidgame[file+Board.NB_OF_FILES*rank]=Integer.parseInt(result[file]);
}
}
}
configMessage += "swm ";
} else if(line.startsWith("swe")) {
for(int rank=0; rank<Board.NB_OF_RANKS; rank++) {
if ((line = problemReader.readLine()) != null) {
String [] result=line.split("\t");
for (int file=0; file<Board.NB_OF_FILES; file++) {
squareWeightEndgame[file+Board.NB_OF_FILES*rank]=Integer.parseInt(result[file]);
}
}
}
configMessage += "swe ";
} else if(line.startsWith("mvm")) {
String [] result=line.split("\t");
primaryMobilityValueMidgame = Integer.parseInt(result[1]);
secondaryMobilityValueMidgame = Integer.parseInt(result[2]);
configMessage += "mvm ";
} else if(line.startsWith("mve")) {
String [] result=line.split("\t");
primaryMobilityValueEndgame = Integer.parseInt(result[1]);
secondaryMobilityValueEndgame = Integer.parseInt(result[2]);
configMessage += "mve ";
} else if(line.startsWith("npp")) {
String [] result=line.split("\t");
endGamePawns = Integer.parseInt(result[1]);
endGamePieces = Integer.parseInt(result[2]);
configMessage += "npp ";
}
}
System.out.println(configMessage);
}
catch (FileNotFoundException e) {
System.out.println("File '"+file+"' not found. Opening book won't be available.");
System.out.println("File '"+configFile+"' not found. Opening book won't be available.");
}
catch (IOException e){
System.out.println("Error reading file '"+file+"'.");
System.out.println("Error reading file '"+configFile+"'.");
}
catch (NumberFormatException e){
System.out.println("Error parsing the config file on line "+currentLineNumber+". Format error: "+currentLine+".");
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Error parsing the config file on line "+currentLineNumber+". Not enough values: "+currentLine+".");
}
finally {
try {
@ -115,36 +188,115 @@ public class ConfigFile {
ex.printStackTrace();
}
}
System.out.println(book.size()+" opening variants loaded.");
}
static {
pieceValuesMiddle[Piece.WHITE_KING]=-100; //500
pieceValuesMiddle[Piece.WHITE_QUEEN]=-100; //900
pieceValuesMiddle[Piece.WHITE_ROOK]=-100; //350
pieceValuesMiddle[Piece.WHITE_KNIGHT]=-100; //300
pieceValuesMiddle[Piece.WHITE_BISHOP]= -100; //started with -100, -400, -200
pieceValuesMiddle[Piece.WHITE_PAWN]= -100; //started with 100
pieceValuesMiddle[Piece.BLACK_KING]=-pieceValuesMiddle[Piece.WHITE_KING];
pieceValuesMiddle[Piece.BLACK_QUEEN]=-pieceValuesMiddle[Piece.WHITE_QUEEN];
pieceValuesMiddle[Piece.BLACK_ROOK]=-pieceValuesMiddle[Piece.WHITE_ROOK];
pieceValuesMiddle[Piece.BLACK_KNIGHT]=-pieceValuesMiddle[Piece.WHITE_KNIGHT];
pieceValuesMiddle[Piece.BLACK_BISHOP]=-pieceValuesMiddle[Piece.WHITE_BISHOP];
pieceValuesMiddle[Piece.BLACK_PAWN]=-pieceValuesMiddle[Piece.WHITE_PAWN];
pieceValuesEnd[Piece.WHITE_KING]=-400;
pieceValuesEnd[Piece.WHITE_QUEEN]=-400;
pieceValuesEnd[Piece.WHITE_ROOK]=-100;
pieceValuesEnd[Piece.WHITE_KNIGHT]=-700;
pieceValuesEnd[Piece.WHITE_BISHOP]=-400;
pieceValuesEnd[Piece.WHITE_PAWN]=-900;
pieceValuesEnd[Piece.BLACK_KING]=-pieceValuesEnd[Piece.WHITE_KING];
pieceValuesEnd[Piece.BLACK_QUEEN]=-pieceValuesEnd[Piece.WHITE_QUEEN];
pieceValuesEnd[Piece.BLACK_ROOK]=-pieceValuesEnd[Piece.WHITE_ROOK];
pieceValuesEnd[Piece.BLACK_KNIGHT]=-pieceValuesEnd[Piece.WHITE_KNIGHT];
pieceValuesEnd[Piece.BLACK_BISHOP]=-pieceValuesEnd[Piece.WHITE_BISHOP];
pieceValuesEnd[Piece.BLACK_PAWN]=-pieceValuesEnd[Piece.WHITE_PAWN];
public static void display() {
String displayConfig;
System.out.println("==================== Current configuration ====================");
displayConfig="pvm\t";
for(int i=0; i<pieceValuesMidgame.length; i++) {
displayConfig+=pieceValuesMidgame[i]+"\t";
}
System.out.println(displayConfig);
displayConfig="pve\t";
for(int i=0; i<pieceValuesEndgame.length; i++) {
displayConfig+=pieceValuesEndgame[i]+"\t";
}
System.out.println(displayConfig);
System.out.println("swm");
for(int rank=0; rank<Board.NB_OF_RANKS; rank++) {
displayConfig = "";
for (int file=1; file<=Board.NB_OF_FILES; file++) {
displayConfig+=squareWeightMidgame[file+Board.NB_OF_FILES*rank-1]+"\t";
}
System.out.println(displayConfig);
}
System.out.println("swe");
for(int rank=0; rank<Board.NB_OF_RANKS; rank++) {
displayConfig = "";
for (int file=1; file<=Board.NB_OF_FILES; file++) {
displayConfig+=squareWeightEndgame[file+Board.NB_OF_FILES*rank-1]+"\t";
}
System.out.println(displayConfig);
}
displayConfig="mvm\t";
displayConfig+= primaryMobilityValueMidgame+"\t";
displayConfig+= secondaryMobilityValueMidgame+"\t";
System.out.println(displayConfig);
displayConfig="mve\t";
displayConfig+= primaryMobilityValueEndgame+"\t";
displayConfig+= secondaryMobilityValueEndgame+"\t";
System.out.println(displayConfig);
displayConfig="npp\t";
displayConfig+= endGamePawns+"\t";
displayConfig+= endGamePieces+"\t";
System.out.println(displayConfig);
System.out.println("===============================================================");
}
static { //default values for everything
pieceValuesMidgame = new int[Piece.MAX_PIECE_NUMBER+1];
pieceValuesEndgame = new int[Piece.MAX_PIECE_NUMBER+1];
pieceValuesMidgame[Piece.WHITE_KING]=-100; //500
pieceValuesMidgame[Piece.WHITE_QUEEN]=-100; //900
pieceValuesMidgame[Piece.WHITE_ROOK]=-100; //350
pieceValuesMidgame[Piece.WHITE_KNIGHT]=-100; //300
pieceValuesMidgame[Piece.WHITE_BISHOP]= -100; //started with -100, -400, -200
pieceValuesMidgame[Piece.WHITE_PAWN]= -100; //started with 100
pieceValuesMidgame[Piece.BLACK_KING]=-pieceValuesMidgame[Piece.WHITE_KING];
pieceValuesMidgame[Piece.BLACK_QUEEN]=-pieceValuesMidgame[Piece.WHITE_QUEEN];
pieceValuesMidgame[Piece.BLACK_ROOK]=-pieceValuesMidgame[Piece.WHITE_ROOK];
pieceValuesMidgame[Piece.BLACK_KNIGHT]=-pieceValuesMidgame[Piece.WHITE_KNIGHT];
pieceValuesMidgame[Piece.BLACK_BISHOP]=-pieceValuesMidgame[Piece.WHITE_BISHOP];
pieceValuesMidgame[Piece.BLACK_PAWN]=-pieceValuesMidgame[Piece.WHITE_PAWN];
pieceValuesEndgame[Piece.WHITE_KING]=-400;
pieceValuesEndgame[Piece.WHITE_QUEEN]=-400;
pieceValuesEndgame[Piece.WHITE_ROOK]=-100;
pieceValuesEndgame[Piece.WHITE_KNIGHT]=-700;
pieceValuesEndgame[Piece.WHITE_BISHOP]=-400;
pieceValuesEndgame[Piece.WHITE_PAWN]=-900;
pieceValuesEndgame[Piece.BLACK_KING]=-pieceValuesEndgame[Piece.WHITE_KING];
pieceValuesEndgame[Piece.BLACK_QUEEN]=-pieceValuesEndgame[Piece.WHITE_QUEEN];
pieceValuesEndgame[Piece.BLACK_ROOK]=-pieceValuesEndgame[Piece.WHITE_ROOK];
pieceValuesEndgame[Piece.BLACK_KNIGHT]=-pieceValuesEndgame[Piece.WHITE_KNIGHT];
pieceValuesEndgame[Piece.BLACK_BISHOP]=-pieceValuesEndgame[Piece.WHITE_BISHOP];
pieceValuesEndgame[Piece.BLACK_PAWN]=-pieceValuesEndgame[Piece.WHITE_PAWN];
squareWeightMidgame = new int[]{
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10
};
squareWeightEndgame = new int[]{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
primaryMobilityValueMidgame = 40;
primaryMobilityValueEndgame = 40;
secondaryMobilityValueMidgame = 40; //5;
secondaryMobilityValueEndgame = 40;
endGamePawns = 3;
endGamePieces = 8;
}
}