aboutsummaryrefslogtreecommitdiff
path: root/Занимательное программирование/4/2_wave/src/main/java/net/caraus/labyrinth/Maze.java
blob: eaeb72bf5191b32d7e38011663297b0338a73814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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()];
    }
}