Example #1
0
 public IterativeSearch(Board board, long maxNodes = long.MaxValue)
 {
     _root     = new Board(board);
     _killers  = new KillerMoves(4);
     _history  = new History();
     _maxNodes = maxNodes;
 }
Example #2
0
        internal static IEnumerable <(Move Move, Board Board)> Play(Board position, int depth, KillerMoves killers, History history)
        {
            //1. Is there a known best move for this position? (PV Node)
            if (Transpositions.GetBestMove(position, out Move bestMove))
            {
                var nextPosition = new Board(position, bestMove);
                yield return(bestMove, nextPosition);
            }

            //2. Try all captures ordered by Mvv-Lva
            foreach (var capture in MoveList.SortedCaptures(position))
            {
                var nextPosition = new Board(position, capture);
                if (!nextPosition.IsChecked(position.SideToMove))
                {
                    yield return(capture, nextPosition);
                }
            }

            //3. Play quiet moves that have caused a beta cutoff elsewhere if available
            foreach (Move killer in killers.Get(depth))
            {
                if (position[killer.ToSquare] != Piece.None || !position.IsPlayable(killer))
                {
                    continue;
                }

                var nextPosition = new Board(position, killer);
                if (!nextPosition.IsChecked(position.SideToMove))
                {
                    yield return(killer, nextPosition);
                }
            }

            //4. Play the remaining quiet moves ordered by history
            foreach (var move in MoveList.SortedQuiets(position, history))
            {
                if (killers.Contains(depth, move))
                {
                    continue;
                }

                var nextPosition = new Board(position, move);
                if (!nextPosition.IsChecked(position.SideToMove))
                {
                    yield return(move, nextPosition);
                }
            }
        }