GithubHelp home page GithubHelp logo

wolfraam / chess-game Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 3.0 227 KB

A java chess library for generating legal chess moves, determining check / mate / draw, determining the chess opening. Supports SAN, FAN, LAN, UCI and PGN.

License: MIT License

Java 100.00%
chess chess-game chess-library chessboard fen java pgn chess-opening-classification chess-openings uci

chess-game's Introduction

chess-game - A Chess Java Library

chess-game is a java chess library which can

  • generate the legal chess moves of a position
  • determine check / mate / draw
  • determine the FEN notation of a position
  • generate the moves of a game in SAN, FAN, LAN and UCI notation in different languages
  • parse moves in SAN, FAN, LAN and UCI notation in different languages
  • determine the chess opening of a game
  • import and export games in PGN notation

Features

Code Quality

LINE BRANCH COMPLEXITY

Gradle

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.github.wolfraam:chessgame:1.4")
}

Maven

<dependency>
  <groupId>io.github.wolfraam</groupId>
  <artifactId>chessgame</artifactId>
  <version>1.4</version>
</dependency>

Playing moves

import io.github.wolfraam.chessgame.ChessGame;

ChessGame chessGame = new ChessGame();
Move move = new Move(Square.E2, Square.E4);

// Check whether move is allowed:
if(chessGame.isLegalMove(move)) {
  // Execute the move on the board:
  chessGame.playMove(move);
}

// Or play a move by supplying the move notation:
chessGame.playMove(NotationType.SAN, "c5");

// Play several moves, seperate the moves by spaces or commas:
chessGame.playMoves(NotationType.SAN, "Nf3 d6");

Get board info

// print ASCII representation of the board
System.out.println(chessGame.getASCII());
// output:
// rnbqkbnr
// pppp_ppp
// ________
// ____p___
// ____P___
// __N_____
// PPPP_PPP
// R_BQKBNR

// Get FEN diagram
System.out.println(chessGame.getFen());
// output:
// rnbqkbnr/pppp1ppp/8/4p3/4P3/2N5/PPPP1PPP/R1BQKBNR b KQkq - 1 2
    
// Get the piece on A1 
System.out.println(chessGame.getPiece(Square.A1));
// ouput: WHITE_ROOK

Get game result

ChessGame chessGame = new ChessGame();
chessGame.playMoves(NotationType.SAN, "e4 f6 Nc3 g5");
System.out.println(chessGame.getGameResultType()); // output: null
chessGame.playMove(NotationType.SAN, "Qh5");
System.out.println(chessGame.getGameResultType()); // output: WHITE_WINS

Move notation

chess-game supports 4 move notation types:

  • Short algebraic notation (SAN). For example: Nc3
  • Long algebraic notation (LAN). For example: Nb1-c3
  • Figurine algebraic notation (FAN). For example: โ™žc3
  • The notation used in the UCI (Universal Chess Interface) protocol, which is used to communicate with chess engines. For example: b1c3
// Find all legal moves and get SAN notation of them:
System.out.println(chessGame.getLegalMoves().stream()
    .map(move -> chessGame.getNotation(NotationType.SAN, move))
    .sorted().collect(Collectors.joining(",")));

// output:
// Ba3,Bb4,Bc5,Bd6,Be7,Ke7,Na6,Nc6,Ne7,Nf6,Nh6,Qe7,Qf6,Qg5,Qh4,a5,a6,b5,b6,c5,c6,d5,d6,f5,f6,g5,g6,h5,h6

Move notation in other languages

The SAN en LAN notations support different languages. For example in French (language code: "fr") the notation for the knight (which is "N" in english) is "C":

ChessGame chessGame = new ChessGame();
chessGame.playMoves(NotationType.SAN, "Nc3 e5");
System.out.println(chessGame.getNotationList(NotationType.LAN, "fr"));
// output:
// [Cb1-c3, e7-e5]

Chess Opening

Determining the opening name of a game:

ChessGame chessGame = new ChessGame();
chessGame.playMoves(NotationType.SAN, "e4 c5 Nf3 d6 d4 cxd4 Nxd4 Nf6 Nc3 a6");
ChessOpening chessOpening = chessGame.getChessOpening();
System.out.println(chessOpening.eco); // output: B90
System.out.println(chessOpening.name); // output: Sicilian
System.out.println(chessOpening.variation); // output: Najdorf
System.out.println(chessOpening.getFullName()); // output: Sicilian / Najdorf (B90)

Export PGN games

To export games in Portable Game Notation (PGN):

import io.github.wolfraam.chessgame.pgn.PGNExporter;

ChessGame chessGame = new ChessGame();
chessGame.playMoves(NotationType.SAN, "e4 c5 Nf3");
chessGame.setPgnTag(PgnTag.EVENT, "Test Event");
chessGame.setPgnTag(PgnTag.RESULT, "1/2-1/2");
PGNExporter pgnExporter = new PGNExporter(System.out);
pgnExporter.write(chessGame);
// output:
// [Event "Test Event"]
// [Result "1/2-1/2"]
// 
// 1.e4 c5 2.Nf3 1/2-1/2

pgnExporter.write(chessGame2); // etc.

Import PGN games

To import games in PGN:

import io.github.wolfraam.chessgame.pgn.PGNImporter;

PGNImporter pgnImporter = new PGNImporter();
pgnImporter.setOnGame((game)->{
  System.out.println("Imported a game with moves:" + game.getMoves());
});
pgnImporter.setOnError(System.out::println);
pgnImporter.setOnWarning(System.out::println);

pgnImporter.run(new File("/temp/games.pgn"));

chess-game's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

chess-game's Issues

Fails to import PGN file, but without any error

I've tried to import this pgn file with the library, but for I reason I was not able to find, it fails to be imported and without any error

[Event ""]
[Site ""]
[Date ""]
[Round ""]
[White ""]
[Black ""]
[Result ""]
[FEN "8/K7/8/8/8/1k6/1N1p4/8 w - - 0 1"]
[SetUp "1"]

*

[Event ""]
[Site ""]
[Date ""]
[Round ""]
[White ""]
[Black ""]
[Result ""]
[FEN "8/8/8/8/5N2/8/2p4K/2k5 w - - 0 1"]
[SetUp "1"]

*

[Event ""]
[Site ""]
[Date ""]
[Round ""]
[White ""]
[Black ""]
[Result ""]
[FEN "8/8/8/8/4N3/1p6/7K/1k6 b - - 0 1"]
[SetUp "1"]

*

[Event ""]
[Site ""]
[Date ""]
[Round ""]
[White ""]
[Black ""]
[Result ""]
[FEN "8/8/K7/4N3/8/7p/7k/8 w - - 0 1"]
[SetUp "1"]

*

[Event ""]
[Site ""]
[Date ""]
[Round ""]
[White ""]
[Black ""]
[Result ""]
[FEN "K7/8/8/5N2/8/3k3p/8/8 w - - 0 1"]
[SetUp "1"]

*

[Event ""]
[Site ""]
[Date ""]
[Round ""]
[White ""]
[Black ""]
[Result ""]
[FEN "8/2K5/8/8/4N3/8/2k4p/8 w - - 0 2"]
[SetUp "1"]

[Event ""]
[Site ""]
[Date ""]
[Round ""]
[White ""]
[Black ""]
[Result ""]
[FEN "8/2K5/8/8/4N3/3k4/7p/8 w - - 0 2"]
[SetUp "1"]

*

[Event ""]
[Site ""]
[Date ""]
[Round ""]
[White ""]
[Black ""]
[Result ""]
[FEN "8/2K5/8/8/3kN3/8/7p/8 w - - 0 2"]
[SetUp "1"]

*

[Event ""]
[Site ""]
[Date ""]
[Round ""]
[White ""]
[Black ""]
[Result ""]
[FEN "8/8/8/8/8/p7/2K1N3/k7 w - - 0 1"]
[SetUp "1"]

*

Regards

test.zip

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.