Changed management of config file, openbook and problems.

Files can now be included in the jarfile.
This commit is contained in:
2006-11-12 21:14:13 +00:00
parent f1e8cd0784
commit 7ff417b453
5 changed files with 43 additions and 24 deletions

View File

@ -655,6 +655,7 @@ public class Board {
if(SuicideChess.USE_MOBILITY) { if(SuicideChess.USE_MOBILITY) {
mobilityValue = ((mobilityMidgame(Piece.WHITE)-mobilityMidgame(Piece.BLACK))); mobilityValue = ((mobilityMidgame(Piece.WHITE)-mobilityMidgame(Piece.BLACK)));
} }
//System.out.println(mobilityValue+" "+pieceValue);
boardValue = pieceValue + mobilityValue; boardValue = pieceValue + mobilityValue;
} else { } else {
//System.out.println("Playing endgame"); //System.out.println("Playing endgame");
@ -694,6 +695,7 @@ public class Board {
if(SuicideChess.USE_MOBILITY) { if(SuicideChess.USE_MOBILITY) {
mobilityValue = ((mobilityEndgame(Piece.WHITE)-mobilityEndgame(Piece.BLACK))); mobilityValue = ((mobilityEndgame(Piece.WHITE)-mobilityEndgame(Piece.BLACK)));
} }
//System.out.println(mobilityValue+" "+pieceValue);
boardValue = pieceValue + mobilityValue; boardValue = pieceValue + mobilityValue;
} }
if (!Rules.isThereALegalMovesForPlayer(this)) { if (!Rules.isThereALegalMovesForPlayer(this)) {

View File

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
/** /**
* This class is used to read configuration settings for the AI * This class is used to read configuration settings for the AI
@ -90,7 +91,7 @@ public class ConfigFile {
* Loads the default configfile * Loads the default configfile
*/ */
public static void load() { public static void load() {
loadFile("config"); loadFile("/config");
} }
/** /**
@ -103,11 +104,16 @@ public class ConfigFile {
String currentLine=""; String currentLine="";
int currentLineNumber=0; int currentLineNumber=0;
//declared here only to make visible to finally clause //declared here only to make visible to finally clause
BufferedReader problemReader = null; BufferedReader configReader = null;
try { try {
problemReader = new BufferedReader(new FileReader(configFile)); if(configFile.equals("/config")) {
configReader = new BufferedReader(new InputStreamReader(SuicideProblems.class.getResourceAsStream(configFile)));
} else {
configReader = new BufferedReader(new FileReader(configFile));
}
String line = null; //not declared within while loop String line = null; //not declared within while loop
while ((line = problemReader.readLine()) != null) { while ((line = configReader.readLine()) != null) {
currentLine=line; currentLine=line;
currentLineNumber++; currentLineNumber++;
if(line.startsWith("pvm")) { if(line.startsWith("pvm")) {
@ -128,7 +134,7 @@ public class ConfigFile {
configMessage += "pve "; configMessage += "pve ";
} else if(line.startsWith("swm")) { } else if(line.startsWith("swm")) {
for(int rank=0; rank<Board.NB_OF_RANKS; rank++) { for(int rank=0; rank<Board.NB_OF_RANKS; rank++) {
if ((line = problemReader.readLine()) != null) { if ((line = configReader.readLine()) != null) {
String [] result=line.split("\t"); String [] result=line.split("\t");
for (int file=0; file<Board.NB_OF_FILES; file++) { for (int file=0; file<Board.NB_OF_FILES; file++) {
squareWeightMidgame[file+Board.NB_OF_FILES*rank]=Integer.parseInt(result[file]); squareWeightMidgame[file+Board.NB_OF_FILES*rank]=Integer.parseInt(result[file]);
@ -138,7 +144,7 @@ public class ConfigFile {
configMessage += "swm "; configMessage += "swm ";
} else if(line.startsWith("swe")) { } else if(line.startsWith("swe")) {
for(int rank=0; rank<Board.NB_OF_RANKS; rank++) { for(int rank=0; rank<Board.NB_OF_RANKS; rank++) {
if ((line = problemReader.readLine()) != null) { if ((line = configReader.readLine()) != null) {
String [] result=line.split("\t"); String [] result=line.split("\t");
for (int file=0; file<Board.NB_OF_FILES; file++) { for (int file=0; file<Board.NB_OF_FILES; file++) {
squareWeightEndgame[file+Board.NB_OF_FILES*rank]=Integer.parseInt(result[file]); squareWeightEndgame[file+Board.NB_OF_FILES*rank]=Integer.parseInt(result[file]);
@ -180,8 +186,8 @@ public class ConfigFile {
} }
finally { finally {
try { try {
if (problemReader!= null) { if (configReader!= null) {
problemReader.close(); configReader.close();
} }
} }
catch (IOException ex) { catch (IOException ex) {
@ -295,8 +301,8 @@ public class ConfigFile {
primaryMobilityValueEndgame = 0; primaryMobilityValueEndgame = 0;
secondaryMobilityValueMidgame = 0; //5; secondaryMobilityValueMidgame = 0; //5;
secondaryMobilityValueEndgame = 0; secondaryMobilityValueEndgame = 0;
endGamePawns = 4; endGamePawns = 3;
endGamePieces = 8; endGamePieces = 6;
} }
} }

View File

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
@ -26,7 +27,7 @@ public class OpeningBook {
//This array will hold all book variants. //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). 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 static void load() { openingBookLoad("/openbook"); reset();} //initialise with file 'problems' if found in current directory
/** /**
* This function is used to load an openingbook file * This function is used to load an openingbook file
@ -34,12 +35,16 @@ public class OpeningBook {
*/ */
public static void openingBookLoad(String file) { public static void openingBookLoad(String file) {
//declared here only to make visible to finally clause //declared here only to make visible to finally clause
BufferedReader problemReader = null; BufferedReader bookReader = null;
book = new ArrayList<String[]>(); book = new ArrayList<String[]>();
try { try {
problemReader = new BufferedReader(new FileReader(file)); if(file.equals("/openbook")) {
bookReader = new BufferedReader(new InputStreamReader(SuicideProblems.class.getResourceAsStream(file)));
} else {
bookReader = new BufferedReader(new FileReader(file));
}
String line = null; //not declared within while loop String line = null; //not declared within while loop
while ((line = problemReader.readLine()) != null) { while ((line = bookReader.readLine()) != null) {
if (!line.startsWith("#")) { //ignore lines starting with # (comments) if (!line.startsWith("#")) { //ignore lines starting with # (comments)
book.add(line.split("\\s")); //each space defines a new move book.add(line.split("\\s")); //each space defines a new move
} }
@ -53,8 +58,8 @@ public class OpeningBook {
} }
finally { finally {
try { try {
if (problemReader!= null) { if (bookReader!= null) {
problemReader.close(); bookReader.close();
} }
} }
catch (IOException ex) { catch (IOException ex) {
@ -118,6 +123,7 @@ public class OpeningBook {
} }
} }
} }
if (SuicideChess.postThinkingOutput()) System.out.println();
//select one of the possible moves randomly //select one of the possible moves randomly
if(possibleMoves.size()==0) { if(possibleMoves.size()==0) {
throw new NoOpeningMovesLeft("No moves left in opening book."); throw new NoOpeningMovesLeft("No moves left in opening book.");

View File

@ -37,7 +37,7 @@ public class SuicideChess {
/** /**
* Use mobility in evaluation function (slows the program down a lot) * Use mobility in evaluation function (slows the program down a lot)
*/ */
public static final boolean USE_MOBILITY = true; public static final boolean USE_MOBILITY = false;
/** /**
* do move ordering in Alpha-Beta pruning ? * do move ordering in Alpha-Beta pruning ?
@ -63,7 +63,7 @@ public class SuicideChess {
/** /**
* Quiescence search -> don't evaluate if captures are possible. * Quiescence search -> don't evaluate if captures are possible.
*/ */
public static final boolean QUIESCENCE_SEARCH = false; public static final boolean QUIESCENCE_SEARCH = true;
/** /**
* Quiescence limit (ie. if more than that many possibilities of capturing, don't analyse further. * Quiescence limit (ie. if more than that many possibilities of capturing, don't analyse further.
@ -99,7 +99,7 @@ public class SuicideChess {
/** /**
* The name to be displayed * The name to be displayed
*/ */
public static final String NAME = "djib's SuShi v1.0.4"; public static final String NAME = "djib's SuShi v1.0.5";
/** /**
* Displays informations in the console. * Displays informations in the console.
@ -225,7 +225,7 @@ public class SuicideChess {
try { try {
String whatMove= moveInput.readLine(); String whatMove= moveInput.readLine();
boolean playedALegalMove = false; boolean playedALegalMove = false;
//System.out.println("Got message from xboard: "+whatMove);
if (whatMove.startsWith("help")) { if (whatMove.startsWith("help")) {
System.out.println("==================== Quick help ====================\nEnter moves in SAN notation : e2e3, e7e8r, ...\n"); System.out.println("==================== Quick help ====================\nEnter moves in SAN notation : e2e3, e7e8r, ...\n");
System.out.println("new\t\t\tcreates a new game"); System.out.println("new\t\t\tcreates a new game");
@ -433,8 +433,8 @@ public class SuicideChess {
System.out.println("Capturing is mandatory."); System.out.println("Capturing is mandatory.");
} }
//bitboard.debug(); //bitboard.debug();
for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++) //for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++)
System.out.println(allLegalMoves.get(moveIndex)); // System.out.println(allLegalMoves.get(moveIndex));
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));

View File

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
/** /**
* This class is used to load problems from a file * This class is used to load problems from a file
@ -21,7 +22,7 @@ public class SuicideProblems {
//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 void load() { 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
@ -55,7 +56,11 @@ public class SuicideProblems {
//declared here only to make visible to finally clause //declared here only to make visible to finally clause
BufferedReader problemReader = null; BufferedReader problemReader = null;
try { try {
problemReader = new BufferedReader(new FileReader(file)); if(file.equals("/problems")) {
problemReader = new BufferedReader(new InputStreamReader(SuicideProblems.class.getResourceAsStream(file)));
} else {
problemReader = new BufferedReader(new FileReader(file));
}
String line = null; //not declared within while loop String line = null; //not declared within while loop
while ((line = problemReader.readLine()) != null) { while ((line = problemReader.readLine()) != null) {
if (!line.startsWith("#")) { //ignore lines starting with # (comments) if (!line.startsWith("#")) { //ignore lines starting with # (comments)