Guest User

outcome.py

a guest
Mar 10th, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1.  
  2.  
  3. class Outcome:
  4. def __init__(self, probability, health_array):
  5. self.probability = probability
  6. self.health_array = health_array
  7.  
  8. def get_outcomes(self, in_array, move):
  9. outcomes = []
  10.  
  11. if self.health_array[move] == 0:
  12. outcomes.append(self)
  13. return outcomes
  14.  
  15. attacker = in_array[move]
  16.  
  17. enemy_count = 0
  18. for i in range(len(in_array)):
  19. defender = in_array[i]
  20. if attacker.teamid != defender.teamid:
  21. enemy_count += 1
  22.  
  23. for i in range(len(in_array)):
  24. defender = in_array[i]
  25.  
  26. if i == move or attacker.teamid == defender.teamid:
  27. continue
  28.  
  29. if self.health_array[i] == 0:
  30. win_probability = self.probability * attacker.attack(defender) * (1 / enemy_count)
  31. win_health_array = self.health_array.copy()
  32. outcomes.append(Outcome(win_probability, win_health_array))
  33. continue
  34.  
  35. win_probability = self.probability * attacker.attack(defender) * (1 / enemy_count)
  36. win_health_array = self.health_array.copy()
  37. win_health_array[i] -= 1
  38. outcomes.append(Outcome(win_probability, win_health_array))
  39.  
  40. loose_probability = self.probability * (1 - attacker.attack(defender)) * (1 / enemy_count)
  41. loose_health_array = self.health_array.copy()
  42. outcomes.append(Outcome(loose_probability, loose_health_array))
  43.  
  44. return outcomes
  45.  
  46.  
  47. def play_round(in_array):
  48. probs = [Outcome(1, [3 for _ in range(len(in_array))])]
  49. probs_buff = []
  50.  
  51. for i in range(len(in_array)):
  52. for j in range(len(probs)):
  53.  
  54. buff = probs[j].get_outcomes(in_array, i)
  55. probs_buff.extend(buff)
  56.  
  57. probs = probs_buff.copy()
  58. probs_buff.clear()
  59.  
  60. return probs
  61.  
  62.  
  63. if __name__ == "__main__":
  64. # test code
  65.  
  66. from fighter import Fighter
  67.  
  68. initiative = [
  69. Fighter("Тсия", 0, 8, 8),
  70. Fighter("Бедный фермер", 1, 0, 0)
  71. ]
  72.  
  73. battle_outcomes = play_round(initiative)
  74.  
  75. for i in range(len(initiative)):
  76. health_probs = [0, 0, 0, 0]
  77.  
  78. for j in range(len(battle_outcomes)):
  79. health_probs[battle_outcomes[j].health_array[i]] += battle_outcomes[j].probability
  80.  
  81. print(initiative[i].name)
  82. for j in range(4):
  83. print(j, "hp", round(health_probs[j] * 100, 6), "%")
  84.  
  85.  
Add Comment
Please, Sign In to add comment