Exemplo n.º 1
0
    ScoringMove Test(AIBoard board, byte depth, int gamma)
    {
        // Devuelve el score del tablero y la jugada con la que se llega a él.
        List <MoveMarble> bestMove = new List <MoveMarble>();
        int bestScore = 0;

        ScoringMove scoringMove; // score, movimiento
        AIBoard     newBoard;
        Record      record;

        if (depth > maximumExploredDepth)
        {
            maximumExploredDepth = depth;
        }

        record = transpositionTable.GetRecord(board.hashValue);

        if (record != null)
        {
            if (record.depth > MAX_DEPTH - depth)
            {
                if (record.minScore > gamma)
                {
                    scoringMove = new ScoringMove(record.minScore, record.bestMove);
                    return(scoringMove);
                }

                if (record.maxScore < gamma)
                {
                    scoringMove = new ScoringMove(record.maxScore, record.bestMove);
                    return(scoringMove);
                }
            }
        }
        else
        {
            record           = new Record();
            record.hashValue = board.hashValue;
            record.depth     = MAX_DEPTH - depth;
            record.minScore  = MINUS_INFINITE;
            record.maxScore  = INFINITE;
        }

        // Comprobar si hemos terminado de hacer recursión
        if (board.IsEndOfGame() || depth == MAX_DEPTH)
        {
            if (depth % 2 == 0)
            {
                record.maxScore = board.Evaluate(activePlayer);
            }
            else
            {
                record.maxScore = -board.Evaluate(activePlayer);
            }

            record.minScore = record.maxScore;
            transpositionTable.SaveRecord(record);

            scoringMove = new ScoringMove(record.maxScore);
        }
        else
        {
            bestScore = MINUS_INFINITE;

            //modificar AIBoard.possibleMoves() para que devuelva los posibles movimientos
            List <MoveMarble>[] possibleMoves;
            possibleMoves = board.PossibleMoves();
            foreach (List <MoveMarble> move in possibleMoves)
            {
                //newBoard = board.GenerateNewBoardFromMove(move);
                newBoard = board.HashGenerateNewBoardFromMove(move);//cambiar

                // Recursividad
                scoringMove = Test(newBoard, (byte)(depth + 1), -gamma);

                int invertedScore = -scoringMove.score;

                // Actualizar mejor score
                if (invertedScore > bestScore)
                {
                    bestScore       = invertedScore;
                    bestMove        = move;//cambiar
                    record.bestMove = move;
                }

                if (bestScore < gamma)
                {
                    record.maxScore = bestScore;
                }
                else
                {
                    record.minScore = bestScore;
                }
            }
            transpositionTable.SaveRecord(record);
            scoringMove = new ScoringMove(bestScore, bestMove);
        }
        return(scoringMove);
    }