Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using DigitalRuby.Threading;
  5.  
  6. public class AIPlayer : PlayerController
  7. {
  8.  
  9. State currentmove;
  10. private bool computing;
  11.  
  12. /////////////////////////////////
  13. ////// You should Implement these
  14. public enum TypeStrategy
  15. {
  16. MinMax,
  17. RandomStrategy,
  18. };
  19.  
  20. public enum EvaluatationFunc
  21. {
  22. Eval
  23. };
  24.  
  25. public enum UtilityFunc
  26. {
  27. Util
  28. };
  29. public TypeStrategy strategy;
  30. public EvaluatationFunc evalfunc;
  31. public UtilityFunc utilfunc;
  32.  
  33. private void InitAI()
  34. {
  35. MoveMaker myStrategy = null;
  36. EvaluationFunction eval = null;
  37. UtilityFunction ufunc = null;
  38.  
  39. ////////////////
  40. // your code here to initialize the MinMax algorithm
  41. // 1. Evaluation function
  42. // 2. Utility function
  43. // 3. Strategy (MinMax and MinMax Alpha Beta)
  44. ///////////////
  45. switch (evalfunc)
  46. {
  47. case EvaluatationFunc.Eval:
  48. eval = new EvaluationFunction();
  49. break;
  50. default:
  51. Debug.Log("Not an option");
  52. break;
  53. }
  54.  
  55. switch (utilfunc)
  56. {
  57. case UtilityFunc.Util:
  58. ufunc = new UtilityFunction();
  59. break;
  60. default:
  61. Debug.Log("Not an option");
  62. break;
  63. }
  64.  
  65. switch (strategy)
  66. {
  67. case TypeStrategy.RandomStrategy:
  68. myStrategy = new RandomSolution(this, GameManager.instance.GetAdversary(this));
  69. break;
  70. case TypeStrategy.MinMax:
  71. myStrategy = new MinMaxAlgorithm(this, eval, ufunc, GameManager.instance.GetAdversary(this));
  72. break;
  73. default:
  74. Debug.Log("Not an option");
  75. break;
  76. }
  77.  
  78. moveMaker = myStrategy;
  79. }
  80.  
  81. // Use this for initialization
  82. public override void Start()
  83. {
  84. base.Start();
  85. InitAI();
  86. }
  87.  
  88. public void ComputeTheMove(object result)
  89. {
  90. currentmove = (State)result;
  91. computing = false;
  92. base.updateboard = true;
  93. }
  94.  
  95. public override void TurnUpdate()
  96. {
  97. if (this.PlayersUnits.Count != 0 && !base.updateboard && !base.OnMovement && !computing)
  98. {
  99. computing = true;
  100. currentmove = null;
  101. EZThread.ExecuteInBackground(moveMaker.MakeMove, ComputeTheMove);
  102. Debug.Log("[AI] thinking.. computing:" + computing + " updateboard:" + base.updateboard);
  103. }
  104.  
  105. if (!computing && base.updateboard)
  106. {
  107. Debug.Log("[AI] performing move! computing:" + computing + " updateboard:" + base.updateboard);
  108. UpdateBoard(currentmove);
  109. }
  110. }
  111.  
  112. private void UpdateBoard(State state)
  113. {
  114. if (state.unitAttacked != null)
  115. {
  116. Attack(state.unitToPermormAction.GetAssociatedTile(), state.unitAttacked.GetAssociatedTile());
  117. base.AttackAnimation();
  118. }
  119. else
  120. {
  121. if (!base.OnMovement)
  122. {
  123. Unit unitToMove = state.unitToPermormAction;
  124. source = this.PlayerUnitsInfoDict[unitToMove.id].GetAssociatedTile();
  125. destination = unitToMove.GetAssociatedTile();
  126. }
  127. base.MoveAnimation();
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement