Question
In C++ using File Processing Write a program that
allows you to write stats of a monster and save it to a file. (in text format) (Things like Name, Hit Points, Minimum damage and Maximum damage.)
Allows you to open the file and read the stats of the monster. (Print what was read onto the screen.)
Allow the user to decide what file to open (just type in the filename)
Allow the program to load the stats of two monsters and let them battle it out.
1. Create monster stats
2. Ask the user if creating or loading
(ask the user the file name)
3a. If loading
open file for reading
read stats in from file
print stats to screen
3b. If creating
open file for writing
write stats to file - make sure to leave spaces or returns between variables, so all values do not get smooshed together
Explanation / Answer
public final class Dungeon { private final Map map = new HashMap(); private Room currentRoom; private int currentX = 0; private int currentY = 0; private Dungeon() { } private void putRoom(int x, int y, Room room) { if (!map.containsKey(x)) { map.put(x, new HashMap()); } map.get(x).put(y, room); } private Room getRoom(int x, int y) { return map.get(x).get(y); } private boolean roomExists(int x, int y) { if (!map.containsKey(x)) { return false; } return map.get(x).containsKey(y); } private boolean isComplete() { return currentRoom.isBossRoom() && currentRoom.isComplete(); } public void movePlayer(Player player) throws IOException { boolean northPossible = roomExists(currentX, currentY + 1); boolean southPossible = roomExists(currentX, currentY - 1); boolean eastPossible = roomExists(currentX + 1, currentY); boolean westPossible = roomExists(currentX - 1, currentY); System.out.print("Where would you like to go :"); if (northPossible) { System.out.print(" North (n)"); } if (eastPossible) { System.out.print(" East (e)"); } if (southPossible) { System.out.print(" South (s)"); } if (westPossible) { System.out.print(" West (w)"); } System.out.print(" ? "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String direction = in.readLine(); if (direction.equals("n") && northPossible) { currentY++; } else if (direction.equals("s") && southPossible) { currentY--; } else if (direction.equals("e") && eastPossible) { currentX++; } else if (direction.equals("w") && westPossible) { currentX--; } currentRoom = getRoom(currentX, currentY); currentRoom.enter(player); } public void startQuest(Player player) throws IOException { while (player.isAlive() && !isComplete()) { movePlayer(player); } if (player.isAlive()) { System.out.println(Art.CROWN); } else { System.out.println(Art.REAPER); } } public static Dungeon newInstance() { Dungeon dungeon = new Dungeon(); dungeon.putRoom(0, 0, Room.newRegularInstance()); dungeon.putRoom(-1, 1, Room.newRegularInstance()); dungeon.putRoom(0, 1, Room.newRegularInstance()); dungeon.putRoom(1, 1, Room.newRegularInstance()); dungeon.putRoom(-1, 2, Room.newRegularInstance()); dungeon.putRoom(1, 2, Room.newRegularInstance()); dungeon.putRoom(-1, 3, Room.newRegularInstance()); dungeon.putRoom(0, 3, Room.newRegularInstance()); dungeon.putRoom(1, 3, Room.newRegularInstance()); dungeon.putRoom(0, 4, Room.newBossInstance()); dungeon.currentRoom = dungeon.getRoom(0, 0); return dungeon; } }