MapPrinter class assignment

From WLCS

Objective

  • You will learn to create a class that makes use two-dimensional (2D) arrays and console output

Purpose

  • The purpose of this class is to print a Map onto the terminal screen.

Resources

Attributes

No additional attributes - they are already provided

Methods

Your MapPrinter class should implement the following methods:

printSimpleMap (Map map, Room theRoom):

  • This method should go through the entire Map, and print out a single character for each room. The following characters should be printed:
    • 'X' for the current location of the player. This location is specified by the second parameter (theRoom). Note that the Room class now has a getCol() method, which returns the numerical column of the Room within the map, and similarly a getRow() method. Thus, to see if you are in the current room where the character is, you compare the current row with getRow(), and the current column with getCol().
    • 'W' if there is a weapon in this room, but no monster. You will want to use the getItem() method from the Room class, which returns null if there is no item present.
    • 'M' (for monster) if there is a creature in this room, but no weapon. You will want to use the getMonster() method from the Room class, which returns null if there is no creature present.
    • 'B' if there is both a creature and a weapon in the room
    • ' ' (space) if there is neither a weapon nor a creature in the room. You may also want to use a lower case 'o' for an empty room as well, as this may be easier to read.

How to start: Your printSimpleMap() method will need nested for loops.

  1. Write a for loop that iterates (traverses) through a row (0 up to height)
    1. Write a for loop that traverses through a column (0 up to width)
      1. Inside the inner for loop, print out the specific character via the System.out.print() method (as all the rooms in a given row need to be on the same line). To do this, you must have several if statements to check the various conditions of the room. e.g. if (rooms[r][c].getWeapon() != null)
    2. After you go through all the columns of a single row, you move to the next line via the System.out.println() method.


  • For the map provided by the main() method in the Media:MapPrinter.java file, the resulting map is as follows:
X W
WWWW
W  WM
 W
   W M
M  WB


  • This may make a bit more sense if we put it into a table:
X   W      
W W W W    
W     W M  
  W        
      W   M
M     W B  


printMap (Map map, Room theRoom):

  • The printSimpleMap() method is useful, but it does not tell us which rooms are connected to each other. In particular, from any given room, you can not necessarily go between them, as there may not be doors present. Thus, we are going to enhance our printSimpleMap() method to add door information.
  • To print a more detailed map, you will print lines that connect the rooms. If there is a doorway between two rooms next to each other (i.e. a east-west doorway), then we will print a '-' (dash) between the rooms. If there is a doorway between two rooms on top of each other (i.e. a north-south doorway), then we will print a '|' (vertical bar) between those two rooms.
  • Thus, our original 6x6 map will look like the following when finished:
X- -W  - -
    | |
W-W W W- -
| | |     |
W     W-M
  | | | | |
 -W
| | | | | |
      W   M
|   | | | |
M- - -W B-


  • Note that this is the same map as above -- the method is just printout out the doorway information this time. The 'X' tells us that the player's current location is in the upper-left corner. From there, s/he can head south, then east, then south, then west, etc.

How to start:

  • Copy-and-paste the code from your printSimpleMap() to your printMap() method
  • To print the dashes for the east-west doors, you will want to use the getRoomToEast() method, which will return null if there is no doorway present. We are assuming that if there is a doorway going one way, then there is a doorway going the other way. Thus, after printing the character for each room, you will either print a space (if there is no doorway) or a dash (if there is a doorway). Thus, the code (so far) is the same as the printSimpleMap() method, with the addition of a character after each room, depending on whether there is a door to the east of a given room. It will be easiest to make sure this is working before you continue to the north-south doors.
  • The 6x6 map with only east-west doors will look like the following:
X- -W  - -
W-W W W- -
W     W-M
 -W
      W   M
M- - -W B-


  • To print the north-south doorways, you will most likely need to use the getRoomToSouth() method, which also returns null if there is no doorway present. Again, we are assuming that all doorways go in both directions.
    • After the for loop that prints out a line of rooms, you will need a separate for loop to print out a line of spaces (if there is no doorway) or vertical bars (if there is a doorway). This for loop is still within the outer for loop, but separate from the inner for loop used above.
    • Note that between each doorway character (space or vertical bar) there is an additional space (so that it lines up with the east-west doorway). Thus, in this for loop, you either print out two spaces (if there is no north-south door) or a vertical bar and a space (if there is a north-south door). The extra space is so that the north-south doors line up with the rooms above and below (because of the east-west doors, the rooms are now every other spot).

Testing