internal static void SaveCurrentGameMove(Board currentBoard, Board previousBoard,
                                                 ICollection <OpeningMove> gameBook, MoveContent bestMove)
        {
            try
            {
                var move = new OpeningMove
                {
                    StartingFEN = Board.Fen(true,
                                            previousBoard),
                    EndingFEN = Board.Fen(true,
                                          currentBoard)
                };

                move.Moves.Add(bestMove);

                gameBook.Add(move);

                foreach (var move1 in gameBook)
                {
                    byte repeatedMoves = 0;

                    foreach (var move2 in gameBook)
                    {
                        if (move1.EndingFEN == move2.EndingFEN)
                        {
                            repeatedMoves++;
                        }
                    }

                    if (previousBoard.RepeatedMove >= repeatedMoves)
                    {
                        continue;
                    }

                    previousBoard.RepeatedMove = repeatedMoves;
                    currentBoard.RepeatedMove  = repeatedMoves;
                }

                if (currentBoard.RepeatedMove >= 3)
                {
                    currentBoard.StaleMate = true;
                }
            }
            catch (Exception)
            {
                // ignored
            }
        }
        // ReSharper disable IdentifierTypo
        internal static MoveContent IterativeSearch(Board examineBoard,
                                                    byte depth,
                                                    ref int nodesSearched,
                                                    ref int nodesQuiessence,
                                                    ref string pvLine,
                                                    ref byte plyDepthReached,
                                                    ref byte rootMovesSearched,
                                                    List <OpeningMove> currentGameBook)
        {
            var       pvChild = new List <Position>();
            var       alpha   = -400000000;
            const int beta    = 400000000;

            var bestMove = new MoveContent();

            //We are going to store our result boards here
            var succ = GetSortValidMoves(examineBoard);

            rootMovesSearched = (byte)succ.Positions.Count;

            if (rootMovesSearched == 1)
            {
                return(succ.Positions[0].LastMove);
            }

            foreach (var pos in succ.Positions)
            {
                var value = -AlphaBeta(pos,
                                       1,
                                       -beta,
                                       -alpha,
                                       ref nodesSearched,
                                       ref nodesQuiessence,
                                       ref pvChild,
                                       true);

                //Can I make an instant mate?
                if (value >= 32767)
                {
                    return(pos.LastMove);
                }
            }

            var currentBoard = 0;

            alpha = -400000000;

            succ.Positions.Sort(Sort);

            depth--;

            plyDepthReached = ModifyDepth(depth, succ.Positions.Count);

            foreach (var pos in succ.Positions)
            {
                currentBoard++;

                progress = (int)(currentBoard / (decimal)succ.Positions.Count * 100);

                pvChild = new List <Position>();

                var value = -AlphaBeta(pos,
                                       depth,
                                       -beta,
                                       -alpha,
                                       ref nodesSearched,
                                       ref nodesQuiessence,
                                       ref pvChild,
                                       false);

                if (value >= 32767)
                {
                    return(pos.LastMove);
                }

                if (examineBoard.RepeatedMove == 2)
                {
                    var fen = Board.Fen(true, pos);

                    foreach (var move in currentGameBook)
                    {
                        if (move.EndingFEN == fen)
                        {
                            value = 0;
                            break;
                        }
                    }
                }

                pos.Score = value;

                //If value is greater then alpha this is the best board
                // ReSharper disable once InvertIf
                if (value > alpha || alpha == -400000000)
                {
                    pvLine = pos.LastMove.ToString();

                    foreach (var pvPos in pvChild)
                    {
                        pvLine += " " + pvPos.ToString();
                    }

                    alpha    = value;
                    bestMove = pos.LastMove;
                }
            }

            plyDepthReached++;
            progress = 100;

            return(bestMove);
        }