Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. protected void readMap(String fileName) {
  2. FileReader fr;
  3. int layerCounter = 0;
  4. int lineLength = 0;
  5. String mapString = "";
  6. String newLine = "";
  7. int charCounter = 0;
  8. boolean notFinishedReading = true;
  9. try {//Try catch to catch any IO errors
  10. fr = new FileReader(fileName);//File reader to read in the file
  11. BufferedReader br = new BufferedReader(fr);//Buffered reader to read in line by line
  12. setMapName(br.readLine());//First line is the map name
  13. setGoldRequired(Integer.parseInt(br.readLine()));//Second line is the gold required
  14. while(notFinishedReading == true) {
  15. newLine = br.readLine();//Reads next line of files
  16. if(newLine != null) {//Ensures that the whole map has not been read already
  17. lineLength = newLine.length();//Length of each line
  18. mapString = mapString + newLine;//Writes map onto one string
  19. layerCounter = layerCounter + 1;//Counts how many lines were taken in
  20. }
  21. else {//If map is read
  22. notFinishedReading = false;//Stop looping
  23. }
  24. }
  25. map = new char[layerCounter][lineLength];//Set map to have new limits to match new map
  26. for(int i = 0; i < layerCounter; i++) {//Nested loops to
  27. for(int j = 0; j < lineLength; j++) {
  28. map[i][j] = mapString.charAt(charCounter);
  29. charCounter++;
  30. }
  31. }
  32. setMap(map);
  33. br.close();
  34. fr.close();
  35. } catch (IOException e) {//In case of IO error
  36. e.printStackTrace();
  37. }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement