HW4: Move the Worker

  1. Use four keys 'H', 'K', 'J', 'L' to move the worker to the left, up, down, right, respectively.
  2. Assume you defined 4 variables
    
    S_LEFT = 1
    S_UP = 2
    S_DOWN = 3
    S_RIGHT = 4
    
    
  3. Define a function nextToWorker(map, direction), which will look up the map and return the neighbor cell of the worker in that direction. For example, in the following figure, nextToWorker(S_LEFT) == 'H' and nextToWorker(S_UP) == ' '.
  4. Define a function moveWorker(map, direction).
  5. In your main loop, when the user press the UP key ('K'), and nextToWorker(S_UP) == ' ', call moveWorker(map, S_UP) to move the worker (update its position in the map) and re-draw.
  6. Handle S_DOWN, S_LEFT and S_RIGHT similarly.
  7. Define a function error_message(msg) to warn the player if it is not a legal move.