HW: Sokoban-1

  1. Write a function readMap(n). When n==0, it reads a map from the file "map000.txt".
  2. The digits "000" may be replaced by other sequence number between 1 and 150.
  3. The format of the map is a plain-text file.
    1. Each line shows a row in the map.
      • A character 'H' indicates a solid wall.
      • A character ' ' indicates an empty location.
      • A character 'B' indicates a box.
      • A character 'D' indicates the destination where you should move a box to.
      • A character 'C' indicates this location is a destination of some box, but it is also occupied by a box at the beginning.
        • At first you may think this case is redundant, because a box is already at its destination. However, there are some levels (e.g. Level 91) which require you to move the box to another destination if you want to solve the puzzle.
      • A character 'W' indicates the initial position of the worker.
  4. Design a function showMap(m), which draws the map on your screen using the curses library.
  5. The following is a sample input, and the map your program is expected to display.
    HHHHHH
    H BD H
    HWC HH
    H BD H
    HHHHHH
    
  6. You may want to define the following color pairs to control the profile when you draw the map. For example, you may easily change your program to display the destination as a space with background color green or cyan:
  7. Note that for 'D', you should not display a character 'D', but a blank with cyan background. Try to think of a smart design to implement this.
  8. Test your functions with the following main program:
    
    def main():
        myMap = readMap(0)
        showMap(myMap)
    
    if __name__ == "__main__":
        main()
    
    
  9. Bonus: Can you detect if the format of a map file is invalid?
    The following are some basic rules:
    1. The number of 'B' must be the same as 'D'.
    2. There should be exactly one 'W'.
    3. The only valid characters in the map matrix are 'H', ' ', 'B', 'C', 'D', 'W'.