Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DIM = 5
- POSITION = 'o'
- EMPTY = 'x'
- LEFT = 'l'
- RIGHT = 'r'
- UP = 'u'
- DOWN = 'd'
- QUIT = 'q'
- def get_move():
- ''' Returns a move corresponding to the user input direction '''
- move = input('Move: ')
- if move not in [LEFT, RIGHT, UP, DOWN]:
- return QUIT
- else:
- return move
- def initialize_grid():
- ''' Returns an initialized grid for the given dimension '''
- grid = []
- for i in range(DIM):
- sublist = []
- for j in range(DIM):
- sublist.append(EMPTY)
- grid.append(sublist)
- grid[0][0] = POSITION # Current position
- return grid
- def print_grid(grid):
- for row in grid:
- out_str = ''
- for col in row:
- out_str += str(col) + ' '
- print(out_str.strip())
- def make_move(grid, move, x, y):
- # check what move we have
- grid[x][y] = EMPTY
- if move == LEFT:
- if y == 0:
- y = DIM - 1
- else:
- y -= 1
- elif move == RIGHT:
- if y == DIM - 1:
- y = 0
- else:
- x -= 1
- elif move == UP:
- if x == 0:
- x = DIM - 1
- else:
- x -= 1
- elif move == DOWN:
- if x == DIM - 1:
- x = 0
- else:
- x += 1
- grid[x][y] = POSITION
- return grid, x, y
- def if_on_edge():
- pass
- # Main program starts here
- # In your implementation, you have to use the functions and the constants given above
- def main():
- x = 0
- y = 0
- grid = initialize_grid()
- move = ''
- while move != QUIT:
- print_grid(grid) # print the grid
- #print(grid) # todo make print function for grid
- move = get_move() # make the move
- grid, x, y = make_move(grid, move, x, y)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement