VikkaLorel

switch example with partial

Oct 29th, 2020
2,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. from functools import partial
  2.  
  3.  
  4. def update_pos(self, room, exit_name):
  5.     # this ensures the pointer variables always
  6.     # stays up to date to where the worm is currently at.
  7.     self.curX, self.curY = self.worm_has_mapped[room][0], self.worm_has_mapped[room][1]
  8.  
  9.     # now we have to actually move the pointer
  10.     # variables depending on which 'exit' it found
  11.  
  12.     def _update_pos(x=0, y=0):
  13.         return self.curX + x, self.curY + y
  14.  
  15.     # define the different cases
  16.     switch = {
  17.         'east': partial(_update_pos, y=1),
  18.         'west': partial(_update_pos, y=-1),
  19.         'north': partial(_update_pos, x=-1),
  20.         'south': partial(_update_pos, x=1),
  21.         'northwest': partial(_update_pos, x=-1, y=-1),
  22.         'northeast': partial(_update_pos, x=-1, y=1),
  23.         'southwest': partial(_update_pos, x=1, y=-1),
  24.         'southeast': partial(_update_pos, x=1, y=1),
  25.     }
  26.  
  27.     update_pos_func = switch.get(exit_name)  # get the right case
  28.     self.curX, self.curY = update_pos_func() if update_pos_func else self.curX, self.curY  # update position
  29.  
Advertisement
Add Comment
Please, Sign In to add comment