Click here to Skip to main content
11,428,876 members (62,561 online)
Click here to Skip to main content
Add your own
alternative version

Chess Program in C#

, 8 Jan 2015 GPL3
SrcChess is a chess program built in C#
Article_demo-noexe.zip
111probs.pgn
Article_demo.zip
111probs.pgn
SrcChess2.exe
Article_src.zip
SrcChess2
Analyze.ruleset
Book.bin
PieceSets
Adventurer
Alpfonso-X
Alpha
Alpha-2
Arial Unicode MS
Berlin
Cases
Chess-7
Condal
Harlequin
Kingdom
Leipzig
Line
Lucena
Magnetic
Maya
Mediaeval
Merida
Millennia
Motif
MS Mincho
Plain
Segoe UI Symbol
Smart
Traveller Standard
Usual
Properties
Settings.settings
Resources
image.png
using System;

namespace SrcChess2 {
    /// <summary>
    /// Zobrist key implementation.
    /// </summary>
    public static class ZobristKey {

        /// <summary>Random value for each piece/position</summary>
        static private Int64[]              s_pi64RndTable;

        /// <summary>
        /// Static constructor. Use to create the random value for each case of the board.
        /// </summary>
        static ZobristKey() {
            Random  rnd;
            long    lPart1;
            long    lPart2;
            long    lPart3;
            long    lPart4;
            
            rnd            = new Random(0);
            s_pi64RndTable = new Int64[64 * 16];
            for (int i = 0; i < 64 * 16; i++) {
                lPart1            = (long)rnd.Next(65536);
                lPart2            = (long)rnd.Next(65536);
                lPart3            = (long)rnd.Next(65536);
                lPart4            = (long)rnd.Next(65536);
                s_pi64RndTable[i] = (lPart1 << 48) | (lPart2 << 32) | (lPart3 << 16) | lPart4;
            }
        }

        /// <summary>
        /// Update the Zobrist key using the specified move
        /// </summary>
        /// <param name="i64ZobristKey">Zobrist key</param>
        /// <param name="iPos">         Piece position</param>
        /// <param name="eOldPiece">    Old value</param>
        /// <param name="eNewPiece">    New value</param>
        public static long UpdateZobristKey(long i64ZobristKey, int iPos, ChessBoard.PieceE eOldPiece, ChessBoard.PieceE eNewPiece) {
            int     iBaseIndex;
            
            iBaseIndex     = iPos << 4;
            i64ZobristKey ^= s_pi64RndTable[iBaseIndex + ((int)eOldPiece)] ^
                             s_pi64RndTable[iBaseIndex + ((int)eNewPiece)];
            return(i64ZobristKey);                             
        }

        /// <summary>
        /// Update the Zobrist key using the specified move
        /// </summary>
        /// <param name="i64ZobristKey">Zobrist key</param>
        /// <param name="iPos1">        Piece position</param>
        /// <param name="eOldPiece1">   Old value</param>
        /// <param name="eNewPiece1">   New value</param>
        /// <param name="iPos2">        Piece position</param>
        /// <param name="eOldPiece2">   Old value</param>
        /// <param name="eNewPiece2">   New value</param>
        public static long UpdateZobristKey(long               i64ZobristKey,
                                            int                iPos1,
                                            ChessBoard.PieceE  eOldPiece1,
                                            ChessBoard.PieceE  eNewPiece1,
                                            int                iPos2,
                                            ChessBoard.PieceE  eOldPiece2,
                                            ChessBoard.PieceE  eNewPiece2) {
            int     iBaseIndex1;
            int     iBaseIndex2;
            
            iBaseIndex1    = iPos1 << 4;
            iBaseIndex2    = iPos2 << 4;
            i64ZobristKey ^= s_pi64RndTable[iBaseIndex1 + ((int)eOldPiece1)] ^
                             s_pi64RndTable[iBaseIndex1 + ((int)eNewPiece1)] ^
                             s_pi64RndTable[iBaseIndex2 + ((int)eOldPiece2)] ^
                             s_pi64RndTable[iBaseIndex2 + ((int)eNewPiece2)];
            return(i64ZobristKey);                             
        }

        /// <summary>
        /// Compute the zobrist key for a board
        /// </summary>
        /// <param name="peBoard">      Board</param>
        public static long ComputeBoardZobristKey(ChessBoard.PieceE[] peBoard) {
            long    lRetVal = 0;
            
            for (int iIndex = 0; iIndex < 64; iIndex++) {
                lRetVal ^= s_pi64RndTable[(iIndex << 4) + (int)peBoard[iIndex]];
            }
            return(lRetVal);
        }
    } // Class ZobristKey
} // Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Jacques Fournier
Web Developer Consyst SQL
Canada Canada
Consyst is a dynamic IT company specialized for more than 20 years in information technology architecture and in the development of innovative productivity tools for businesses. Rep++, the product at the core of its mission, can significantly accelerate the development cycle of applications and services by reducing the duration of the design, coding, testing and maintenance stages.
Rep++ uses a model-driven approach supported by a powerful model execution mechanism. Essential complement to Visual Studio® (Microsoft®), Rep++ includes: an open and centralized model that is used to define, contain and manage all the metadata of an application set; toolkits and application frameworks that implement various flavors of the presentation layer; and specialized assistants that simplify the creation of applications and services for a variety of architectures and technologies. These elements provide a very high automation level, which enable businesses to focus their development efforts on where it counts: their business rules.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 8 Jan 2015
Article Copyright 2009 by Jacques Fournier
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid