Guest User

Untitled

a guest
Feb 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.67 KB | None | 0 0
  1. #-NOTE: every cell is just a list.  When empty, that cell-list is [None] (not []).  This way we can pass a reference to a cell around and change the cell-list without having to remember co-ordinates, or have a dedicated cell datastructure.
  2.  
  3. def _fix_overfull_cell(self, cell_list):
  4.     #-TODO: Docstrings?  How are they supposed to be written?
  5.     # takes a reference to a cell's contents list -- so we can change it in this function and grid's referenced version will be changed too
  6.  
  7.     if length(cell_list) > 1:
  8.         #make a list that contains all bots in cell that moved there this turn
  9.         bots_to_move_back = filter(lambda bot:(bot.has_moved == 1), cell) #-TODO:will throw a AttributeError if there's food/wall in this cell.  Might want to raise our own error, and catch the AttributeError if only to have informative crash messages.
  10.  
  11.         for bot in bots_to_move_back:
  12.             self.grid.move(bot, bot.last_position)
  13.             bot.has_moved = -1
  14.             #-TODO replace with setter, raise actionfailed error with bot when set to -1
  15.  
  16.         for bot in bots_to_move_back:
  17.             self._fix_overfull_cell(bot.position) #check the cell into which we just moved them
  18.  
  19. def _is_stationary(self, cell_element):
  20.     if cell_element == 'WALL':
  21.         return True
  22.     elif cell_element == None:
  23.         return False
  24.     elif cell_element.__class__ == #-TODO fill in ref to Food class
  25.         return True
  26.     elif cell_element.__class__ == #-TODO fill in ref to Bot class
  27.         return (not cell_element.get_action_type() == 'move') or (cell_element.has_moved == -1)
  28.         #other team bots will count as stationary; their action is None
  29.         #truth table -- bots that have been unmoved are stationary (has_moved == -1)
  30.         #has_moved == -1 | action == 'move' | not action == 'move' | desired return value
  31.         #    1           |        1         |           0          |     1
  32.         #    0           |        1         |           0          |     0
  33.         #    0           |        0         |           1          |     1
  34.  
  35.     else:
  36.         assert False, "World._is_stationary doesn't recognize cell_element: ", cell_element
  37.  
  38.  
  39. #-TODO make this a public method of grid
  40. def grid.move(self, bot, destination_cell_list):
  41.     bot.last_position = bot.position
  42.     bot.position = destination_cell_list
  43.  
  44.     self.add_to_cell(destination_cell_list, bot) #-TODO implement this, remove None when filling empty cell.
  45.     self.remove_from_cell(bot.last_position, bot) #-TODO implement this, add None to empty cells
  46.  
  47. # world logic
  48. # Apply and check action loop
  49. for bot in self._current_team:
  50.     #-TODO we should do this in start-turn or end-turn cleanup.  Just putting it here so we don't forget.
  51.     bot.has_moved = None
  52.  
  53.     if bot.get_action_type() == 'move':
  54.         dest_cell_list = bot.get_action_params()
  55.         if filter(self._is_stationary(), dest_cell_list).length > 0: #if any objects in dest didn't plan to move this turn
  56.         #-NOTE: Moving into same cell as a bot that has moved or is going to move MUST be allowed, because that bot might have to move forward or backwards.  And we don't want the first bot we iterate over to get precedence.  We want to disallow either one from moving.
  57.             bot.has_moved = -1
  58.         else:
  59.             self.grid.move(bot, dest_cell_list)
  60.             bot.has_moved = 1
  61.     else:
  62.         #-TODO: check non-move actions for legality
  63.         #-TODO: apply non-move actions; none of the others actions are affected by position of teammate bots
  64.  
  65. # Undo illegal moves
  66. for cell in self.grid:
  67.     self._fix_overfull_cell(cell)
  68.  
  69. #-TODO: Cleanup loop.  Set lots of bot properties to None.  action, has_moved, last_position, any others?
  70.  
  71. #-TODO: new public methods/fields:
  72. #BOT
  73.     #bot.position -- is a reference to bot's current cell-list
  74.     #bot.last_position -- basically, should just be set to None.  We only use it in the apply/check action loop and the undo illegal move loop
  75.     #bot.has_moved --   None if action isn't move
  76.     #                   -1 if bot has been moved back or had move fail
  77.     #                   0 if bot hasn't tried to move yet, but intends to
  78.     #                   1 if bot has moved, and hasn't been moved back yet
  79.     #mechanism for sending failed action notifications
  80.     # Should maybe replace with getter and setter methods.
  81. #WORLD
  82.     #world.kill_bot(bot), removes bot from player botlist and grid (using grid.remove_from_cell(cellentity))
  83. #GRID
  84.     #grid.add_to_cell(cellentity)
  85.     #grid.remove_from_cell(cellentity)
  86.     #make grid an iterable object, returning each cell in no particular order.
  87.     #make each cell a list of its contents.  If empty, it should be [None] not []
Add Comment
Please, Sign In to add comment