Advertisement
Guest User

maze

a guest
Nov 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. package se.isark.robot;
  2.  
  3. import java.io.IOException;
  4. import java.io.Reader;
  5. import java.util.Arrays;
  6.  
  7. public class Maze {
  8.     //Could have used some dynamic way of storing the values but I felt like writing functions for e.g. parsing the maximum (char)area of the file being read.
  9.     private char[][] mazeData;
  10.     private Reader mapping;
  11.     private Position startPos;
  12.  
  13.  
  14.     public Maze(Reader reader) throws IOException {
  15.         mapping = reader;
  16.         mazeData = initMazeSize();
  17.         System.out.println(mazeData.length + ":" + mazeData[0].length);
  18.        
  19.     }
  20.  
  21.     public boolean isMovable() { //TODO: Inte säker än på ändamålet.
  22.  
  23.         return false;
  24.     }
  25.  
  26.     public boolean isGoal(Position posToCheck) {
  27.         return (posToCheck == startPos);
  28.     }
  29.  
  30.     public Position getStartPosition() {
  31.         return startPos;
  32.     }
  33.  
  34.     private void fillArray() {
  35.         for(char[] row : mazeData) {
  36.             Arrays.fill(row, '*');
  37.         }
  38.     }
  39.    
  40.     private char[][] initMazeSize() throws IOException {
  41.         String stringMap = getMap();
  42.        
  43.         int width = getWidth(stringMap);
  44.         int height = getHeight(stringMap);
  45.        
  46.         return new char[width][height];
  47.     }
  48.  
  49.     private String getMap() throws IOException {
  50.         String map = "";
  51.  
  52.         char read = (char)mapping.read();
  53.  
  54.         while(read != (char)-1) {
  55.             map += read;
  56.             read = (char)mapping.read();
  57.         }
  58.        
  59.         return map;
  60.     }
  61.  
  62.     private int getWidth(String stringMap) {
  63.         int currentMaxWidth = 0;
  64.         int currentWidth = 0;
  65.         int i = 0;
  66.         while(i < stringMap.length()) {
  67.             if(stringMap.charAt(i) == '\n') {
  68.                 if(currentWidth > currentMaxWidth) {
  69.                     currentMaxWidth = currentWidth;
  70.                 }
  71.                 currentWidth = 0;
  72.             } else {
  73.                 currentWidth++;
  74.             }
  75.             i++;
  76.         }
  77.         if(currentWidth > currentMaxWidth) {    // Last line might not contain a \n and thus the above won't account for the width of that line.
  78.             currentMaxWidth = currentWidth;
  79.         }
  80.         return currentMaxWidth;
  81.     }
  82.  
  83.     private int getHeight(String stringMap) {
  84.         int height = 0;
  85.         for(int i = 0; i < stringMap.length(); i++) {
  86.             if(stringMap.charAt(i) == '\n') {
  87.                 height++;
  88.             }
  89.         }
  90.         if(stringMap.charAt(stringMap.length()-1) != '\n') { // Last line might not contain a \n and thus the above won't account for that line.
  91.             height++;
  92.         }
  93.         return height;
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement