Files
Sushi/src/suicideChess/ConfigFile.java
djib f67d82d129 Version 0.8.9
=============
Added external config file -> the resulting program seems a lot faster !!!
Added help.
2006-07-04 11:55:00 +00:00

303 lines
12 KiB
Java

package suicideChess;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* This class is used to read configuration settings for the AI
*
* @author Jean-Baptiste Hétier
* @version $LastChangedRevision$, $LastChangedDate$
*
*/
public class ConfigFile {
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[] getPieceValuesMidgame() {
return pieceValuesMidgame;
}
/**
* The pieces value in the end
*/
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;
}
/**
* 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(configFile));
String line = null; //not declared within while loop
while ((line = problemReader.readLine()) != null) {
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 '"+configFile+"' not found. Opening book won't be available.");
}
catch (IOException e){
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 {
if (problemReader!= null) {
problemReader.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
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;
}
}