Browse Source

Changed management of config file, openbook and problems.

Files can now be included in the jarfile.
master
djib 15 years ago
parent
commit
7ff417b453
  1. 2
      src/suicideChess/Board.java
  2. 26
      src/suicideChess/ConfigFile.java
  3. 18
      src/suicideChess/OpeningBook.java
  4. 12
      src/suicideChess/SuicideChess.java
  5. 9
      src/suicideChess/SuicideProblems.java

2
src/suicideChess/Board.java

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

26
src/suicideChess/ConfigFile.java

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

18
src/suicideChess/OpeningBook.java

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

12
src/suicideChess/SuicideChess.java

@ -37,7 +37,7 @@ public class SuicideChess {
/**
* 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 ?
@ -63,7 +63,7 @@ public class SuicideChess {
/**
* 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.
@ -99,7 +99,7 @@ public class SuicideChess {
/**
* 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.
@ -225,7 +225,7 @@ public class SuicideChess {
try {
String whatMove= moveInput.readLine();
boolean playedALegalMove = false;
//System.out.println("Got message from xboard: "+whatMove);
if (whatMove.startsWith("help")) {
System.out.println("==================== Quick help ====================\nEnter moves in SAN notation : e2e3, e7e8r, ...\n");
System.out.println("new\t\t\tcreates a new game");
@ -433,8 +433,8 @@ public class SuicideChess {
System.out.println("Capturing is mandatory.");
}
//bitboard.debug();
for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++)
System.out.println(allLegalMoves.get(moveIndex));
//for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++)
// System.out.println(allLegalMoves.get(moveIndex));
System.out.println("Illegal move: "+theMove.toString());
} else {
bitboard.doMove(allLegalMoves.get(foundMoveIndex));

9
src/suicideChess/SuicideProblems.java

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 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.
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
@ -55,7 +56,11 @@ public class SuicideProblems {
//declared here only to make visible to finally clause
BufferedReader problemReader = null;
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
while ((line = problemReader.readLine()) != null) {
if (!line.startsWith("#")) { //ignore lines starting with # (comments)

Loading…
Cancel
Save