Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. DIM = 5
  2. POSITION = 'o'
  3. EMPTY = 'x'
  4. LEFT = 'l'
  5. RIGHT = 'r'
  6. UP = 'u'
  7. DOWN = 'd'
  8. QUIT = 'q'
  9.  
  10. def get_move():
  11.     ''' Returns a move corresponding to the user input direction '''
  12.     move = input('Move: ')
  13.    
  14.     if move not in [LEFT, RIGHT, UP, DOWN]:
  15.         return QUIT
  16.     else:
  17.         return move
  18.  
  19. def initialize_grid():
  20.     ''' Returns an initialized grid for the given dimension '''
  21.     grid = []
  22.  
  23.     for i in range(DIM):
  24.         sublist = []
  25.         for j in range(DIM):
  26.             sublist.append(EMPTY)
  27.         grid.append(sublist)
  28.  
  29.     grid[0][0] = POSITION  # Current position
  30.     return grid
  31.  
  32. def print_grid(grid):
  33.     for row in grid:
  34.         out_str = ''
  35.         for col in row:
  36.             out_str += str(col) + ' '
  37.         print(out_str.strip())
  38.  
  39. def make_move(grid, move, x, y):
  40.     # check what move we have
  41.     grid[x][y] = EMPTY
  42.     if move == LEFT:
  43.         if y == 0:
  44.             y = DIM - 1
  45.         else:
  46.             y -= 1
  47.     elif move == RIGHT:
  48.         if y == DIM - 1:
  49.             y = 0
  50.         else:
  51.             x -= 1
  52.     elif move == UP:
  53.         if x == 0:
  54.             x = DIM - 1
  55.         else:
  56.             x -= 1
  57.     elif move == DOWN:
  58.         if x == DIM - 1:
  59.             x = 0
  60.         else:
  61.             x += 1
  62.     grid[x][y] = POSITION
  63.     return grid, x, y
  64.  
  65. def if_on_edge():
  66.     pass
  67.  
  68. # Main program starts here
  69. # In your implementation, you have to use the functions and the constants given above
  70.  
  71. def main():
  72.     x = 0
  73.     y = 0
  74.     grid = initialize_grid()
  75.     move = ''
  76.     while move != QUIT:
  77.         print_grid(grid)    # print the grid
  78.         #print(grid) # todo make print function for grid
  79.         move = get_move()   # make the move
  80.         grid, x, y = make_move(grid, move, x, y)
  81.  
  82.  
  83. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement