Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using DeepCopyExtensions;
  6.  
  7. public class MinMaxAlgorithm: MoveMaker
  8. {
  9. public EvaluationFunction evaluator;
  10. private UtilityFunction utilityfunc;
  11. public int depth = 0;
  12. private PlayerController MaxPlayer;
  13. private PlayerController MinPlayer;
  14. private int MaxDepthTerminal= 1000;
  15. private bool Win;
  16. private State bestMove=null;
  17.  
  18. public MinMaxAlgorithm(PlayerController MaxPlayer, EvaluationFunction eval, UtilityFunction utilf, PlayerController MinPlayer)
  19. {
  20. this.MaxPlayer = MaxPlayer;
  21. this.MinPlayer = MinPlayer;
  22. this.evaluator = eval;
  23. this.utilityfunc = utilf;
  24. }
  25.  
  26. public override State MakeMove()
  27. {
  28. // The move is decided by the selected state
  29. Debug.Log("Entrou\n");
  30. return GenerateNewState();
  31. }
  32.  
  33. private State GenerateNewState()
  34. {
  35. Debug.Log("Entrou2\n");
  36. // Creates initial state
  37. State initialState = new State(this.MaxPlayer, this.MinPlayer);
  38. // Call the MinMax implementation
  39. Debug.Log("Entrou3\n");
  40. State bestMove = MinMax(initialState);
  41. return bestMove;
  42. }
  43.  
  44.  
  45.  
  46.  
  47. //nos terminais-> funcao utilidade/recompensa
  48. //nos nao terminais-> funcao de avaliacao ->prever melhor jogada
  49. //10000
  50. public State MinMax(State currentState)
  51. {
  52. Debug.Log("Entrou4\n");
  53. float Value = ValMax(currentState);
  54. Debug.Log(Value);
  55. //Devolve movimento associado a esse valor máximo
  56. //State bestMove=null;
  57.  
  58. return this.bestMove;
  59. }
  60.  
  61. private float ValMin(State stateNode){
  62. Debug.Log("min\n");
  63. List<State> sucessors= GeneratePossibleStates(stateNode);
  64.  
  65. if(stateNode.depth==MaxDepthTerminal|| evaluator.evaluate(stateNode)==-1){
  66. return utilityfunc.evaluate(stateNode);
  67. }
  68.  
  69. float Value= Int32.MaxValue;
  70.  
  71. foreach(State suc in sucessors){
  72. Value= Math.Min(Value, ValMax(suc));
  73. }
  74. return Value;
  75. }
  76.  
  77. private float ValMax(State StateNode){
  78. Debug.Log("max\n");
  79. List<State> sucessors= GeneratePossibleStates(StateNode);
  80.  
  81. if(StateNode.depth==MaxDepthTerminal|| evaluator.evaluate(StateNode)==-1){
  82. return utilityfunc.evaluate(StateNode);
  83. }
  84. float Value= Int32.MinValue;
  85.  
  86. foreach(State suc in sucessors){
  87. Debug.Log(Value);
  88. // float result= evaluator.evaluate(suc);
  89. Value = Math.Max(Value, ValMin(suc));
  90. }
  91.  
  92. return Value;
  93. }
  94.  
  95.  
  96. private List<State> GeneratePossibleStates(State state)
  97. {
  98. List<State> states = new List<State>();
  99. //Generate the possible states available to expand
  100. foreach(Unit currentUnit in state.PlayersUnits)
  101. {
  102. // Movement States
  103. List<Tile> neighbours = currentUnit.GetFreeNeighbours(state);
  104. foreach (Tile t in neighbours)
  105. {
  106. State newState = new State(state, currentUnit, true);
  107. newState = MoveUnit(newState, t);
  108. states.Add(newState);
  109. }
  110. // Attack states
  111. List<Unit> attackOptions = currentUnit.GetAttackable(state, state.AdversaryUnits);
  112. foreach (Unit t in attackOptions)
  113. {
  114. State newState = new State(state, currentUnit, false);
  115. newState = AttackUnit(newState, t);
  116. states.Add(newState);
  117. }
  118.  
  119. }
  120.  
  121. // YOU SHOULD NOT REMOVE THIS
  122. // Counts the number of expanded nodes;
  123. this.MaxPlayer.ExpandedNodes += states.Count;
  124. //
  125.  
  126. return states;
  127. }
  128.  
  129. private State MoveUnit(State state, Tile destination)
  130. {
  131. Unit currentUnit = state.unitToPermormAction;
  132. //First: Update Board
  133. state.board[(int)destination.gridPosition.x, (int)destination.gridPosition.y] = currentUnit;
  134. state.board[currentUnit.x, currentUnit.y] = null;
  135. //Second: Update Players Unit Position
  136. currentUnit.x = (int)destination.gridPosition.x;
  137. currentUnit.y = (int)destination.gridPosition.y;
  138. state.isMove = true;
  139. state.isAttack = false;
  140. return state;
  141. }
  142.  
  143. private State AttackUnit(State state, Unit toAttack)
  144. {
  145. Unit currentUnit = state.unitToPermormAction;
  146. Unit attacked = toAttack.DeepCopyByExpressionTree();
  147.  
  148. Tuple<float, float> currentUnitBonus = currentUnit.GetBonus(state.board, state.PlayersUnits);
  149. Tuple<float, float> attackedUnitBonus = attacked.GetBonus(state.board, state.AdversaryUnits);
  150.  
  151.  
  152. attacked.hp += Math.Min(0, (attackedUnitBonus.Item1)) - (currentUnitBonus.Item2 + currentUnit.attack);
  153. state.unitAttacked = attacked;
  154.  
  155. if (attacked.hp <= 0)
  156. {
  157. //Board update by killing the unit!
  158. state.board[attacked.x, attacked.y] = null;
  159. int index = state.AdversaryUnits.IndexOf(attacked);
  160. state.AdversaryUnits.RemoveAt(index);
  161.  
  162. }
  163. state.isMove = false;
  164. state.isAttack = true;
  165.  
  166. return state;
  167.  
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement