Advertisement
NickIsNotTaken

geek_class_tanks

Jul 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. import random
  2.  
  3. def make_choice(x,y,field):
  4. my_life = field[x][y]['life']
  5. act = ''
  6. #width = len(field)
  7. #height = len(field[0])
  8. coin_up = False
  9. coin_down = False
  10. coin_left = False
  11. coin_right = False
  12. coin = False
  13. tank = False
  14. tank_up = False
  15. tank_down = False
  16. tank_left = False
  17. tank_right = False
  18.  
  19. tank_life_en = {}
  20. actions = ['go_left', 'go_right', 'go_down', 'go_up']
  21. back_up = actions[:]
  22. print(back_up)
  23.  
  24. if x - 1 != 0:
  25. for i in range(x-1, 0, -1):
  26.  
  27. if field[i][y] == 1:
  28. coin_left = True
  29.  
  30. if field[i][y] not in [-1, 0, 1]:
  31. tank_left = True
  32. tank_life_en = field[i][y]['life']
  33. break
  34. if field[i][y] == -1:
  35. break
  36.  
  37. if x + 1 != len(field):
  38. for i in range(x+1, len(field)):
  39. if field[i][y] == 1:
  40. coin_right = True
  41.  
  42. if field[i][y] not in [-1, 0, 1]:
  43. tank_right = True
  44. tank_life_en = field[i][y]['life']
  45. break
  46. if field[i][y] == -1:
  47. break
  48.  
  49. if y - 1 != 0:
  50. for i in range(y-1, 0, -1):
  51.  
  52. if field[x][i] == 1:
  53. coin_up = True
  54.  
  55. if field[x][i] not in [-1, 0, 1]:
  56. tank_up = True
  57. tank_life_en = field[x][i]['life']
  58. break
  59. if field[x][i] == -1:
  60. break
  61.  
  62. if y + 1 != len(field[x]):
  63. for i in range(y+1, len(field[x])):
  64.  
  65. if field[x][i] == 1:
  66. coin_down = True
  67.  
  68. if field[x][i] not in [-1, 0, 1]:
  69. tank_down = True
  70. tank_life_en = field[x][i]['life']
  71. break
  72. if field[x][i] == -1:
  73. break
  74.  
  75.  
  76.  
  77. if coin_up == True or coin_down == True or coin_left == True or coin_right == True:
  78. coin = True
  79. if coin_up == True:
  80. coin = True
  81. act = 'go_up'
  82. elif coin_down == True:
  83. coin = True
  84. act = 'go_down'
  85. elif coin_left == True:
  86. coin = True
  87. act = 'go_left'
  88. elif coin_right == True:
  89. coin = True
  90. act = 'go_right'
  91.  
  92. if tank_up == True or tank_down == True or tank_left == True or tank_right == True:
  93. tank = True
  94. if tank_up == True:
  95. if tank_life_en > my_life + 1:
  96. if field[x+1][y] == 0:
  97. act = 'go_right'
  98. elif field[x-1][y] == 0:
  99. act == 'go_left'
  100. elif field[x][y]['history'][-1] == 'fire_up' and field[x+1][y] == 0 or field[x+1][y] == 1 and field[x-1][y] == 0 or field[x-1][y] == 1:
  101. act = random.choice('go_right', 'go_left')
  102. elif field[x][y]['history'][-1] == 'fire_up' and field[x-1][y] == 0 or field[x-1][y] == 1 and field[x+1][y] == 0 or field[x+1][y] == 1:
  103. act = random.choice('go_right', 'go_left')
  104. elif field[x][y]['history'][-1] == 'fire_up' and field[x+1][y] == 0 or field[x+1][y] == 1:
  105. act = 'go_right'
  106. elif field[x][y]['history'][-1] == 'fire_up' and field[x-1][y] == 0 or field[x-1][y] == 1:
  107. act ='go left'
  108. else:
  109. act = random.choice('fire_up', 'go_up', 'go_down')
  110. elif tank_down == True:
  111. if tank_life_en > my_life + 1:
  112. if field[x+1][y] == 0:
  113. act = 'go_right'
  114. elif field[x-1][y] == 0:
  115. act == 'go_left'
  116. elif field[x][y]['history'][-1] == 'fire_down' and field[x+1][y] == 0 or field[x+1][y] == 1 and field[x-1][y] == 0 or field[x-1][y] == 1:
  117. act = random.choice('go_right', 'go_left')
  118. elif field[x][y]['history'][-1] == 'fire_down' and field[x-1][y] == 0 or field[x-1][y] == 1 and field[x+1][y] == 0 or field[x+1][y] == 1:
  119. act = random.choice('go_right', 'go_left')
  120. elif field[x][y]['history'][-1] == 'fire_down' and field[x+1][y] == 0 or field[x+1][y] == 1:
  121. act = 'go_right'
  122. elif field[x][y]['history'][-1] == 'fire_down' and field[x-1][y] == 0 or field[x-1][y] == 1:
  123. act ='go left'
  124. else:
  125. act = random.choice('fire_down', 'go_up', 'go_down')
  126. elif tank_left == True:
  127. if tank_life_en > my_life + 1:
  128. if field[x][y-1] == 0:
  129. act = 'go_up'
  130. elif field[x][y+1] == 0:
  131. act = 'go_down'
  132. elif field[x][y]['history'][-1] == 'fire_left' and field[x][y-1] == 0 or field[x][y-1] == 1:
  133. act = 'go_up'
  134. elif field[x][y]['history'][-1] == 'fire_left' and field[x][y+1] == 0 or field[x][y+1] == 1:
  135. act = 'go_down'
  136. else:
  137. act = 'fire_left'
  138. elif tank_right == True:
  139. if tank_life_en > my_life + 1:
  140. if field[x][y-1] == 0:
  141. act = 'go_up'
  142. elif field[x][y+1] == 0:
  143. act = 'go_down'
  144. elif field[x][y]['history'][-1] == 'fire_right' and field[x][y+1] == 0 or field[x][y+1] == 1:
  145. act = 'go_down'
  146. elif field[x][y]['history'][-1] == 'fire_right' and field[x][y-1] == 0 or field[x][y-1] == 1:
  147. act = 'go_up'
  148. else:
  149. act = 'fire_right'
  150.  
  151.  
  152.  
  153. if x == len(field):
  154. actions.remove('go_right')
  155. if x == 0:
  156. actions.remove('go_left')
  157. if y == 0:
  158. actions.remove('go_up')
  159. if y == len(field[x]):
  160. actions.remove('go_down')
  161.  
  162. if tank == True:
  163. return act
  164. elif coin == True:
  165. return act
  166. elif actions == []:
  167. actions = back_up[:]
  168. return random.choice(actions)
  169. else:
  170. return random.choice(actions)
  171.  
  172.  
  173. '''
  174. if __name__ == "__main__":
  175. T1 = {"life": 10, 'history': ['fire_down']}
  176. T2 = {'life': 10}
  177. T = {'life': 20}
  178. my_field = [
  179. [T1,T2,0,0],
  180. [0,T,0,1],
  181. [0,0,T,0],
  182. [0,0,0,0]
  183. ]
  184. my_x = 0
  185. my_y = 0
  186. res = make_choice(my_x, my_y, my_field)
  187. print(res)
  188.  
  189. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement