Advertisement
Guest User

Untitled

a guest
May 24th, 2021
15
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from typing import List, Tuple
  2. from random import randint
  3.  
  4. class Robot:
  5. def __init__(self, name: str, place : List[int], start: Tuple[int, int] = (0,0), power: int = 100):
  6. self._name = name
  7. self._place = place
  8. self._start = start
  9. self._power = power
  10.  
  11. @property
  12. def name(self):
  13. return self._name
  14.  
  15. @name.setter
  16. def name(self, value):
  17. if isinstance(value, str):
  18. self._name = value
  19. else:
  20. raise TypeError("must be a string")
  21. @property
  22. def place(self):
  23. return self._place
  24.  
  25. @place.setter
  26. def place(self, value):
  27. if isinstance(value, list):
  28. self._start = value
  29. else:
  30. raise TypeError("must be a list")
  31.  
  32. @property
  33. def start(self):
  34. return self._start
  35.  
  36. @start.setter
  37. def start(self, value):
  38. if isinstance(value, tuple):
  39. self._start = value
  40. else:
  41. raise TypeError("must be a tuple")
  42.  
  43. @property
  44. def power(self):
  45. return self._power
  46.  
  47. @power.setter
  48. def power(self, value):
  49. if isinstance(value, int):
  50. self._start = value
  51. else:
  52. raise TypeError("must be a int")
  53.  
  54. @property
  55. def check_power(self):
  56. if self._power <= 0:
  57. raise ValueError("No power")
  58.  
  59. def left(self, value):
  60. # self.sprawdz_power()
  61. self.power -= value
  62.  
  63. if self.place[0] - value < 0:
  64. self.place[0] = self.place[0] - value + 8
  65.  
  66. else:
  67. self.place[0] = self.place[0] - value
  68.  
  69. def up(self, value):
  70. # self.sprawdz_power()
  71. self.power -= value
  72.  
  73. if self.place[1] + value > 7:
  74. self.place[1] = self.place[1] + value - 8
  75.  
  76. else:
  77. self.place[1] = self.place[1] + value
  78.  
  79. if self.place[1] == 5:
  80. self.power += 2
  81.  
  82. def __str__(self):
  83. return "{} {} {}".format(self._name, self._place, self._power)
  84.  
  85. from itertools import combinations
  86.  
  87. def game_round(robots):
  88. losers = set()
  89. for a, b in combinations(robots, 2):
  90. if a.place == b.place:
  91. loser, winner = sorted([a, b], key=lambda robot: robot.power)
  92. winner.power += loser.power
  93. losers.add(loser)
  94. print(f'Robot {loser.name} has been slain by {winner.name}')
  95.  
  96. return [robot for robot in robots if robot not in losers]
  97.  
  98.  
  99.  
  100. def game(number):
  101. for _ in range(number):
  102.  
  103. if len(robots) == 1:
  104. print(robots[0].name, "is victorious")
  105. break
  106.  
  107. to_delete = []
  108. for r_nr in range(len(robots)):
  109. strona_rand = randint(0,1)
  110. ruch_value = randint(0,4)
  111.  
  112.  
  113. if strona_rand == 0:
  114. try:
  115. robots[r_nr].up(ruch_value)
  116. except:
  117. to_delete.append(r_nr)
  118.  
  119. elif strona_rand == 1:
  120. try:
  121. robots[r_nr].left(ruch_value)
  122. except:
  123. to_delete.append(r_nr)
  124.  
  125. for r_nr in sorted(to_delete, reverse=True):
  126. print(f'Robot {robots[r_nr].name} lost all the power')
  127. del robots[r_nr]
  128.  
  129.  
  130. robots = [
  131. Robot(name = "A", place = [3,4], start = (0,0), power = 100),
  132. Robot(name = "B", place = [1,2], start = (0,0), power = 100),
  133. Robot(name = "C", place = [1,2], start = (0,0), power = 100),
  134. Robot(name = "D", place = [1,2], start = (0,0), power = 100),
  135. Robot(name = "E", place = [1,2], start = (0,0), power = 100),
  136. ]
  137.  
  138.  
  139. robots = game_round(robots)
  140. game(100)
  141.  
  142.  
  143. Robot B has been slain by C
  144. Robot B has been slain by D
  145. Robot B has been slain by E
  146. Robot C has been slain by D
  147. Robot C has been slain by E
  148. Robot D has been slain by E
  149.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement