GithubHelp home page GithubHelp logo

davecom / chess.dart Goto Github PK

View Code? Open in Web Editor NEW
51.0 5.0 28.0 288 KB

A library for legal chess move generation, maintenance of game state, and format conversion.

License: Other

CSS 3.82% Dart 49.12% HTML 47.07%

chess.dart's Introduction

chess.dart

chess.dart is a library for legal chess move generation, maintenance of chess game state, and conversion to and from the formats FEN and PGN. It has no external dependencies.

In 2014, chess.dart started as a port of chess.js to Dart, including all of the functionality in the original library, while making the API a little bit more Dart-like. It has since accumulated bug fixes and performance improvements thanks to contributions from the Dart community.

chess.dart includes a test suite composed of all of the original chess.js unit tests as well as newer tests specific to bug fixes and feature additions specific to the Dart version (which you can find in the test directory). chess.dart is depended upon by multiple popular packages for chess board display.

Installation

The package is just called chess on pub. If for some strange reason you can't use pub, you can also just add the chess.dart file from the lib directory to your project. Adding the pub package is:

 $ dart pub add chess

Or you can manually add chess to your pubspec.yaml.

Then importing chess.dart in your code is simply:

import 'package:chess/chess.dart';

A Random Game

import "package:chess/chess.dart";

void main() {
  Chess chess = new Chess();
  while (!chess.game_over) {
    print('position: ' + chess.fen);
    print(chess.ascii);
    var moves = chess.moves();
    moves.shuffle();
    var move = moves[0];
    chess.move(move);
    print('move: ' + move);
  }
}

Documentation

The chess.js documentation is largely relevant, but there are also DartDocs generated within the lib/docs directory. The only change in naming from chess.js, is that history() has been changed to getHistory() due to a conflict with the history ivar and some methods have been changed into properties.

Versioning

chess.dart version 0.6.5 and above requires Dart 2. For Dart 1, use version 0.6.4.

Testing

The test directory contains tests.dart which is a port of chess.js's unit tests. The program random.dart plays a random game of chess. ai.dart is an example of a simple 4 ply alpha beta search for black (yes a simple chess-playing program) that uses a purely material evaluation function (it is rather slow). You can run the unit tests using pub:

pub get
dart run test/tests.dart

And you can also run performance tests.

dart run test/perft.dart

And, finally you can run the simple AI:

dart run test/ai.dart

Links

chess.dart's People

Contributors

adam-singer avatar andersforsell avatar daniel-mf avatar davecom avatar deven98 avatar gabriel-amaral avatar kasperl avatar loloof64 avatar safield avatar unibeck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

chess.dart's Issues

load_pgn with FEN-Header

Hi,
when exporting a pgn game and the starting position differs from default, the [FEN]-Header is correctly exported. But when reading the same pgn game, the [FEN] header seems to be ignored - so game data cannot be loaded correctly.

Bug or Feature?

Best Regards and thanks for the great library,
Tobias

Confusing name for variable "half_move"

In the Chess class, the variable name "half_move" is being used as a counter for the 50-move rule. At first sight it seems like half_move is another name for a ply(A half move) and can be easily misunderstood. "half_move" being named to something like drawCounter would be more appropriate in my opinion.

please help

even tho I assign a correct fen always give this error 6th field (move number) must be a positive integer.

Headers in PGN

Hello, thank you for your chess package.
There's a problem though.

When I do load_pgn, I don't get pgn headers except for Result.
Is it a bug or feature?

loadPgn method returns false in some Pgn strings

I've a pgn string which has only FEN and SetUp headers.

[FEN "1q3rk1/2p2ppp/8/5N2/8/2PQ4/PP6/1K1R4 w - - 0 1"]
[SetUp "1"]

  1. Ne7+ Kh8 2. Qxh7+ Kxh7 3. Rh1# 1-0

As shown on the image I can easily use this pgn in Chess.com's Analysis Part.

image

I can also download the regular pgn format (you can see on the attached image) for this game from 'Download' part.
image

The problem is for these kind of formats when loadPgn method returns always false even if I use the standart pgn format which I get from the Chess.com

I think the main reason is about the initial board position because on the Debug section I got a null 'Move' variable in line 1467.

image

DartPub package giving an error when added as dependency.

I have a package flutter_chess_board on DartPub which has chess as a dependency. It worked fine until Dart 2.1.0 was released. After release, pub analysis gives this as an analysis error:

ERR: The current Dart SDK version is 2.1.0-dev.1.0.flutter-69fce633b7.

Because flutter_chess_board depends on chess >=0.0.2 which requires SDK version <2.0.0, version solving failed.

Note: This is my pubspec.yaml dependency line ->
chess: ">=0.6.4 <0.7.0"

Incorrect Move Generation for Pawn

The moves function within the package generates inaccurate moves for a pawn situated on square e5 in specific scenarios. The issue arises when it produces the following available moves:

e5 to e6
e5 to d6 (incorrect move)

Additionally, the function incorrectly permits the pawn to move to d6, resulting in the removal of the pawn on d5 from the board.

Steps to Reproduce the Issue:

Set up a scenario where either your or your opponent's pawn is already positioned on the 6th row.
Move a pawn from a neighboring column two steps forward.

This sequence triggers the generation of incorrect moves, including the flawed move from e5 to d6.

Sample fen:
rnbqkbnr/ppp1p1pp/8/3pPp2/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 3

image

The copy() method, sometimes results in a Chess class with a different turn variable....chaos ensues.

I am observing a very strange behaviour where sometimes a copied board will have a different turn....sort of. The issue is not yet reproducible reliably, but I am obseriving it happen often.

Basically, I have a Chess object whose turn variable is white. I deep copy the object, and the resulting boards turn variable toString() returns 'b'. It seems like the internal int value (which underneath is like a double) has some sort of precision error.

Observations:
-the turn variable is a Color class.
-the initial turn variable Color instance, and the subsequent copied turn variable Color instance both have an internal value of 0 (aka WHITE).
-if you call toString() on the first instance it returns 'w', and on the second instance 'b'.
-if you use the value of the new turn variable for comparisons, it will not match to either white of black.
-because of this, the generate_moves() (with legal flag), always returns and empty list (when it should actually return many moves).

Some thoughts....

Could the Color class just be an enum to simplify things? I was looking into making this change (and it did fix the problem), but I am not quite sure if I broke something. For example there is a ColorMap class that seems intimately coupled with the Color class.

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.