Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class GameMap {
- private MapLocation[][] map;
- public GameMap(String fname) throws FileNotFoundException {
- Scanner scan = new Scanner(new File(fname));
- String[] rowCol = scan.nextLine().split("\\s*[x]\\s*");
- int x = Integer.parseInt(rowCol[0]);
- int y = Integer.parseInt(rowCol[1]);
- map = new MapLocation[x][y];
- for (int i = 0; i < x; i++) {
- for (int j = 0; j < y; j++) {
- map[i][j] = new MapLocation("BAD ROOM", "", "");
- }
- }
- while (scan.hasNextLine()) {
- try{
- try{
- String phrase = scan.nextLine();
- char char1 = '#';
- if (phrase.charAt(0) == char1) {
- System.out.println();
- }
- else {
- String[] lines = new String[4];
- lines = phrase.split("\\t+");
- String coords = lines[0];
- String location = lines[1];
- String action = lines[2];
- String desc = lines[3];
- // Store numbers to print later
- String[] newCoords = coords.split("\\s*[,]\\s*");
- int x1 = Integer.parseInt(newCoords[0]);
- int y1 = Integer.parseInt(newCoords[1]);
- map[x1][y1].setMapLocationName(location);
- map[x1][y1].setMapLocationDescription(desc);
- map[x1][y1].setAction(action);
- }}
- catch (ArrayIndexOutOfBoundsException e) {
- }
- }
- catch (StringIndexOutOfBoundsException e) {
- }
- }
- }
- public String mapAction(int x, int y) {
- return map[x][y].getAction();
- }
- public String getName(int x, int y) {
- return map[x][y].getMapLocationName();
- }
- public String getDescription(int x, int y) {
- return map[x][y].getMapLocationDescription();
- }
- public String getAction(int x, int y) {
- return map[x][y].getAction();
- }
- public String toString()
- {
- String s = new String();
- for (int i = 0; i < map.length; i++)
- for (int j = 0; j<map[i].length;j++)
- if(map[i][j] != null)
- s+= i + "," + j + "\t" + map[i][j].toString() + "\n";
- return s;
- }
- }
Add Comment
Please, Sign In to add comment