Ejemplo n.º 1
0
        public override PlayerResult GetMove(OthelloBoard board)
        {
            StringBuilder statusString = new StringBuilder();;

            _finalEvaluations = 0;
            _boardEvaluations = 0;
#if USE_TRANSPOSITION_TABLE
            _transpositionHits = 0;
            _transpositionTable._wrongEntriesFound      = 0;
            _transpositionTable._tooShallowEntriesFound = 0;
#endif
            DateTime startTime = DateTime.Now;

            OthelloMoveWithData[] moves          = board.GetValidMovesWithData();
            MoveWithValue[]       movesWithValue = new MoveWithValue[moves.Length];
            for (int i = 0; i < moves.Length; i++)
            {
                movesWithValue[i]      = new MoveWithValue();
                movesWithValue[i].Move = moves[i];
            }

            int currentPly;
#if USE_ITERATIVE_DEEPENING
            for (currentPly = 0; ; currentPly++)
            {
                //for (currentPly = 0; currentPly<2; currentPly++) {
#else
            for (int currentPly = ply - 1; currentPly < ply; currentPly++)
            {
#endif

                statusString.Length = 0;
                DateTime startIterationTime = DateTime.Now;

                statusString.Append("Depth " + (currentPly + 1).ToString() + ": ");

                // Should we do a final search
                if (board.EmptySquares <= 15 && currentPly > 5)
                {
                    currentPly = board.EmptySquares;
                }

                int alpha = WORST_MOVE;
                int beta  = BEST_MOVE;
                foreach (MoveWithValue move in movesWithValue)
                {
                    board.PlayMove(move.Move);

                    statusString.Append(move.Move.ToString());
                    this.PostStatus(statusString.ToString());

                    move.Value = (-GetBoardValueRecursive(board, currentPly, 2, -beta, -alpha));

                    if (move.Value != BEST_MOVE && move.Value != WORST_MOVE)
                    {
                        statusString.Append("(" + move.Value.ToString() + ")");
                    }
                    statusString.Append(" ");
                    this.PostStatus(statusString.ToString());

                    board.UnplayMove(move.Move);

                    if (move.Value > alpha)
                    {
                        alpha = move.Value;
                    }
                    if (alpha >= beta)
                    {
                        break;
                    }
                }

                Array.Sort(movesWithValue);

                // If we've already search to the end, we're done
                if (currentPly == board.EmptySquares)
                {
                    break;
                }

                if (movesWithValue[0].Value < -500 || movesWithValue[0].Value > 500)
                {
                    break;
                }

                TimeSpan t = DateTime.Now - startTime;
                if (t.TotalSeconds >= 3)
                {
                    break;
                }
            }

            PlayerResult result = new PlayerResult();
            result.Move = movesWithValue[0].Move;
            TimeSpan processingTime = DateTime.Now - startTime;
            statusString.Append("\r\nEvals: " + _boardEvaluations + " Final evals: " + _finalEvaluations + "\r\n");
#if USE_TRANSPOSITION_TABLE
            /*
             * statusString.Append("Transp: " + _transpositionHits +
             *  " wrong hits: " + _transpositionTable._wrongEntriesFound +
             *  " shallow hits: " + _transpositionTable._tooShallowEntriesFound + "\r\n");
             */
#endif
            statusString.Append("Score: " + movesWithValue[0].Value + " (" + processingTime + ")");
            result.Status = statusString.ToString();
            return(result);
        }