// Analyses a given situation and returns its "positivity" // Game lost -> 0 // Game won -> 1 // Other cases -> Proportion of allied pieces in all the pieces in game private float LightAnalysis(PieceState[][] table) { Team winner = InfoGiver.HasGameEnded(table); if (winner == team) { return(1); } if (winner == Team.none) { int allies = 0; int total = 0; // Counting the allied pieces and all the pieces for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (table[i][j] != null) { total += 1; if (table[i][j].team == team) { allies += 1; } } } } return((float)allies / (float)total); } return(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]); }