import java.io.*; import java.util.*; import java.nio.file.*; class Graph{ Vector nodes = new Vector(); /* Node depot; Node stable; Node wheatland; Node hotel; Node grassland; Node statue; */ Player you; Graph() throws IOException{ String yourname = null; String occupation = null; //* int id = -1; String spot = ""; String des = ""; String eastId = ""; String westId = ""; String southId = ""; String northId = ""; String str = ""; String attributes[]; BufferedReader br = new BufferedReader(new FileReader("spots.txt")); while((str=br.readLine())!=null){ attributes = str.split(","); for(int i = 0; i < attributes.length; i++) { id = Integer.parseInt(attributes[0].trim()); spot = attributes[1].trim(); des = attributes[2].trim(); eastId = attributes[3].trim(); westId = attributes[4].trim(); southId = attributes[5].trim(); northId = attributes[6].trim(); } Node newNode = new Node(id, spot, des); if(!(eastId.equalsIgnoreCase("null"))){ newNode.eastId = Integer.parseInt(eastId.trim()); } if(!(westId.equalsIgnoreCase("null"))){ newNode.westId = Integer.parseInt(westId.trim()); } if(!(southId.equalsIgnoreCase("null"))){ newNode.southId = Integer.parseInt(southId.trim()); } if(!(northId.equalsIgnoreCase("null"))){ newNode.northId = Integer.parseInt(northId.trim()); } nodes.add(newNode); }//while br.close(); for(int i = 0; i < nodes.size(); i++){ int eid, wid, sid, nid; if(!((eid=nodes.get(i).eastId) == -1)){ nodes.get(i).setEast(nodes.get(eid)); } if(!((wid=nodes.get(i).westId) == -1)){ nodes.get(i).setWest(nodes.get(wid)); } if(!((sid=nodes.get(i).southId) == -1)){ nodes.get(i).setSouth(nodes.get(sid)); } if(!((nid=nodes.get(i).northId) == -1)){ nodes.get(i).setNorth(nodes.get(nid)); } }//for //*/ /* depot = new Node(0, "修道院", "古樸恢弘的建築"); stable = new Node(1, "馬廄","幾批馬正在吃草"); wheatland = new Node(2, "一大片麥田","陽光照耀著金黃色的麥田"); hotel = new Node(3, "有間旅館", "一間破舊的旅社"); grassland = new Node(4,"綠油油的草地","風吹過草地形成一片片的波浪"); statue = new Node(5, "大理石雕像","美麗的女神雕像似乎正望著你"); ///////////////////////////// depot.east = stable; depot.west = wheatland; depot.north = statue; depot.south = grassland; stable.west = depot; wheatland.east = depot; wheatland.south = hotel; grassland.north = depot; grassland.west = hotel; statue.south = depot; hotel.north = wheatland; hotel.east = grassland; //*/ ///////////////////////////// //you = new Player(yourname); //you.setOccupation(occupation); //you.setLocation(nodes.get(0));//you.setLocation(depot); //* --- Load into the game or create a new acount --- Scanner sc = new Scanner(System.in); System.out.print("請輸入你的名字:\n>>"); yourname = sc.next().trim(); String filename = (yourname.toLowerCase())+".txt"; if(Files.exists(Paths.get(filename))){ BufferedReader br1 = new BufferedReader(new FileReader(filename)); you = new Player(br1.readLine().trim()); you.setOccupation(br1.readLine().trim()); you.setLocation(nodes.get(Integer.parseInt(br1.readLine().trim()))); br1.close(); System.out.println("歡迎繼續冒險 " +you.getOccupation() + " " + yourname); }else{ System.out.print("請選擇你的職業: \n1.戰士\n2.法師\n3.牧師\n>>"); int occupationId = -1; while(true){//if choose the wrong number of occupation, you need to choose again until it is valid occupationId = sc.nextInt(); switch(occupationId){ case 1: occupation = "戰士"; break; case 2: occupation = "法師"; break; case 3: occupation = "牧師"; break; default: System.out.println("請選擇上列職業編號"); }//switch if(occupation != null) break; }//while you = new Player(yourname); you.setOccupation(occupation); you.setLocation(nodes.get(0)); System.out.println("歡迎開始冒險 " +you.getOccupation() + " " + yourname); } //*/ }//Constructor //Methods...... public String moveEast(){ if(you.location.east==null){ return "前面沒路可走"; }else{ you.location = you.location.east; return you.getName() + "往 東 走向 " + you.location.getSpot() + "\n看到 " + you.location.toString(); } }//moveEast() public String moveWest(){ if(you.location.west==null){ return "前面沒路可走"; }else{ you.location = you.location.west; return you.getName() + "往 西 走向 " + you.location.getSpot() + "\n看到 " + you.location.toString(); } }//moveEast() public String moveSouth(){ if(you.location.south==null){ return "前面沒路可走"; }else{ you.location = you.location.south; return you.getName() + "往 南 走向 " + you.location.getSpot() + "\n看到 " + you.location.toString(); } }//moveEast() public String moveNorth(){ if(you.location.north==null){ return "前面沒路可走"; }else{ you.location = you.location.north; return you.getName() + "往 北 走向 " + you.location.getSpot() + "\n看到 " + you.location.toString(); } }//moveEast() //////////////////////////////////// public String sit(){ if(you.getPose().contains("坐")){ return "你已經坐著了"; }else{ you.setPose("坐著"); return you.getName() + "坐了下來"; } }//sit() public String stand(){ if(you.getPose().contains("站")){ return "你已經站著了"; }else{ you.setPose("站著"); return you.getName() + "站了起來"; } }//stand() public String look(){ return "你面前是 " + you.location.getSpot(); }//look() public String check(){ return you.getName() +" "+ you.getOccupation() + " 等級: " + you.level; }//check() }