示例#1
0
 public ThreadStartArguments(Position pos, sbyte depth, SecureFloat ansPlace, SecureFloat globalAlpha, SecureFloat globalBeta)
 {
     this.pos         = pos;
     this.depth       = depth;
     this.ansPlace    = ansPlace;
     this.globalAlpha = globalAlpha;
     this.globalBeta  = globalBeta;
 }
示例#2
0
        public void threadStart(Position pos, sbyte depth, int moveIndex, SecureFloat bestMove,
                                SecureFloat ansPlace, SecureFloat globalAlpha, SecureFloat globalBeta)
        {
            float value = alphaBeta(pos, depth, globalAlpha, globalBeta, false);

            ansPlace.setValue(value);
            if (!pos.whiteToMove)
            {
                if (value > globalAlpha.getValue())
                {
                    globalAlpha.setValue(value);
                    bestMove.setValue(moveIndex);
                }
            }
            else
            {
                if (value < globalBeta.getValue())
                {
                    globalBeta.setValue(value);
                    bestMove.setValue(moveIndex);
                }
            }
        }
示例#3
0
        public EvalResult eval(Position pos, int minDepth)
        {
            //Om minDepth är -1, skall datorn själv bestämma djup.
            if (minDepth == -1)
            {
                minDepth = automaticDepth(pos);
            }
            List <Move> moves  = allValidMoves(pos, true);
            EvalResult  result = new EvalResult();

            int gameResult = decisiveResult(pos, moves);

            if (gameResult != -2)
            {
                result.evaluation = gameResult;
                result.allMoves   = new List <Move>();
                result.allEvals   = null;
                return(result); //Ställningen är avgjord.
            }
            else
            {
                List <SecureFloat> allEvals = new List <SecureFloat>();
                float bestValue             = pos.whiteToMove ? float.NegativeInfinity : float.PositiveInfinity;

                if (moves.Count == 1)
                {
                    //Spela forcerande drag omedelbart?
                    EvalResult res = eval(moves[0].execute(pos), minDepth - 1);
                    result.evaluation = evaluationStep(res.evaluation);
                    result.allMoves   = moves;
                    result.bestMove   = moves[0];
                    return(result);
                }

                // Ökar minimum-djupet om antalet tillgängliga drag är färre än de som anges
                // i moveIncreaseLimits, vilka återfinns i EngineData.
                foreach (int item in moveIncreaseLimits)
                {
                    if (moves.Count <= item)
                    {
                        minDepth++;
                    }
                    else
                    {
                        break;
                    }
                }


                SecureFloat globalAlpha = new SecureFloat();
                globalAlpha.setValue(float.NegativeInfinity);
                SecureFloat globalBeta = new SecureFloat();
                globalBeta.setValue(float.PositiveInfinity);
                List <Thread> threadList = new List <Thread>();

                SecureFloat bestMove = new SecureFloat(0);
                for (int i = 0; i < moves.Count; i++)
                //foreach (Move currentMove in moves)
                {
                    Move        currentMove = moves[i];
                    SecureFloat newFloat    = new SecureFloat();
                    int         iCopy       = i;
                    allEvals.Add(newFloat);

                    Thread thread = new Thread(delegate()
                    {
                        threadStart(currentMove.execute(pos), (sbyte)(minDepth - 1), iCopy, bestMove, newFloat, globalAlpha, globalBeta);
                    });
                    thread.Name = currentMove.toString(pos);
                    thread.Start();
                    threadList.Add(thread);
                }
                //result.bestMove = moves[0];
                Thread.Sleep(sleepTime);
                for (int i = 0; i < allEvals.Count; i++)
                {
                    SecureFloat threadResult = allEvals[i];
                    float       value        = threadResult.getValue();
#pragma warning disable CS1718          // Comparison made to same variable
                    if (value != value) //Kollar om talet är odefinierat.
#pragma warning restore CS1718          // Comparison made to same variable
                    {
                        //Om resultatet inte hunnit beräknas.
                        if (cancelFlag.getValue() != 0)
                        {
                            abortAll(threadList);
                            result.bestMove   = null;
                            result.evaluation = float.NaN;
                            result.allEvals   = null;
                            Thread.Sleep(10); //För att ge övriga trådar chans att stanna.
                            return(result);
                        }
                        else if (this.moveNowFlag.getValue() != 0)
                        {
                            abortAll(threadList);
                            Thread.Sleep(10); //För att ge övriga trådar chans att stanna.
                            result.bestMove = moves[(int)bestMove.getValue()];
                            return(result);
                        }
                        Thread.Sleep(sleepTime);
                        i--;
                    }
                    else
                    {
                        //Om resultatet är klart.
                        result.bestMove = moves[(int)bestMove.getValue()];
                    }
                }
                if (pos.whiteToMove)
                {
                    result.evaluation = globalAlpha.getValue();
                }
                else
                {
                    result.evaluation = globalBeta.getValue();
                }
                result.allEvals = allEvals;
            }

            result.allMoves = moves;
            return(result);
        }