0

I am programming a simple algorithm for chess AI (minimax). I have a functions that takes a list of possible moves and returning a score for the best move in a current state (taking into account the board state). I want to add a way to check if a current board state is already seen (I don't need nor want to save all the already seen boards, just to know if the current board already seen and what the assigned value was, something like a hash code). In general, I want a way to to assign a code (again, like a hash code) for a class (that contains multiple properties, both value and refence type) and then save this code for future checks.

I emphasizes, I don't want to check only the board/class reference but I want to check the board/class content.

this is a board class:

        public class ChessBoard
    {
        public Piece[,] board; //Piece as a class as well
        public PieceColor myColor; //enum
        public PieceColor opColor; //enum
        public DateTime lastMoveTime;
        public TimeSpan myTimer;
        public TimeSpan opTimer;
        public GameState currentTurn; //enum
        public int enPassantCol = -1;
        public bool whiteShortCastleFlag = true;
        public bool whiteLongCastleFlag = true;
        public bool blackShortCastleFlag = true;
        public bool blackLongCastleFlag = true;
        //more nonrelevant fields, constructors and logic
        }

the board Piece array can be checked by the references. no need to check Piece content (that means, if all the elements in Piece[,] array points to the same references, this is ok, all the Pieces content stay the same, they are just moving in the board array).

Thank you.

Yakir Shlezinger
  • 147
  • 1
  • 13
  • R# can generate a Hashcode method for you. And even [VS can, it seems](https://learn.microsoft.com/en-us/visualstudio/ide/reference/generate-equals-gethashcode-methods?view=vs-2019) – Thomas Weller Jul 24 '21 at 19:55
  • It seems you want to be able to compare instances of `ChessBoard`, so that you can save copies of them and compare those to the current one. See duplicate. Note that you will also need to understand how to make "deep clones" of the instances (see e.g. https://stackoverflow.com/questions/78536/deep-cloning-objects), and how to maintain a collection of the instances you've seen (see e.g. `HashSet`) – Peter Duniho Jul 24 '21 at 19:56
  • Thank you, in regards to cloning the class and compare them to already seen one, this is exactly what I dont want ( I dont want to mess with "deep clones" and I dont need the content anyway). I want a way to generate some code (again, like a hash) that I will save and check with future boards hash code for equality. If the current board hash is the same as a previous board hash (that I didn't save, I saved only the hash and the score), I can use the saved score and no need for me to check the board again. In regards to R#, do you have more information on the subject? Thank you. – Yakir Shlezinger Jul 24 '21 at 20:00
  • This might be of assistance: [Zobrist hashing](https://en.wikipedia.org/wiki/Zobrist_hashing). – Michael Roy Jul 25 '21 at 02:53

0 Answers0