Skip to content

Commit

Permalink
Merge pull request #5 from dykstrom/feature/integer-moves
Browse files Browse the repository at this point in the history
Feature/integer moves
  • Loading branch information
dykstrom authored Feb 12, 2018
2 parents 458d6ef + 5fc2f36 commit 07689a6
Show file tree
Hide file tree
Showing 65 changed files with 1,869 additions and 1,882 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ buildNumber.properties
.mvn/timing.properties
.idea/
*.iml
.project
.classpath
.settings/
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ An XBoard/WinBoard chess engine written in Java.
Ronja is a chess engine. It provides only a simple, character based user
interface. It is highly recommended that you run it from a chess GUI like
[XBoard/WinBoard](https://www.gnu.org/software/xboard) or
[Arena](http://www.playwitharena.com/). You will also need
[Arena](http://www.playwitharena.com). You will also need
Java 8 installed to run Ronja. The Java runtime can be downloaded
from [Oracle](https://java.com/download).

Expand Down Expand Up @@ -43,16 +43,15 @@ In the next dialog, set the type of the engine to Winboard. That's it!

## Description

Ronja is my first try to write a chess engine. It is written entirely
in Java, and has its own opening book. It implements some of the
commonly seen features of chess engines:
Ronja is written entirely in Java, and has its own opening book. It
implements a few of the commonly seen features of chess engines:

* Incremental move generation
* Iterative deepening
* Alpha-beta pruning
* Move ordering

Ronja is designed in an object-oriented way, with classes representing
moves, positions, and other entities. This probably slows the engine
down, and so may be changed in the future.
positions, and other entities. This makes the engine rather slow, and
so may be changed in the future.

[![Build Status](https://travis-ci.org/dykstrom/ronja.svg?branch=master)](https://travis-ci.org/dykstrom/ronja)
20 changes: 16 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -43,15 +43,15 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<version>2.20.1</version>
<configuration>
<argLine>-Djava.util.logging.config.class=se.dykstrom.ronja.test.LoggingConfig</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<version>2.20.1</version>
<configuration>
<argLine>-Djava.util.logging.config.class=se.dykstrom.ronja.test.LoggingConfig</argLine>
</configuration>
Expand All @@ -67,7 +67,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<version>3.1.0</version>
<configuration>
<descriptors>
<descriptor>src/assembly/bin.xml</descriptor>
Expand Down Expand Up @@ -115,6 +115,18 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
10 changes: 4 additions & 6 deletions src/main/java/se/dykstrom/ronja/common/book/BookMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,27 @@

package se.dykstrom.ronja.common.book;

import se.dykstrom.ronja.common.model.Move;

/**
* Represents an opening book move with the actual move, and its weight in the opening book.
*
* @author Johan Dykstrom
*/
public class BookMove {

private final Move move;
private final int move;
private final int weight;

public BookMove(Move move, int weight) {
public BookMove(int move, int weight) {
this.move = move;
this.weight = weight;
}

@Override
public String toString() {
return move.toString();
return Integer.toString(move);
}

public Move getMove() {
public int getMove() {
return move;
}

Expand Down
30 changes: 15 additions & 15 deletions src/main/java/se/dykstrom/ronja/common/book/OpeningBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

package se.dykstrom.ronja.common.book;

import se.dykstrom.ronja.common.parser.IllegalMoveException;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

import java.util.*;

import se.dykstrom.ronja.common.model.Move;
import se.dykstrom.ronja.common.model.Piece;
import se.dykstrom.ronja.common.model.Position;
import se.dykstrom.ronja.common.model.Square;

import java.util.*;

import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import se.dykstrom.ronja.common.parser.IllegalMoveException;

/**
* A chess game opening book, that reads its opening moves from a file that is
Expand Down Expand Up @@ -55,10 +55,10 @@ private OpeningBook() {
positions = new HashMap<>();

// Add some simple moves to the empty opening book
Move e2e4 = Move.of(Piece.PAWN, Square.E2, Square.E4, null, false, false);
Move e7e5 = Move.of(Piece.PAWN, Square.E7, Square.E5, null, false, false);
Move d2d4 = Move.of(Piece.PAWN, Square.D2, Square.D4, null, false, false);
Move d7d5 = Move.of(Piece.PAWN, Square.D7, Square.D5, null, false, false);
int e2e4 = Move.create(Piece.PAWN, Square.E2, Square.E4);
int e7e5 = Move.create(Piece.PAWN, Square.E7, Square.E5);
int d2d4 = Move.create(Piece.PAWN, Square.D2, Square.D4);
int d7d5 = Move.create(Piece.PAWN, Square.D7, Square.D5);
try {
positions.put(Position.START, Arrays.asList(new BookMove(e2e4, 50), new BookMove(d2d4, 50)));
positions.put(Position.of(new String[]{"e2e4"}), singletonList(new BookMove(e7e5, 100)));
Expand Down Expand Up @@ -162,12 +162,12 @@ private static int indexOfFirstWithNonZeroWeight(List<BookMove> bookMoves) {
* @param position A chess game position.
* @return One of the possible moves found, or {@code null} if no move was found.
*/
public Move findBestMove(Position position) {
public int findBestMove(Position position) {
List<BookMove> moves = positions.get(position);

// If no book moves are known for this position
if (moves == null) {
return null;
return 0;
}

// Make a random decision on which move to make
Expand All @@ -184,9 +184,9 @@ public Move findBestMove(Position position) {
* @param value A value between 0 and 99 (inclusive).
* @return The chosen move, or {@code null} if no move was found.
*/
public static Move findMoveInList(final List<BookMove> moves, final int value) {
public static int findMoveInList(final List<BookMove> moves, final int value) {
if (moves.isEmpty()) {
return null;
return 0;
} else if (value < moves.get(0).getWeight()) {
return moves.get(0).getMove();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@

package se.dykstrom.ronja.common.book;

import se.dykstrom.ronja.common.model.Position;
import se.dykstrom.ronja.common.parser.IllegalMoveException;
import se.dykstrom.ronja.common.parser.MoveParser;
import static java.util.stream.Collectors.toSet;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.logging.Logger;

import static java.util.stream.Collectors.toSet;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import se.dykstrom.ronja.common.model.Position;
import se.dykstrom.ronja.common.parser.IllegalMoveException;
import se.dykstrom.ronja.common.parser.MoveParser;

/**
* A class for parsing the Ronja opening book file. The opening book file should
Expand Down
36 changes: 32 additions & 4 deletions src/main/java/se/dykstrom/ronja/common/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import se.dykstrom.ronja.common.book.OpeningBook;
import se.dykstrom.ronja.common.parser.IllegalMoveException;
import se.dykstrom.ronja.engine.time.TimeControl;
import se.dykstrom.ronja.engine.time.TimeControlType;
import se.dykstrom.ronja.engine.time.TimeData;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -48,7 +49,7 @@ public class Game {
private Color engineColor;

/** All moves made in this game. */
private List<Move> moves;
private List<Integer> moves;

/** The name of the opponent as set by the "name" command.*/
private String opponent;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void reset() {
/**
* Makes the given move, and updates game data accordingly.
*/
public void makeMove(Move move) throws IllegalMoveException {
public void makeMove(int move) throws IllegalMoveException {
Position newPosition = position.withMove(move);

// If the user is in check after his move
Expand All @@ -117,6 +118,33 @@ public void makeMove(Move move) throws IllegalMoveException {
moves.add(move);
}

/**
* Updates the time data in the game after a move, taking into account the {@code usedTime},
* and the type of the time control.
*
* @param usedTime The time used for this move in milliseconds.
*/
public void updateTimeDataAfterMove(long usedTime) {
TimeControl timeControl = getTimeControl();
if (timeControl.getType() == TimeControlType.SECONDS_PER_MOVE) {
setTimeData(TimeData.from(timeControl));
} else if (timeControl.getType() == CLASSIC) {
TimeData timeData = getTimeData();
long remainingTime = timeData.getRemainingTime() - usedTime;
long numberOfMoves = timeData.getNumberOfMoves() - 1;
// If we have reached the time control, reset number of moves and add time
if (numberOfMoves == 0) {
numberOfMoves = timeControl.getNumberOfMoves();
remainingTime += timeControl.getBaseTime();
}
setTimeData(timeData.withRemainingTime(remainingTime).withNumberOfMoves(numberOfMoves));
} else { // TimeControlType.INCREMENTAL
TimeData timeData = getTimeData();
long remainingTime = timeData.getRemainingTime() - usedTime + timeControl.getIncrement();
setTimeData(timeData.withRemainingTime(remainingTime));
}
}

/**
* Sets the color the engine plays.
*
Expand All @@ -136,14 +164,14 @@ public Color getEngineColor() {
/**
* Sets the list of moves.
*/
public void setMoves(List<Move> moves) {
public void setMoves(List<Integer> moves) {
this.moves = moves;
}

/**
* Returns the list of moves made so far in this game.
*/
public List<Move> getMoves() {
public List<Integer> getMoves() {
return moves;
}

Expand Down
Loading

0 comments on commit 07689a6

Please sign in to comment.