Esempio n. 1
0
    // Analyses all the possible turns and returns the most positive one
    private TurnResponse DeepAnalysis()
    {
        BoardState          board         = InfoGiver.board;
        List <TurnResponse> possibleTurns = GetAllTurns();
        List <TurnResponse> bestTurns     = new List <TurnResponse>();

        float bestPositivity = 0;

        foreach (TurnResponse turn in possibleTurns)
        {
            BoardState newBoard   = InfoGiver.ApplyTurn(board, turn);
            float      positivity = LightAnalysis(newBoard.table);

            if (bestPositivity < positivity)
            {
                bestTurns = new List <TurnResponse>();
                bestTurns.Add(turn);
                bestPositivity = positivity;
            }
            else if (bestPositivity == positivity)
            {
                bestTurns.Add(turn);
            }
        }

        if (bestTurns.Count == 0)
        {
            return(null);
        }

        int randomIndex = Random.Range(0, bestTurns.Count);

        return(bestTurns[randomIndex]);
    }
Esempio n. 2
0
    // Does a depth-first traversal of all the possibilities (with a max depth) to get the best path
    private System.Tuple <TurnResponse, float> DeepAnalysis(BoardState board, Team team, int depth)
    {
        // If the game is won or lost, immediately return null and the positivity
        Team winner = InfoGiver.HasGameEnded(board.table);

        if (winner != Team.none)
        {
            return(new System.Tuple <TurnResponse, float>(null, winner == team ? 1 : 0));
        }

        List <TurnResponse> possibleTurns = GetAllTurns(board, team);

        // Makes the list of all the equivalent best turns, by traversing the possible turns and analysing their outcomes
        List <System.Tuple <TurnResponse, float> > bestTurns = new List <System.Tuple <TurnResponse, float> >();
        float bestPositivity = 0;

        foreach (TurnResponse turn in possibleTurns)
        {
            BoardState newBoard = InfoGiver.ApplyTurn(board, turn);

            float positivity = 0;
            if (depth <= 0)
            {
                positivity = LightAnalysis(newBoard.table, team);
            }
            else
            {
                System.Tuple <TurnResponse, float> recursiveResponse = DeepAnalysis(newBoard, team == Team.A ? Team.B : Team.A, depth - 1);
                positivity = 1 - recursiveResponse.Item2;
            }

            if (isOutputNeeded)
            {
                linesToWrite.Add("Depth: " + depth.ToString() + " - Team " + team.ToString()
                                 + " - " + turn.cardName + ": (" + turn.source.x + ", " + turn.source.y + ") -> (" + turn.destination.x + ", " + turn.destination.y + ") - Postivity: " + positivity.ToString());
                for (int i = 0; i < depth; i++)
                {
                    linesToWrite.Add("   ---");
                }
            }

            if (bestPositivity < positivity)
            {
                bestTurns = new List <System.Tuple <TurnResponse, float> >();
                bestTurns.Add(new System.Tuple <TurnResponse, float>(turn, positivity));
                bestPositivity = positivity;
            }
            else if (bestPositivity == positivity)
            {
                bestTurns.Add(new System.Tuple <TurnResponse, float>(turn, positivity));
            }
        }

        if (bestTurns.Count == 0)
        {
            return(null);
        }

        int randomIndex = Random.Range(0, bestTurns.Count);

        return(bestTurns[randomIndex]);
    }