Ejemplo n.º 1
0
        private double MaxValue(int depth, double alpha, double beta, ChessEngine.Engine.Engine engine)
        {
            //Check if stalemate
            if (engine.IsStalemateBy50MoveRule())
            {
                return(0);
            }

            //Check if checkmate
            if (engine.IsCheckMate())
            {
                return(Double.MinValue);
            }

            //A leaf node, return value of board state
            if ((depth == 0 && maxDepth != 0) || (maxTime.Seconds != 0 && timer.Elapsed > maxTime))
            {
                if (maxTime.Seconds != 0 && timer.Elapsed > maxTime)
                {
                    overTime = true;
                }

                double boardValue = evaluator.ValuateBoard(engine.ChessBoard.Squares);
                return(boardValue);
            }

            double value = Double.MinValue;

            //Find all pieces that can be moved on this turn
            DynamicArray pieceList = ReturnAllMovablePieces(engine);

            //Go through all possible moves
            foreach (byte piece in pieceList)
            {
                foreach (byte move in engine.ChessBoard.Squares[piece].Piece.ValidMoves)
                {
                    //Create the next board stage
                    ChessEngine.Engine.Engine newState = ReturnNewState(engine, piece, move);

                    //Invalid move, continue to next move
                    if (newState == null)
                    {
                        continue;
                    }

                    //Pass parameters to the next node
                    value = MinValue(depth - 1, alpha, beta, newState);

                    //Check if alpha can be given bigger value
                    if (value > alpha)
                    {
                        alpha = value;

                        if (depth == maxDepth)
                        {
                            //Back at the root node with a better move than the previous best
                            byte[] source      = engine.CalculateColumnAndRow(piece);
                            byte[] destination = engine.CalculateColumnAndRow(move);
                            bestMove.SourceColumn      = source[0];
                            bestMove.SourceRow         = source[1];
                            bestMove.DestinationColumn = destination[0];
                            bestMove.DestinationRow    = destination[1];
                        }
                    }

                    //If alpha is bigger than beta, don't investigate branch further
                    if (alpha > beta)
                    {
                        return(alpha);
                    }

                    if (overTime)
                    {
                        break;
                    }
                }

                if (overTime)
                {
                    break;
                }
            }
            return(alpha);
        }
Ejemplo n.º 2
0
        private double MinValue(int depth, double alpha, double beta, ChessEngine.Engine.Engine engine)
        {
            //Check if stalemate
            if (engine.IsStalemateBy50MoveRule())
            {
                return(0);
            }

            //Check if checkmate
            if (engine.IsCheckMate())
            {
                return(Double.MaxValue);
            }

            //A leaf node, return value of board state
            if ((depth == 0 && maxDepth != 0) || (maxTime.Seconds != 0) && timer.Elapsed > maxTime)
            {
                if (maxTime.Seconds != 0 && timer.Elapsed > maxTime)
                {
                    overTime = true;
                }

                double boardValue = evaluator.ValuateBoard(engine.ChessBoard.Squares);
                return(boardValue);
            }

            double value = Double.MaxValue;

            //Find all pieces that can be moved on this turn
            DynamicArray pieceList = ReturnAllMovablePieces(engine);

            //Go through all possible moves
            foreach (byte piece in pieceList)
            {
                foreach (byte move in engine.ChessBoard.Squares[piece].Piece.ValidMoves)
                {
                    //Create the next board stage and pass it to the next node
                    ChessEngine.Engine.Engine newState = ReturnNewState(engine, piece, move);

                    //Invalid move, continue to next move
                    if (newState == null)
                    {
                        continue;
                    }

                    value = MaxValue(depth - 1, alpha, beta, newState);

                    //Check if beta can be given smaller value
                    if (value < beta)
                    {
                        beta = value;
                    }

                    //If beta is smaller than alpha, don't investigate branch further
                    if (beta < alpha)
                    {
                        return(beta);
                    }

                    if (overTime)
                    {
                        break;
                    }
                }

                if (overTime)
                {
                    break;
                }
            }
            return(beta);
        }