Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package se.isark.robot;
- import java.io.IOException;
- import java.io.Reader;
- import java.util.Arrays;
- public class Maze {
- //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.
- private char[][] mazeData;
- private Reader mapping;
- private Position startPos;
- public Maze(Reader reader) throws IOException {
- mapping = reader;
- mazeData = initMazeSize();
- System.out.println(mazeData.length + ":" + mazeData[0].length);
- }
- public boolean isMovable() { //TODO: Inte säker än på ändamålet.
- return false;
- }
- public boolean isGoal(Position posToCheck) {
- return (posToCheck == startPos);
- }
- public Position getStartPosition() {
- return startPos;
- }
- private void fillArray() {
- for(char[] row : mazeData) {
- Arrays.fill(row, '*');
- }
- }
- private char[][] initMazeSize() throws IOException {
- String stringMap = getMap();
- int width = getWidth(stringMap);
- int height = getHeight(stringMap);
- return new char[width][height];
- }
- private String getMap() throws IOException {
- String map = "";
- char read = (char)mapping.read();
- while(read != (char)-1) {
- map += read;
- read = (char)mapping.read();
- }
- return map;
- }
- private int getWidth(String stringMap) {
- int currentMaxWidth = 0;
- int currentWidth = 0;
- int i = 0;
- while(i < stringMap.length()) {
- if(stringMap.charAt(i) == '\n') {
- if(currentWidth > currentMaxWidth) {
- currentMaxWidth = currentWidth;
- }
- currentWidth = 0;
- } else {
- currentWidth++;
- }
- i++;
- }
- if(currentWidth > currentMaxWidth) { // Last line might not contain a \n and thus the above won't account for the width of that line.
- currentMaxWidth = currentWidth;
- }
- return currentMaxWidth;
- }
- private int getHeight(String stringMap) {
- int height = 0;
- for(int i = 0; i < stringMap.length(); i++) {
- if(stringMap.charAt(i) == '\n') {
- height++;
- }
- }
- if(stringMap.charAt(stringMap.length()-1) != '\n') { // Last line might not contain a \n and thus the above won't account for that line.
- height++;
- }
- return height;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement