Ejemplo n.º 1
0
        public void BeginSearch(string fen, ISearchResults target, TimeSpan timeVailable)
        {
            var board = CreateBoard(fen);
            var eg    = EndGameReporter.ReportEndGame(board);

            if (eg != GameEnded.None)
            {
                target.SearchDone(string.Empty, eg);
            }
            else
            {
                BackgroundWorker wrk = new BackgroundWorker();
                wrk.DoWork += (e, a) =>
                {
                    int    bestMove = DoSearch(board, timeVailable);
                    string move     = string.Empty;
                    if (bestMove != 0)
                    {
                        move = MovePackHelper.GetAlgebraicString(bestMove);
                    }
                    target.SearchDone(move, EndGameReporter.ReportEndGame(board));
                };
                wrk.RunWorkerAsync();
            }
        }
Ejemplo n.º 2
0
        protected override int DoSearch(IChessBoard board, TimeSpan timeAvail)
        {
            this.totalMillisec = (int)timeAvail.TotalMilliseconds;
            this.board         = board;
            int  bestMove = 0;
            int  val      = 0;
            int  alpha;
            bool timeout = false;

            kslot = new long[200];
            using (clk = new RunClock())
            {
                var preOrder = GetPreOrderedMoves();
                for (int curDepth = 1; curDepth <= maxDepth; ++curDepth) // iterative deeping
                {
                    alpha = -INFINITE;
                    var moves = GetBestMoveFirst(bestMove, preOrder);
                    //var moves = GetMoves(bestMove);
                    foreach (var m in moves)
                    {
                        board.Move(m);
                        var cm = MovePackHelper.GetAlgebraicString(m);
                        if (m == bestMove || -AlphaBeta(curDepth - 1, -alpha - 1, -alpha) > alpha)
                        {
                            val = -AlphaBeta(curDepth - 1, -INFINITE, -alpha);
                        }
                        board.UndoMove();

                        if (val > alpha)
                        {
                            alpha    = val;
                            bestMove = m;
                            ttable.Save(board.ZKey, curDepth, alpha, TTType.Alpha, m);
                        }

                        if (clk.GetElapsedMilliseconds() > timeAvail.TotalMilliseconds / .5)
                        {
                            timeout = true;
                            break;
                        }
                    }
                    if (timeout)
                    {
                        break;
                    }
                    ttable.Save(board.ZKey, curDepth, alpha, TTType.Alpha, bestMove);
                }
            }
            return(bestMove);
        }