package Wumpus;/** * A cave the holds the room array */import java.io.*;import System.Collections.Generic.*;public class Cave{ //Arraylist containing all the room objects of a specific cave private List<Room> roomList; private int caveNum; //Default Constructor with an null arraylist of rooms public Cave() { roomList = null; } public Cave(int aCaveNum) { caveNum = aCaveNum; roomList = new List<Room>(); } public void initializeCave() { //Don't fully understand read/write from file, but will hopefully read cave information from a .dat file //Which will be an array with all the rooms needed for that specific cave. File cave = new File("cave" + caveNum + ".dat"); if (cave.exists()) { try { BufferedReader in = new BufferedReader(new FileReader(cave)); for (int i = 0; i < 30; i++) { String[] list = in.readLine().Split(new char[] { ';' }); int[] rmList = new int[6]; boolean canHaveRm = true; int roomNum = -9; for (int j = 0; j < list.length; j++) { if (list[j].Contains(",")) { String[] connectingRm = list[j].Split(new char[] { ',' }); for (int k = 0; k < connectingRm.length; k++) { rmList[k] = Integer.parseInt(connectingRm[k]); } } else if (list[j].equalsIgnoreCase("true")) { canHaveRm = true; } else if (list[j].equalsIgnoreCase("false")) { canHaveRm = false; } else { roomNum = Integer.parseInt(list[j]); } } roomList.Add(new Room(roomNum, rmList, canHaveRm)); } in.close(); } catch(FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { //IOException handling replaced with this. If the file "cave" doesn't exist, will automatically //Use a known file which we can build later. /* File defaultCave = new File("defaultcave.dat"); ObjectInputStream in = new ObjectInputStream(new FileInputStream(defaultCave)); roomList = (List<Room>)in.readObject(); in.close(); */ } } /** * @param roomNum the number of the room to be returned. Based for now on its array position, but can be set to be displayed room number */ public Room getRoom(int roomNum) { return roomList.get_Item(roomNum); } /** * @param roomNum the room number to be used to get connections; * @return an array of the connecting rooms; */ public int[] getConnections(int roomNum) { Room room = roomList.get_Item(roomNum); return room.getConnections(); } public int getCaveName() { return caveNum; }}