Esempio n. 1
0
    private Move computeBestMove(Side side, Chessboard chessboard, int presentCumulativeScore)
    {
        currentDepth++;

        // First, get the set of available moves
        List <Move> moves = new List <Move>();
        Move        bestMoveForIteration = new Move(null, null);

        bool isPlayingSide = chessboard.CurrentMovingSide() == side;

        foreach (AbstractPiece piece in chessboard.getActivePieces())
        {
            if (piece.side == chessboard.CurrentMovingSide())
            {
                foreach (Position movePosition in piece.GetSafeMovesForCurrentPosition().GetPositions())
                {
                    Move newMove = MoveEvaluator.EvaluateMove(new Move(piece, movePosition), isPlayingSide);
                    moves = InsertionSortMove(moves, newMove);
                }
            }
        }

        if (moves.Count > 0)
        {
            bestMoveForIteration = moves.ToArray()[0];
            if (currentDepth < maxAllowableDepth)
            {
                foreach (Move move in moves)
                {
                    Chessboard copyChessboard = Chessboard.MakeCopyOfChessboard(chessboard);
                    copyChessboard.MoveTo(copyChessboard.GetPieceAtPosition(move.getPiece().GetCurrentPosition()), move.getPosition());
                    copyChessboard.ChangeMovingSide();
                    int followUpScore = computeBestMove(side, copyChessboard, presentCumulativeScore + move.getScore()).getScore();

                    if (move.getScore() + followUpScore > bestMoveForIteration.getScore())
                    {
                        bestMoveForIteration = move;
                    }
                }
            }
        }

        currentDepth--;

        return(bestMoveForIteration);
    }