Guest User

Untitled

a guest
Jun 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5.  
  6. public class GameMap {
  7.  
  8. private MapLocation[][] map;
  9.  
  10. public GameMap(String fname) throws FileNotFoundException {
  11.  
  12. Scanner scan = new Scanner(new File(fname));
  13.  
  14. String[] rowCol = scan.nextLine().split("\\s*[x]\\s*");
  15.  
  16. int x = Integer.parseInt(rowCol[0]);
  17. int y = Integer.parseInt(rowCol[1]);
  18.  
  19. map = new MapLocation[x][y];
  20.  
  21. for (int i = 0; i < x; i++) {
  22. for (int j = 0; j < y; j++) {
  23. map[i][j] = new MapLocation("BAD ROOM", "", "");
  24.  
  25. }
  26. }
  27.  
  28. while (scan.hasNextLine()) {
  29.  
  30. try{
  31. try{
  32. String phrase = scan.nextLine();
  33.  
  34.  
  35. char char1 = '#';
  36.  
  37. if (phrase.charAt(0) == char1) {
  38. System.out.println();
  39. }
  40.  
  41. else {
  42. String[] lines = new String[4];
  43. lines = phrase.split("\\t+");
  44.  
  45. String coords = lines[0];
  46. String location = lines[1];
  47. String action = lines[2];
  48. String desc = lines[3];
  49.  
  50. // Store numbers to print later
  51. String[] newCoords = coords.split("\\s*[,]\\s*");
  52.  
  53. int x1 = Integer.parseInt(newCoords[0]);
  54. int y1 = Integer.parseInt(newCoords[1]);
  55. map[x1][y1].setMapLocationName(location);
  56. map[x1][y1].setMapLocationDescription(desc);
  57. map[x1][y1].setAction(action);
  58.  
  59. }}
  60. catch (ArrayIndexOutOfBoundsException e) {
  61.  
  62. }
  63. }
  64.  
  65. catch (StringIndexOutOfBoundsException e) {
  66.  
  67. }
  68.  
  69. }
  70.  
  71. }
  72.  
  73. public String mapAction(int x, int y) {
  74. return map[x][y].getAction();
  75. }
  76.  
  77. public String getName(int x, int y) {
  78.  
  79. return map[x][y].getMapLocationName();
  80. }
  81.  
  82. public String getDescription(int x, int y) {
  83. return map[x][y].getMapLocationDescription();
  84. }
  85.  
  86. public String getAction(int x, int y) {
  87. return map[x][y].getAction();
  88. }
  89.  
  90. public String toString()
  91. {
  92. String s = new String();
  93. for (int i = 0; i < map.length; i++)
  94. for (int j = 0; j<map[i].length;j++)
  95. if(map[i][j] != null)
  96. s+= i + "," + j + "\t" + map[i][j].toString() + "\n";
  97.  
  98. return s;
  99. }
  100. }
Add Comment
Please, Sign In to add comment