Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. import random
  2. from tronclient.Client import *
  3. from Enums import PlayerActions
  4.  
  5. class PlayerAI():
  6. def __init__(self):
  7. return
  8.  
  9. def new_game(self, game_map, player_lightcycle, opponent_lightcycle):
  10. return
  11.  
  12. def get_move(self, game_map, player_lightcycle, opponent_lightcycle, moveNumber):
  13. my_position = player_lightcycle['position']
  14. my_x = my_position[0]
  15. my_y = my_position[1]
  16. my_direction = player_lightcycle['direction']
  17. randMove = self.next_move(my_x, my_y, game_map, my_direction)
  18. if randMove == 3:
  19. return PlayerActions.MOVE_LEFT
  20. elif randMove == 1:
  21. return PlayerActions.MOVE_RIGHT
  22. elif randMove == 2:
  23. return PlayerActions.MOVE_DOWN
  24. else:
  25. return PlayerActions.MOVE_UP
  26.  
  27. def next_move(self, x, y, game_map, direction):
  28. #checks current direction
  29. print direction
  30. if direction == 2:
  31. down = self.check_down(x, y, game_map)
  32. if down and self.peek(x, y, game_map, 2):
  33. return 2
  34. else:
  35. return self.find_next_step(x, y, game_map, direction)
  36. elif direction == 3:
  37. left = self.check_left(x, y, game_map)
  38. if left and self.peek(x, y, game_map, 3):
  39. return 3
  40. else:
  41. return self.find_next_step(x, y, game_map, direction)
  42. elif direction == 1:
  43. right = self.check_right(x, y, game_map)
  44. if right and self.peek(x, y, game_map, 1):
  45. return 1
  46. else:
  47. return self.find_next_step(x, y, game_map, direction)
  48. else:
  49. up = self.check_up(x,y,game_map)
  50. if up and self.peek(x, y, game_map, 0):
  51. return
  52. else:
  53. return self.find_next_step(x, y, game_map, direction)
  54.  
  55. def find_next_step(self, x, y, game_board, direction):
  56. if direction is 3:
  57. down = self.check_down(x,y, game_board)
  58. up = self.check_up(x, y, game_board)
  59. if self.go_up(y, game_board):
  60. if up and self.peek(x, y, game_board, 0):
  61. return 0
  62. else:
  63. return 2
  64. else:
  65. if down:
  66. return 2
  67. else:
  68. return 0
  69.  
  70. elif direction is 1:
  71. down = self.check_down(x,y, game_board)
  72. up = self.check_up(x, y, game_board)
  73. if self.go_up(y, game_board):
  74. if up:
  75. return 0
  76. else:
  77. return 2
  78. else:
  79. if down:
  80. return 2
  81. else:
  82. return 0
  83.  
  84. elif direction is 2:
  85. right = self.check_right(x, y, game_board)
  86. left = self.check_left(x, y, game_board)
  87. if self.go_left(x, game_board):
  88. if left:
  89. return 3
  90. else:
  91. return 1
  92. else:
  93. if right:
  94. return 1
  95. else:
  96. return 3
  97.  
  98. else:
  99. right = self.check_right(x,y, game_board)
  100. left = self.check_left(x, y, game_board)
  101. if self.go_left(x, game_board):
  102. if left:
  103. return 3
  104. else:
  105. return 1
  106. else:
  107. if right:
  108. return 1
  109. else:
  110. return 3
  111.  
  112.  
  113. def go_up(self, y, game_board):
  114. result = y*(1.0)/(len(game_board[0]) - 1)
  115. print result
  116. if result > 0.5:
  117. return True
  118. else:
  119. return False
  120.  
  121. def go_left(self, x, game_board):
  122. result = (x*1.0)/(len(game_board[0]) - 1)
  123. if result > 0.5:
  124. return True
  125. else:
  126. return False
  127.  
  128. def check_left(self, x, y, game_board):
  129. if x <= 0:
  130. return False
  131. elif game_board[x-1][y] != EMPTY and game_board[x-1][y] != POWERUP:
  132. return False
  133. else:
  134. return True
  135.  
  136. def check_right(self, x, y, game_board):
  137. if x >= (len(game_board) - 1):
  138. return False
  139. elif game_board[x+1][y] != EMPTY and game_board[x+1][y] != POWERUP:
  140. return False
  141. else:
  142. return True
  143.  
  144. def check_down(self, x, y, game_board):
  145. if y>=(len(game_board[0])-1):
  146. return False
  147. elif game_board[x][y+1] != EMPTY and game_board[x][y+1] != POWERUP:
  148. return False
  149. else:
  150. return True
  151.  
  152. def check_up(self, x, y, game_board):
  153. if y <= 0:
  154. return False
  155. elif game_board[x][y-1] != EMPTY and game_board[x][y-1] != POWERUP:
  156. return False
  157. else:
  158. return True
  159.  
  160. def peek(self, x, y, game_board, direction):
  161. if direction == 0:
  162. return self.check_up(x, y-1, game_board)
  163. elif direction == 1:
  164. return self.check_right(x+1, y, game_board)
  165. elif direction == 2:
  166. return self.check_down(x, y+1, game_board)
  167. else:
  168. return self.check_left(x-1, y, game_board)
  169.  
  170.  
  171. '''
  172.  
  173. 8888888 8888888888 8 888888888o. ,o888888o. b. 8
  174. 8 8888 8 8888 `88. . 8888 `88. 888o. 8
  175. 8 8888 8 8888 `88 ,8 8888 `8b Y88888o. 8
  176. 8 8888 8 8888 ,88 88 8888 `8b .`Y888888o. 8
  177. 8 8888 8 8888. ,88' 88 8888 88 8o. `Y888888o. 8
  178. 8 8888 8 888888888P' 88 8888 88 8`Y8o. `Y88888o8
  179. 8 8888 8 8888`8b 88 8888 ,8P 8 `Y8o. `Y8888
  180. 8 8888 8 8888 `8b. `8 8888 ,8P 8 `Y8o. `Y8
  181. 8 8888 8 8888 `8b. ` 8888 ,88' 8 `Y8o.`
  182. 8 8888 8 8888 `88. `8888888P' 8 `Yo
  183.  
  184. Quick Guide
  185. --------------------------------------------
  186. Feel free to delete this comment.
  187.  
  188. 1. THIS IS THE ONLY .PY OR .BAT FILE YOU SHOULD EDIT THAT CAME FROM THE ZIPPED STARTER KIT
  189.  
  190. 2. Any external files should be accessible from this directory
  191.  
  192. 3. new_game is called once at the start of the game if you wish to initialize any values
  193.  
  194. 4. get_move is called for each turn the game goes on
  195.  
  196. 5. game_map is a 2-d array that contains values for every board position.
  197. example call: game_map[2][3] == POWERUP would evaluate to True if there was a powerup at (2, 3)
  198.  
  199. 6. player_lightcycle is your lightcycle and is what the turn you respond with will be applied to.
  200. It is a dictionary with corresponding keys: "position", "direction", "hasPowerup", "isInvincible", "powerupType"
  201. position is a 2-element int array representing the x, y value
  202. direction is the direction you are travelling in. can be compared with Direction.DIR where DIR is one of UP, RIGHT, DOWN, or LEFT
  203. hasPowerup is a boolean representing whether or not you have a powerup
  204. isInvincible is a boolean representing whether or not you are invincible
  205. powerupType is what, if any, powerup you have
  206.  
  207. 7. opponent_lightcycle is your opponent's lightcycle. Same keys and values as player_lightcycle
  208.  
  209. 8. You ultimately are required to return one of the following:
  210. PlayerAction.SAME_DIRECTION
  211. PlayerAction.MOVE_UP
  212. PlayerAction.MOVE_DOWN
  213. PlayerAction.MOVE_LEFT
  214. PlayerAction.MOVE_RIGHT
  215. PlayerAction.ACTIVATE_POWERUP
  216. PlayerAction.ACTIVATE_POWERUP_MOVE_UP
  217. PlayerAction.ACTIVATE_POWERUP_MOVE_DOWN
  218. PlayerAction.ACTIVATE_POWERUP_MOVE_LEFT
  219. PlayerAction.ACTIVATE_POWERUP_MOVE_RIGHT
  220.  
  221. 9. If you have any questions, contact challenge@orbis.com
  222.  
  223. 10. Good luck! Submissions are due Sunday, September 21 at noon. You can submit multiple times and your most recent submission will be the one graded.
  224.  
  225. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement