Coder Social home page Coder Social logo

chessmoves's Introduction

Chessmoves

Chess move and position (FEN/SAN/UCI) generation in C for use in scripting applications.

`Chessmoves' is a small module to make fast chess tree traversal available in a scripting environment, such as Python or sh.

In the core this is a set of C functions for chess move making, wrapped for easy use. All input and output is in the form of FEN strings for positions. SAN notation for moves is made available as well. The FEN output is strictly sanitized. For example, no fake en-passant target square flag is produced when there is no such legal capture in the position in the first place.

Python module interface:

NAME
    chessmoves - Chess move and position generation (SAN/FEN/UCI).

FUNCTIONS
    moves(...)
        moves(position, notation='san') -> { move : newPosition, ... }

        Generate all legal moves from a position.
        Return the result as a dictionary, mapping moves to positions.

        The `notation' keyword controls the output move syntax.
        Available notations are:
            'san': Standard Algebraic Notation (e.g. Nc3+, O-O, dxe8=Q)
            'long': Long Algebraic Notation (e.g. Nb1-c3+, O-O, d7xe8=Q)
            'uci': Universal Chess Interface computer notation (e.g. b1c3, e8g8, d7e8q)

    position(...)
        position(inputFen) -> standardFen

        Parse a FEN like string and convert it into a standardized FEN.
        For example:
         - Complete shortened ranks
         - Order castling flags
         - Remove en passant target square if there is no such legal capture
         - Remove excess data beyond the FEN

    move(...)
        move(inputFen, inputMove, notation='san') -> (move, fen)

        Try to parse the input move and return it as a normalized string
        if successful, legal and unambiguous.

        The parser accepts a wide variety of formats. The only restriction
        is that piece identifiers, other than promotion pieces, must always
        be in upper case, and file letters must always be in lower case.
        Input capture signs, check marks, checkmate marks, annotations
        (x, +, !, ?, etc) are all swallowed and ignored: these are not used
        for disambiguation and also not checked for correctness.
        When a promotion piece is missing, queening is assumed.
        The output fen is the position after the move.

        The `notation' keyword controls the output move syntax. See moves(...)
        for details.

   hash(...)
        hash(fen) -> hash

        Compute the Zobrist-Polyglot hash for the position.

DATA
    notations = ['uci', 'san', 'long']
    startPosition = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - ...

Example of the Python extension:

>>> import chessmoves
>>> moves = chessmoves.moves(chessmoves.startPosition)
>>> print moves.keys()
['g3', 'f3', 'f4', 'h3', 'Nc3', 'h4', 'b4', 'Na3', 'a3', 'g4', 'Nf3', 'a4', 'Nh3', 'b3', 'c3', 'e4', 'd4', 'd3', 'e3', 'c4']
>>> print moves['e4']
rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq -

Performance of the Python extension:

$ echo rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - | time python Tools/perft.py 5
4865609
        3.08 real         3.06 user         0.00 sys
# --> results per second: 1,579,743

chessmoves's People

Contributors

kervinck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

chessmoves's Issues

How to run as .sh script?

Hi, thank you for your project!

`Chessmoves' is a small module to make fast chess tree traversal available in a scripting environment, such as Python or sh.

I have figured out how to use chessmoves in a python script after running make, but could you please provide instructions on how to run it as a (standalone) shell/bash -script?

What I have tried so far:

  1. Compile from source:
$ g++ Source/chessmovesmodule.c 
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
Source/chessmovesmodule.c:46:10: fatal error: 'Python.h' file not found
  1. Running make and then compile the .so from the build-directory:
$ g++ -o chessmoves build/lib.macosx-10.11-x86_64-2.7/chessmoves.so 
ld: can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB) file 'build/lib.macosx-10.11-x86_64-2.7/chessmoves.so'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Somehow I am stuck at the moment and cannot figure out what the above error messages are trying to tell me. Thank you for your help!

A call to chessmoves.moves(...) is loosing counts for both `Halfmove clock` and `Fullmove number`

Hi, I'm trying to use your chessmoves project to create a chess AI, unfortunately the library is loosing counts for for Halfmove clock and Fullmove number (names picked according to wiki article https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation) that are used to guarantee 50/75 move rule. Which means that such an AI will not be limited by that rule and will expect a play after 50 inactive moves. I'm able to identify a pawn move or a capture externally but it seems that it would be more reasonable to perform a fix in a lib. So is there a way that you could point out where to look for a problem in the project in order to fix it. I would be glad to provide PR with a fix.

Thank you in advance

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.