PIbd-23_Polevoy_S.V._SelfPr.../ForestMap.java

50 lines
1.6 KiB
Java
Raw Normal View History

2022-10-08 16:16:03 +04:00
import java.awt.*;
import java.util.Arrays;
public class ForestMap extends AbstractMap {
private final Color barrierColor = Color.green;
private final Color roadColor = new Color(165, 42, 42, 255);
@Override
protected void drawBarrierPart(Graphics2D g, int i, int j)
{
g.setColor(barrierColor);
2022-10-08 17:51:17 +04:00
g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y);
2022-10-08 16:16:03 +04:00
}
@Override
protected void drawRoadPart(Graphics2D g, int i, int j)
{
g.setColor(roadColor);
2022-10-08 17:51:17 +04:00
g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y);
2022-10-08 16:16:03 +04:00
}
@Override
protected void generateMap() {
_map = new int[50][50];
_size_x = (float)_width / _map[0].length;
_size_y = (float)_height / _map.length;
int counter = 0;
for (int[] row : _map) {
Arrays.fill(row, _freeRoad);
}
while (counter < 20)
{
int x = _random.nextInt(2, 49);
int y = _random.nextInt(3, 50);
2022-10-08 17:51:17 +04:00
var points = new int[] { _map[y][x], _map[y - 1][x], _map[y - 2][x], _map[y - 2][x - 1], _map[y - 2][x + 1], _map[y - 3][x] };
2022-10-08 16:16:03 +04:00
var forComparison = new int[] { _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad };
if (Arrays.equals(points, forComparison))
{
2022-10-08 17:51:17 +04:00
_map[y][x] = _barrier;
_map[y - 1][x] = _barrier;
_map[y - 2][x] = _barrier;
_map[y - 2][x - 1] = _barrier;
_map[y - 2][x + 1] = _barrier;
_map[y - 3][x] = _barrier;
2022-10-08 16:16:03 +04:00
counter++;
}
}
}
}