Question: Please help me on how to draw map in this class? MapDrawer: extends javafx.Canvas and draws map from demo file Each room is rendered as
Please help me on how to draw map in this class?
MapDrawer: extends javafx.Canvas and draws map from demo file
Each room is rendered as a square with small strokes into the interior at the midpoints of edges where there is an exit to an adjacent room (North, South, East or West).
The player is to be indicated with a @ in the top left quadrant (ie the top left corner) of the room. If any treasure is present in the room, a $ is drawn in the top right quadrant. If any live critters are present in the room, a M is drawn in the bottom left quadrant. Any dead critters are indicated with a m in the bottom right quadrant.
Player methods:
void fight(Mob mob), boolean wantsToFight(Mob mob), boolean isAlive(), void setAlive(boolean alive), void takeDamage(int d), int getDamage()
Treasure methods:
double getValue(), boolean canLoot
Critter methods:
Same as Player methods
code for loadMap:
/** * Read information from a file created with saveMap. * @param filename Filename to read from * @return Null if unsucessful. If successful, an array of two Objects. * @throws FileNotFoundException handles file to read from not found * [0] being the Player object (if found) and [1] being the start room. */ public static Object[] loadMap(String filename) throws FileNotFoundException { if (null == filename) { return null; //return null on failure } BufferedReader br = null; FileInputStream fis = new FileInputStream(filename); try { br = new BufferedReader(new InputStreamReader(fis)); //The number of rooms in the file int numOfRooms = Integer.valueOf(br.readLine()); Room[] rooms = new Room[numOfRooms]; //Read all room descriptions for (int i = 0; i < numOfRooms; i++) { rooms[i] = new Room(br.readLine()); } //Room exits for (int i = 0; i < numOfRooms; i++) { int numberOfExits = Integer.valueOf(br.readLine()); for (int j = 0; j < numberOfExits; j++) { String[] exit = br.readLine().split(" "); int roomIndex = Integer.valueOf(exit[0]); String direction = exit[1]; rooms[i].addExit(direction, rooms[roomIndex]); } } Player player = null; //Room contents for (int i = 0; i < numOfRooms; i++) { int numberOfItems = Integer.valueOf(br.readLine()); //number of items in room for (int j = 0; j < numberOfItems; j++) { String item = br.readLine(); Thing thing = decodeThing(item, rooms[i]); // Do not add the player to the room they appear in, // the caller will be responsible for placing the player in the start room if (thing instanceof Player) { if (null == player) player = (Player) thing; } else { rooms[i].enter(thing); } } } return new Object[] {player, rooms[0]}; //if successful, return player Object (if found) and start room } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != br) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; //return null otherwise } BoundsMapper visit:
/** Assign room coordinates relative to a neighbour. *
If room has no known neighbours, give it coordinate (0,0).
* If your "North" neighbour has coordinates (x,y), then * your coodinates should be (x, y-1).
* If your "East" neighbour has coordinates (x,y), then
* your coordinates should be (x+1, y).
* (Similar for South and West). * *
Check for known coordinates in order: North, South, East, West. * @param room Room to assign coordinates to * @require All exits are labelled * one of {"North", "South", "East", "West"} * */ protected void visit(Room room) { Mapexits = room.getExits(); Pair n = null; if (hasVisited(exits.get("North"))) { Pair p = coords.get(exits.get("North")); n = new Pair(p.x,p.y + 1); // remember screen coords flipped } else if (hasVisited(exits.get("South"))) { Pair p = coords.get(exits.get("South")); n = new Pair(p.x,p.y - 1); // remember screen coords flipped } else if (hasVisited(exits.get("East"))) { Pair p = coords.get(exits.get("East")); n = new Pair(p.x - 1,p.y); } else if (hasVisited(exits.get("West"))) { Pair p = coords.get(exits.get("West")); n = new Pair(p.x + 1,p.y); } else { // can't be sure where we are, assume 0, 0 n = new Pair(0,0); } coords.put(room, n); if (n.x < xMin) { xMin = n.x; } else if (n.x > xMax) { xMax = n.x; } if (n.y < yMin) { yMin = n.y; } else if (n.y > yMax) { yMax = n.y; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
