Giter Site home page Giter Site logo

santafiora / chesslib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rudzen/chesslib

0.0 0.0 0.0 1.5 MB

C# chess library containing a complete data structure and move generation.

License: MIT License

C# 96.59% HTML 3.32% Dockerfile 0.09%

chesslib's Introduction

ChessLib

A C# chess data library with complete move generation and all needed custom types.

Build status Build & Test Nuget

Requirements

  • .NET 6.0+

What is this for?

This library contains all the data, types and structures for which to create a piece of chess software. It does not contain any heuristics or search algorithms as these are meant to be implemented separately.

It also contains KPK bit compact data to determine endgame draw.

Can I use this as a starting point for my chess software?

Yes you can, it is designed with that in mind.

Features

  • Custom perft application which uses the library to calculate and compare results from custom positions
  • Transposition Table
  • Complete move generation with several types
    • Legal
    • Captures
    • Quiets
    • NonEvasions
    • Evasions
    • QuietChecks
  • Custom compact and very efficient types with tons of operators and helper functionality
    • Bitboard
    • CastleRight
    • Depth
    • Direction
    • ExtMove (move + score)
    • File
    • HashKey
    • Move
    • Piece
    • PieceSquare (for UI etc)
    • PieceValue
    • Player
    • Rank
    • Score
    • Square
    • Value
  • Bitboard use with piece attacks for all types, including lots of helper functions
  • Very fast FEN handling with optional legality check
  • Magic bitboard implementation Copyright (C) 2007 Pradyumna Kannan. Converted to C#
  • FEN input and output supported
  • Chess960 support
  • Zobrist key support
  • Basic UCI structure
  • HiRes timer
  • Draw by repetition detection
  • Mate validation
  • Notation generation
    • Coordinate
    • FAN
    • ICCF
    • LAN
    • RAN
    • SAN
    • SMITH
    • UCI
  • Benchmark project for perft
  • Custom MoveList data structure
  • Pawn blockage algorithm
  • Cuckoo repetition algorithm
  • Polyglot book support
  • Plenty of unit tests to see how it works

Perft

Perft console test program approximate timings to depth 6 for normal start position

  • AMD-FX 8350 = ~12.5 seconds. (without TT) (earlier version)
  • Intel i7-8086k = ~3.3 seconds

Transposition Table

ph

Move Generator

Example

// generate all legal moves for current position
const string fen = "rnbqkbnr/1ppQpppp/p2p4/8/8/2P5/PP1PPPPP/RNB1KBNR b KQkq - 1 6";

var game = GameFactory.Create(fen);
var moveList = game.Pos.GenerateMoves();
// ..

What is not included?

  • Evaluation (except KPK)
  • Search
  • Communication using e.i. UCI (base parameter struct supplied though)

Planned

  • Basic chess engine (search + evaluation) w. UCI support

More Detailed Documentation for Newbies and Beginners

Unit Test "FoolsCheckMateTests.cs"

Implementation in a C# Console App

using Rudzoft.ChessLib;
using Rudzoft.ChessLib.Factories;
using Rudzoft.ChessLib.Fen;
using Rudzoft.ChessLib.MoveGeneration;
using Rudzoft.ChessLib.Types;

namespace ConsoleAppFoolsCheckMateTests
{
    class Program
    {
        static void Main(string[] args)

            //public const string StartPositionFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";

        {
            //var game = GameFactory.Create(Fen.StartPositionFen);
            var game = GameFactory.Create(Fen.StartPositionFen);
            var position = game.Pos;
            var state = new State();

            var moves = new[]
            {
                Move.Create(Square.F2, Square.F3),
                Move.Create(Square.E7, Square.E5),
                Move.Create(Square.G2, Square.G4),
                Move.Create(Square.D8, Square.H4)
            };

            foreach (var move in moves)
                position.MakeMove(move, state);

            if (position.InCheck)
            {
                var resultingMoves = position.GenerateMoves();
                if (resultingMoves.Length == 0)
                {
                    Console.WriteLine("Checkmate!");
                }
                else
                {
                    Console.WriteLine("In check, but not checkmate.");
                }
            }
            else
            {
                Console.WriteLine("Not in check.");
            }
            Console.ReadKey();
        }
    }
}

Unit Test "FenTests.cs"

Implementation in a C# Console App

using Rudzoft.ChessLib.Factories;
using Rudzoft.ChessLib.Fen;
using Rudzoft.ChessLib.Types;

namespace ConsoleAppFenTests.cs
{
    class Program
    {
        static void Main(string[] args)
        {
         
            /*  Test Fens To Use but you can Try many other
             *  rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
             *  q3k1nr/1pp1nQpp/3p4/1P2p3/4P3/B1PP1b2/B5PP/5K2 b k - 0 17
             */

            Console.WriteLine("Enter a FEN string: ");
            var fen = Console.ReadLine();

            var game = GameFactory.Create(fen);
            var actualFen = game.GetFen().ToString();

            Console.WriteLine("The FEN string for the current position is: " + actualFen);

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

chesslib's People

Contributors

rudzen avatar santafiora avatar jongleur1983 avatar

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.