示例#1
0
        public int QuiescenceSearch(Gameboard state, int alpha, int beta, bool myMove)
        {
            // DEBUG
            iterationCounter++;

            int score = int.MinValue;

            GameLogic gameLogic            = new GameLogic(state);
            var       possibleCaptureMoves = gameLogic.GetPossibleCaptureMoves();

            // If there are no capture moves possible, use evaluation function and return value
            if (possibleCaptureMoves.Count == 0)
            {
                score = myMove ? evaluation.Evaluate(state) : -evaluation.Evaluate(state);
            }
            // If there are capture moves, continue iterating
            else
            {
                foreach (Move move in possibleCaptureMoves)
                {
                    Gameboard newState = (Gameboard)state.Clone();
                    GameLogic newLogic = new GameLogic(newState);
                    newLogic.ApplyMove(move);

                    int value = -QuiescenceSearch(newState, -beta, -alpha, !myMove);
                    if (value > score)
                    {
                        score = value;
                        if (score >= beta)
                        {
                            break;
                        }
                        if (score > alpha)
                        {
                            alpha = score;
                        }
                    }
                }
            }

            return(score);
        }
示例#2
0
        public int AlphaBeta(Gameboard state, int depth, int alpha, int beta, bool myMove)
        {
            // DEBUG
            iterationCounter++;

            GameLogic gameLogic = new GameLogic(state);

            if (depth <= 0 || gameLogic.IsFinished())
            {
                return(myMove ? evaluation.Evaluate(state) : -evaluation.Evaluate(state));
            }

            int score         = int.MinValue;
            var possibleMoves = gameLogic.GetPossibleMoves();

            foreach (Move move in possibleMoves)
            {
                Gameboard newState = (Gameboard)state.Clone();
                GameLogic newLogic = new GameLogic(newState);
                newLogic.ApplyMove(move);

                int value = -AlphaBeta(newState, depth - 1, -beta, -alpha, !myMove);
                if (value > score)
                {
                    score = value;
                }
                if (score > alpha)
                {
                    alpha = score;
                }
                if (score >= beta)
                {
                    break;
                }
            }

            return(score);
        }
示例#3
0
        public int AlphaBeta(Gameboard state, int depth, int alpha, int beta, bool myMove)
        {
            Int64 zHash    = state.GetZobristHash();
            Move  bestMove = null;

            // Check transposition table
            if (transpositionTable.ContainsKey(zHash))
            {
                Transposition transposition = transpositionTable[zHash];

                bestMove = transposition.BestMove;

                if (transposition.Depth >= depth)
                {
                    if (transposition.Lowerbound == transposition.Upperbound)
                    {
                        return(transposition.Lowerbound);
                    }

                    if (transposition.Lowerbound >= beta)
                    {
                        return(transposition.Lowerbound);
                    }
                    if (transposition.Upperbound <= alpha)
                    {
                        return(transposition.Upperbound);
                    }
                    alpha = Math.Max(alpha, transposition.Lowerbound);
                    beta  = Math.Min(beta, transposition.Upperbound);
                }
            }

            GameLogic gameLogic = new GameLogic(state);
            int       score;

            // Evaluate
            if (depth <= 0 || gameLogic.IsFinished())
            {
                score = myMove ? evaluation.Evaluate(state) : -evaluation.Evaluate(state);
            }
            // Search deeper
            else
            {
                score = int.MinValue;
                var possibleMoves = gameLogic.GetPossibleMoves();

                OrderMoves(possibleMoves, state, bestMove);
                // Reset best move
                bestMove = null;

                foreach (Move move in possibleMoves)
                {
                    Gameboard newState = (Gameboard)state.Clone();
                    GameLogic newLogic = new GameLogic(newState);
                    newLogic.ApplyMove(move);

                    // Continue search
                    int value = -AlphaBeta(newState, depth - 1, -beta, -alpha, !myMove);

                    // Adjust alpha and beta
                    if (value > score)
                    {
                        bestMove = move;
                        score    = value;
                    }
                    if (score > alpha)
                    {
                        alpha = score;
                    }
                    if (score >= beta)
                    {
                        break;
                    }
                }
            }


            // Store values in transposition table
            if (score <= alpha)
            {
                if (!transpositionTable.ContainsKey(zHash))
                {
                    Transposition transposition = new Transposition(alpha, score, depth, bestMove);
                    transpositionTable.Add(state.GetZobristHash(), transposition);
                }
                else
                {
                    Transposition transposition = transpositionTable[zHash];
                    transposition.Upperbound = score;
                    transposition.Depth      = depth;
                    transposition.BestMove   = bestMove;
                }
            }
            else if (score > alpha && score < beta)
            {
                if (!transpositionTable.ContainsKey(zHash))
                {
                    Transposition transposition = new Transposition(score, score, depth, bestMove);
                    transpositionTable.Add(state.GetZobristHash(), transposition);
                }
                else
                {
                    Transposition transposition = transpositionTable[zHash];
                    transposition.Lowerbound = score;
                    transposition.Upperbound = score;
                    transposition.Depth      = depth;
                    transposition.BestMove   = bestMove;
                }
            }
            else if (score >= beta)
            {
                if (!transpositionTable.ContainsKey(zHash))
                {
                    Transposition transposition = new Transposition(score, beta, depth, bestMove);
                    transpositionTable.Add(state.GetZobristHash(), transposition);
                }
                else
                {
                    Transposition transposition = transpositionTable[zHash];
                    transposition.Lowerbound = score;
                    transposition.Depth      = depth;
                    transposition.BestMove   = bestMove;
                }
            }

            return(score);
        }
示例#4
0
 public override int Evaluate(Gameboard state)
 {
     return(evaluation.Evaluate(state));
 }
示例#5
0
        public int AlphaBeta(Gameboard state, int depth, int alpha, int beta, bool myMove)
        {
            // DEBUG
            iterationCounter++;

            Int64 zHash    = state.GetZobristHash();
            Move  bestMove = null;

            if (transpositionTable.ContainsKey(zHash))
            {
                Transposition transposition = transpositionTable[zHash];

                bestMove = transposition.BestMove;

                if (transposition.Depth >= depth)
                {
                    if (transposition.Lowerbound == transposition.Upperbound)
                    {
                        return(transposition.Lowerbound);
                    }

                    if (transposition.Lowerbound >= beta)
                    {
                        return(transposition.Lowerbound);
                    }
                    if (transposition.Upperbound <= alpha)
                    {
                        return(transposition.Upperbound);
                    }
                    alpha = Math.Max(alpha, transposition.Lowerbound);
                    beta  = Math.Min(beta, transposition.Upperbound);
                }
            }

            GameLogic gameLogic = new GameLogic(state);
            int       score;

            if (depth <= 0 || gameLogic.IsFinished())
            {
                score = myMove ? evaluation.Evaluate(state) : -evaluation.Evaluate(state);
            }
            else
            {
                score = int.MinValue;
                var possibleMoves = gameLogic.GetPossibleMoves();

                OrderMoves(possibleMoves, state);
                // Reset best move
                bestMove = null;

                // Calculate score for PVS
                {
                    Move pvsMove = possibleMoves[0];

                    Gameboard newState = (Gameboard)state.Clone();
                    GameLogic newLogic = new GameLogic(newState);
                    newLogic.ApplyMove(pvsMove);

                    score = -AlphaBeta(newState, depth - 1, -beta, -alpha, !myMove);

                    bestMove = pvsMove;
                }

                if (score < beta)
                {
                    for (int i = 1; i < possibleMoves.Count; i++)
                    {
                        Move move       = possibleMoves[i];
                        int  lowerbound = Math.Max(alpha, score);
                        int  upperbound = lowerbound + 1;

                        Gameboard newState = (Gameboard)state.Clone();
                        GameLogic newLogic = new GameLogic(newState);
                        newLogic.ApplyMove(move);

                        int value = -AlphaBeta(newState, depth - 1, -upperbound, -lowerbound, !myMove);

                        // Fail high
                        if (value >= upperbound && value < beta)
                        {
                            value = -AlphaBeta(newState, depth - 1, -beta, -value, !myMove);
                        }

                        if (value > score)
                        {
                            bestMove = move;
                            score    = value;
                        }
                        if (score >= beta)
                        {
                            break;
                        }
                    }
                }
            }

            if (score <= alpha)
            {
                if (!transpositionTable.ContainsKey(zHash))
                {
                    Transposition transposition = new Transposition(alpha, score, depth, bestMove);
                    transpositionTable.Add(state.GetZobristHash(), transposition);
                }
                else
                {
                    Transposition transposition = transpositionTable[zHash];
                    transposition.Upperbound = score;
                    transposition.Depth      = depth;
                    transposition.BestMove   = bestMove;
                }
            }
            else if (score > alpha && score < beta)
            {
                if (!transpositionTable.ContainsKey(zHash))
                {
                    Transposition transposition = new Transposition(score, score, depth, bestMove);
                    transpositionTable.Add(state.GetZobristHash(), transposition);
                }
                else
                {
                    Transposition transposition = transpositionTable[zHash];
                    transposition.Lowerbound = score;
                    transposition.Upperbound = score;
                    transposition.Depth      = depth;
                    transposition.BestMove   = bestMove;
                }
            }
            else if (score >= beta)
            {
                if (!transpositionTable.ContainsKey(zHash))
                {
                    Transposition transposition = new Transposition(score, beta, depth, bestMove);
                    transpositionTable.Add(state.GetZobristHash(), transposition);
                }
                else
                {
                    Transposition transposition = transpositionTable[zHash];
                    transposition.Lowerbound = score;
                    transposition.Depth      = depth;
                    transposition.BestMove   = bestMove;
                }
            }

            return(score);
        }
示例#6
0
        public int AlphaBeta(Gameboard state, int depth, int alpha, int beta, bool myMove)
        {
            // DEBUG
            iterationCounter++;

            Int64 zHash = state.GetZobristHash();

            Move bestMove = null;

            if (transpositionTable.ContainsKey(zHash))
            {
                Transposition transposition = transpositionTable[zHash];

                bestMove = transposition.BestMove;

                if (transposition.Depth >= depth)
                {
                    if (transposition.Lowerbound == transposition.Upperbound)
                    {
                        return(transposition.Lowerbound);
                    }

                    if (transposition.Lowerbound >= beta)
                    {
                        return(transposition.Lowerbound);
                    }
                    if (transposition.Upperbound <= alpha)
                    {
                        return(transposition.Upperbound);
                    }
                    alpha = Math.Max(alpha, transposition.Lowerbound);
                    beta  = Math.Min(beta, transposition.Upperbound);
                }
            }

            GameLogic gameLogic = new GameLogic(state);
            int       score;

            if (depth <= 0 || gameLogic.IsFinished())
            {
                score = myMove ? evaluation.Evaluate(state) : -evaluation.Evaluate(state);
            }
            else
            {
                score = int.MinValue;
                var possibleMoves = gameLogic.GetPossibleMoves();

                OrderMoves(possibleMoves, state);
                // Reset best move
                bestMove = null;

                // Multi-Cut
                {
                    Move move = possibleMoves[0];

                    Gameboard newState = (Gameboard)state.Clone();
                    GameLogic newLogic = new GameLogic(newState);
                    newLogic.ApplyMove(move);

                    int c = 0;
                    int m = 0;

                    while (newState != null && m < 10)
                    {
                        int value = -AlphaBeta(newState, depth - 1 - 2, GameLogic.LOSS_VALUE, GameLogic.WIN_VALUE, false);

                        if (value >= beta)
                        {
                            c++;
                            if (c > 3)
                            {
                                return(beta);
                            }
                        }

                        m++;

                        if (m < possibleMoves.Count)
                        {
                            move = possibleMoves[m];

                            newState = (Gameboard)state.Clone();
                            newLogic = new GameLogic(newState);
                            newLogic.ApplyMove(move);
                        }
                        else
                        {
                            newState = null;
                        }
                    }
                }

                foreach (Move move in possibleMoves)
                {
                    Gameboard newState = (Gameboard)state.Clone();
                    GameLogic newLogic = new GameLogic(newState);
                    newLogic.ApplyMove(move);

                    int value = -AlphaBeta(newState, depth - 1, -beta, -alpha, !myMove);
                    if (value > score)
                    {
                        bestMove = move;
                        score    = value;
                    }
                    if (score > alpha)
                    {
                        alpha = score;
                    }
                    if (score >= beta)
                    {
                        break;
                    }
                }
            }

            if (score <= alpha)
            {
                if (!transpositionTable.ContainsKey(zHash))
                {
                    Transposition transposition = new Transposition(alpha, score, depth, bestMove);
                    transpositionTable.Add(state.GetZobristHash(), transposition);
                }
                else
                {
                    Transposition transposition = transpositionTable[zHash];
                    transposition.Upperbound = score;
                    transposition.Depth      = depth;
                    transposition.BestMove   = bestMove;
                }
            }
            else if (score > alpha && score < beta)
            {
                if (!transpositionTable.ContainsKey(zHash))
                {
                    Transposition transposition = new Transposition(score, score, depth, bestMove);
                    transpositionTable.Add(state.GetZobristHash(), transposition);
                }
                else
                {
                    Transposition transposition = transpositionTable[zHash];
                    transposition.Lowerbound = score;
                    transposition.Upperbound = score;
                    transposition.Depth      = depth;
                    transposition.BestMove   = bestMove;
                }
            }
            else if (score >= beta)
            {
                if (!transpositionTable.ContainsKey(zHash))
                {
                    Transposition transposition = new Transposition(score, beta, depth, bestMove);
                    transpositionTable.Add(state.GetZobristHash(), transposition);
                }
                else
                {
                    Transposition transposition = transpositionTable[zHash];
                    transposition.Lowerbound = score;
                    transposition.Depth      = depth;
                    transposition.BestMove   = bestMove;
                }
            }

            return(score);
        }