Advertisement
Guest User

Untitled

a guest
May 24th, 2021
12
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. def check_position():
  86.  
  87. to_delete = []
  88.  
  89. for r_a_nr in range(len(robots)):
  90. for r_b_nr in range(r_a_nr + 1, len(robots)):
  91.  
  92. if robots[r_a_nr].place == robots[r_b_nr].place:
  93.  
  94. if robots[r_a_nr].power >= robots[r_b_nr].power:
  95. robots[r_a_nr].power = robots[r_a_nr].power + robots[r_b_nr].power
  96. to_delete.append(r_b_nr)
  97. print(f'Robot {robots[r_b_nr].name} has been slain by {robots[r_a_nr].name}')
  98.  
  99. elif robots[r_a_nr].power < robots[r_b_nr].power:
  100. robots[r_b_nr].power = robots[r_b_nr].power + robots[r_a_nr].power
  101. to_delete.append(r_a_nr)
  102. print(f'Robot {robots[r_a_nr].name} has been slain by {robots[r_b_nr].name}')
  103.  
  104. for i in sorted(list(set(to_delete)), reverse=True):
  105. del robots[i]
  106.  
  107.  
  108. def game(number):
  109. for _ in range(number):
  110.  
  111. if len(robots) == 1:
  112. print(robots[0].name, "is victorious")
  113. break
  114.  
  115. to_delete = []
  116. for r_nr in range(len(robots)):
  117. strona_rand = randint(0,1)
  118. ruch_value = randint(0,4)
  119.  
  120.  
  121. if strona_rand == 0:
  122. try:
  123. robots[r_nr].up(ruch_value)
  124. except:
  125. to_delete.append(r_nr)
  126.  
  127. elif strona_rand == 1:
  128. try:
  129. robots[r_nr].left(ruch_value)
  130. except:
  131. to_delete.append(r_nr)
  132.  
  133. for r_nr in sorted(to_delete, reverse=True):
  134. print(f'Robot {robots[r_nr].name} lost all the power')
  135. del robots[r_nr]
  136.  
  137. check_position()
  138.  
  139. robots = [
  140. Robot(name = "A", place = [3,4], start = (0,0), power = 100),
  141. Robot(name = "B", place = [1,2], start = (0,0), power = 100),
  142. Robot(name = "C", place = [1,2], start = (0,0), power = 100),
  143. Robot(name = "D", place = [1,2], start = (0,0), power = 100),
  144. Robot(name = "E", place = [1,2], start = (0,0), power = 100),
  145. ]
  146.  
  147. game(100)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement