package net.caraus.labyrinth; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.Vector2; import java.io.BufferedReader; import java.io.Reader; import java.io.IOException; public final class Maze { private Location[][] maze; public static final int[] DX = {1, 0, -1, 0}; public static final int[] DY = {0, -1, 0, 1}; public static final float CELL_SIZE = 75.0f; public Maze(Reader inputReader) throws IOException { BufferedReader reader = new BufferedReader(inputReader); String[] inputPair = reader.readLine().split(" "); var width = Integer.parseInt(inputPair[0]); var height = Integer.parseInt(inputPair[1]); this.maze = new Location[width + 1][height + 1]; for (var y = 0; y <= height; ++y) { for (var x = 0; x <= width; ++x) { if (y == height || x == width) { this.maze[x][y] = new Location(true, true); } else { inputPair = reader.readLine().split(" "); this.maze[x][y] = new Location(inputPair[1].equals("1"), inputPair[0].equals("1")); } } } } public int width() { return this.maze == null ? 0 : this.maze.length - 1; } public int height() { return this.maze == null ? 0 : this.maze[0].length - 1; } public void draw(ShapeRenderer shape) { for (var x = 0; x < width(); ++x) { for (var y = 0; y < height(); ++y) { var start = new Vector2((x + 1) * CELL_SIZE, (height() - y + 1) * CELL_SIZE); if (this.maze[x][y].upWall()) { var end = new Vector2(start).add(CELL_SIZE, 0); shape.line(start, end); } if (this.maze[x][y].leftWall()) { var end = new Vector2(start).sub(0, CELL_SIZE); shape.line(start, end); } } } // Рисуем нижнюю и правую стены. var edge = new Vector2(CELL_SIZE * (width() + 1), CELL_SIZE); shape.line(new Vector2(CELL_SIZE, CELL_SIZE), edge); shape.line(new Vector2(edge).add(0, height() * CELL_SIZE), edge); } public boolean canGo(Point xy, int deltaIndex) { if (DX[deltaIndex] == -1) { return !this.maze[xy.x()][xy.y()].leftWall(); } else if (DX[deltaIndex] == 1) { return !this.maze[xy.x() + 1][xy.y()].leftWall(); } else if (DY[deltaIndex] == -1) { return !this.maze[xy.x()][xy.y()].upWall(); } else { return !this.maze[xy.x()][xy.y() + 1].upWall(); } } public Location at(Point xy) { return this.maze[xy.x()][xy.y()]; } }