Esempio n. 1
0
        private static ScorePair AlphaBetaMax(ChessBoard chessBoard, int alpha, int beta, int depth)
        {
            if (depth == 0)
            {
                return(new ScorePair(CountScore(chessBoard, chessBoard.CurrentPlayer), null));
            }
            Serializator ser = new Serializator();
            List <Tuple <ChessPiece, Tuple <int, int> > > allMovesThisTurn = chessBoard
                                                                             .GetAllPossibleMoves()
                                                                             .FindAll(chessPieceNMoves => chessPieceNMoves.Item1.GetPlayer() == chessBoard.CurrentPlayer);

            MovePair bestMove = null;

            foreach (var move in allMovesThisTurn)
            {
                ChessBoard chessBoardCopy = ser.DeepCopy(chessBoard);
                var        thisMove       = new MovePair(move.Item1.Position, move.Item2);
                if (chessBoardCopy.MovePiece(thisMove.Item1, thisMove.Item2))
                {
                    var score = AlphaBetaMin(chessBoardCopy, alpha, beta, depth - 1).Item1;
                    if (score >= beta)
                    {
                        return(new ScorePair(beta, null)); // fail hard beta-cutoff
                    }
                    if (score > alpha)
                    {
                        alpha    = score; // alpha acts like max in MiniMax
                        bestMove = thisMove;
                    }
                }
            }

            return(new ScorePair(alpha, bestMove));
        }
Esempio n. 2
0
        private static ScorePair NegaMax(ChessBoard chessBoard, int depth,
                                         MovePair lastMove = null)
        {
            Serializator ser = new Serializator();

            if (depth == 0)
            {
                return(new Tuple <int, MovePair>(
                           CountScore(chessBoard, chessBoard.CurrentPlayer), lastMove));
            }

            ScorePair maxS = new ScorePair(Int32.MinValue, null);

            List <Tuple <ChessPiece, Tuple <int, int> > > allMovesThisTurn = chessBoard
                                                                             .GetAllPossibleMoves()
                                                                             .FindAll(chessPieceNMoves => chessPieceNMoves.Item1.GetPlayer() == chessBoard.CurrentPlayer);

            foreach (var move in allMovesThisTurn)
            {
                ChessBoard chessBoardCopy = ser.DeepCopy(chessBoard);
                var        thisMove       = new MovePair(move.Item1.Position, move.Item2);

                if (chessBoardCopy.MovePiece(thisMove.Item1, thisMove.Item2))
                {
                    int score = -NegaMax(chessBoardCopy, depth - 1, thisMove).Item1;
                    if (score > maxS.Item1)
                    {
                        maxS = new ScorePair(score, thisMove);
                    }
                }
            }

            return(maxS);
        }