-
Notifications
You must be signed in to change notification settings - Fork 14
Создал модуль по генерации карт #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <artifactId>hypernull</artifactId> | ||
| <groupId>ru.croccode.hypernull</groupId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <artifactId>create-map</artifactId> | ||
|
|
||
|
|
||
|
|
||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| import java.io.IOException; | ||
| import java.util.*; | ||
|
|
||
| public class CreateMap { | ||
| private int MAP_WIDTH; | ||
| private int MAP_HEIGHT; | ||
| private boolean[][] MAP; | ||
| private Queue<Point> queuePoint; | ||
| private int countBot; | ||
| private List<Point> listOfSpawn; | ||
|
|
||
|
|
||
| public CreateMap(int height, int width) { | ||
| this.MAP_WIDTH = width; | ||
| this.MAP_HEIGHT = height; | ||
|
|
||
| MAP = new boolean[height][width]; | ||
| queuePoint = new ArrayDeque<>(); | ||
|
|
||
| int rndX = (int) (Math.random() * MAP_HEIGHT); | ||
| int rndY = (int) (Math.random() * MAP_WIDTH); | ||
| queuePoint.add(new Point(rndX, rndY)); | ||
|
|
||
| createLabirint(); | ||
| setSpawnBot(); | ||
| PrintMap(); | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| * метод реализует создание идеального лабиринта | ||
| * Логика метода: На вход передается случайная точка, метод определяет в какие точки можно пойти из это точки, | ||
| * создает список этих точек, случайным образом выбирает в какую точку может пойти, удаляет выбранную точку | ||
| * из нашего списка, отправляет наш список в очередь точек, для выбранной точки метод начинается сначала | ||
| * */ | ||
| public void createLabirint() { | ||
| while (!queuePoint.isEmpty()) { | ||
| //получаем точку | ||
| Point point = queuePoint.poll(); | ||
| MAP[point.getX()][point.getY()] = true; | ||
| //добавляем точки, в которые можем пойти из начальной точки | ||
| List<Point> list = addPointsList(point); | ||
| //выбираем в какую точку отправимся | ||
| int rnd = (int) (Math.random() * list.size()); | ||
| Point newPoint; | ||
| //если список пуст, значит нет доступных точек, пропускаем ход | ||
| if (list.size() != 0) { | ||
| newPoint = list.get(rnd); | ||
| list.remove(rnd); | ||
| queuePoint.addAll(list); | ||
| } else { | ||
| continue; | ||
| } | ||
| //выбираем случайным образом в какую сторону сдвинемся от полученной точки | ||
| // (влево/вправо по Х, или вверх/вниз по У) | ||
| int rnd2 = (int) (Math.random() * 4); | ||
| switch (rnd2) { | ||
| //Сдвигаемся в зависимости от полученного рандомного положения | ||
| case (0): | ||
| if ((0 <= newPoint.getX() - 2 && newPoint.getX() + 2 < MAP_HEIGHT) && !MAP[newPoint.getX() - 2][newPoint.getY()]) { | ||
| MAP[newPoint.getX()][newPoint.getY()] = true; | ||
| MAP[newPoint.getX() + 1][newPoint.getY()] = true; | ||
| addPoints(newPoint); | ||
| break; | ||
| } | ||
| case (1): | ||
| if ((0 <= newPoint.getX() - 2 && newPoint.getX() + 2 < MAP_HEIGHT) && !MAP[newPoint.getX() + 2][newPoint.getY()]) { | ||
| MAP[newPoint.getX()][newPoint.getY()] = true; | ||
| MAP[newPoint.getX() - 1][newPoint.getY()] = true; | ||
| addPoints(newPoint); | ||
| break; | ||
| } | ||
| case (2): | ||
| if (0 <= newPoint.getY() - 2 && newPoint.getY() + 2 < MAP_WIDTH && !MAP[newPoint.getX()][newPoint.getY() - 2]) { | ||
| MAP[newPoint.getX()][newPoint.getY()] = true; | ||
| MAP[newPoint.getX()][newPoint.getY() + 1] = true; | ||
| addPoints(newPoint); | ||
| break; | ||
| } | ||
| case (3): | ||
| if ((0 <= newPoint.getY() - 2 && newPoint.getY() + 2 < MAP_WIDTH) && !MAP[newPoint.getX()][newPoint.getY() + 2]) { | ||
| MAP[newPoint.getX()][newPoint.getY()] = true; | ||
| MAP[newPoint.getX()][newPoint.getY() - 1] = true; | ||
| addPoints(newPoint); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (countBot>=4){ | ||
| changeMap(); | ||
| } | ||
| } | ||
|
|
||
| //метод изменяет наш лабиринт, "дробя" его на свободные ходы | ||
| //т.е рандомим точку, проверяем есть ли в ней препятствие, если есть удаляем препятствие смешаемся по оси У | ||
| public void changeMap() { | ||
| for (int i = 0; i < MAP_HEIGHT * 2; i++) { | ||
| int rnd1 = (int) (Math.random() * MAP_HEIGHT-1); | ||
| int rnd2 = (int) (Math.random() * MAP_WIDTH-1); | ||
| if (MAP[rnd1][rnd2]) { | ||
| int count=0; | ||
| while (MAP[rnd1][rnd2] && rnd1 != MAP_HEIGHT-1 &&count<4) { | ||
| MAP[rnd1][rnd2] = false; | ||
| rnd2++; | ||
| count++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //задаем количество ботов и координаты их спавна | ||
| public void setSpawnBot() { | ||
| //проверям размер карты и исходя из него задаем количество ботов | ||
| if (0 < MAP_WIDTH * MAP_HEIGHT && MAP_WIDTH * MAP_HEIGHT < 100) { | ||
| countBot = 2; | ||
| } else if (100 <= MAP_WIDTH * MAP_HEIGHT && MAP_WIDTH * MAP_HEIGHT < 200) { | ||
| countBot = 3; | ||
| } else if (200 <= MAP_WIDTH * MAP_HEIGHT && MAP_WIDTH * MAP_HEIGHT < 900) { | ||
| countBot = 4; | ||
| } else if (900 <= MAP_WIDTH * MAP_HEIGHT && MAP_WIDTH * MAP_HEIGHT < 1_600) { | ||
| countBot = 5; | ||
| } else if (1_600 <= MAP_WIDTH * MAP_HEIGHT && MAP_WIDTH * MAP_HEIGHT < 4_200) { | ||
| countBot = 6; | ||
| } else if (4_200 <= MAP_WIDTH * MAP_HEIGHT && MAP_WIDTH * MAP_HEIGHT < 8_100) { | ||
| countBot = 9; | ||
| } else { | ||
| countBot = 12; | ||
| } | ||
| //создаем список точек в которых будут спавниться боты | ||
| listOfSpawn = new ArrayList<>(); | ||
| for (int i = 0; i < countBot; ) { | ||
| int rndX = (int) (Math.random() * MAP_HEIGHT); | ||
| int rndY = (int) (Math.random() * MAP_WIDTH); | ||
| if (!MAP[rndX][rndY]) { | ||
| listOfSpawn.add(new Point(rndX, rndY)); | ||
| i++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| * Метод реализует список точек, в которые мы можем пойти от изначальной точки | ||
| * */ | ||
| public List<Point> addPointsList(Point point) { | ||
| List<Point> list = new ArrayList<>(); | ||
| if (0 <= point.getX() - 2 && !MAP[point.getX() - 2][point.getY()]) { | ||
| list.add(new Point(point.getX() - 2, point.getY())); | ||
| } | ||
| if (point.getX() + 2 < MAP_HEIGHT && !MAP[point.getX() + 2][point.getY()]) { | ||
| list.add(new Point(point.getX() + 2, point.getY())); | ||
| } | ||
| if (point.getY() + 2 < MAP_WIDTH && !MAP[point.getX()][point.getY() + 2]) { | ||
| list.add(new Point(point.getX(), point.getY() + 2)); | ||
| } | ||
| if (0 <= point.getY() - 2 && !MAP[point.getX()][point.getY() - 2]) { | ||
| list.add(new Point(point.getX(), point.getY() - 2)); | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| /* | ||
| * Добавляем точки, в которые можем пойти, в нашу очередь | ||
| * */ | ||
| public void addPoints(Point point) { | ||
| if (0 <= point.getX() - 2 && !MAP[point.getX() - 2][point.getY()]) { | ||
| queuePoint.add(new Point(point.getX() - 2, point.getY())); | ||
| } | ||
| if (point.getX() + 2 < MAP_HEIGHT && !MAP[point.getX() + 2][point.getY()]) { | ||
| queuePoint.add(new Point(point.getX() + 2, point.getY())); | ||
| } | ||
| if (point.getY() + 2 < MAP_WIDTH && !MAP[point.getX()][point.getY() + 2]) { | ||
| queuePoint.add(new Point(point.getX(), point.getY() + 2)); | ||
| } | ||
| if (0 <= point.getY() - 2 && !MAP[point.getX()][point.getY() - 2]) { | ||
| queuePoint.add(new Point(point.getX(), point.getY() - 2)); | ||
| } | ||
| } | ||
|
|
||
| //записываем наши данные в файлы | ||
| public void PrintMap() { | ||
| try { | ||
| WriteMap writeMap = new WriteMap(MAP, countBot, listOfSpawn); | ||
| writeMap.writeMap(); | ||
| } catch (IOException e) { | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import java.util.List; | ||
|
|
||
| public class InitSettings { | ||
| private int WIDTH; | ||
| private int HEIGHT; | ||
| private int CountBot; | ||
| private List<Point> ListOfSpawn; | ||
|
|
||
| public InitSettings(int WIDTH, int HEIGHT, int countBot, List<Point> listOfSpawn) { | ||
| this.WIDTH = WIDTH; | ||
| this.HEIGHT = HEIGHT; | ||
| this.CountBot = countBot; | ||
| this.ListOfSpawn = listOfSpawn; | ||
| } | ||
|
|
||
|
|
||
| //задаем радиус видимости в зависимости от количества ботов | ||
| public int getViewRadius() { | ||
| if ( CountBot <= 4) { | ||
| return 4; | ||
| } else if (CountBot <= 8) { | ||
| return 7; | ||
| } else if (CountBot <= 12) { | ||
| return 10; | ||
| }else { | ||
| return 13; | ||
| } | ||
| } | ||
|
|
||
| //задаем радиус майнинга монет в зависимости от количества ботов | ||
| public int getMiningRadius() { | ||
| if ( CountBot <= 4) { | ||
| return 2; | ||
| } else if (CountBot <= 8) { | ||
| return 3; | ||
| } else { | ||
| return 5; | ||
| } | ||
| } | ||
| //задаем радиус атаки в зависимости от количества ботов | ||
| public int getAtackRadius() { | ||
| if ( CountBot <= 4) { | ||
| return 2; | ||
| } else if (CountBot <= 8) { | ||
| return 3; | ||
| } else { | ||
| return 5; | ||
| } | ||
| } | ||
|
|
||
| public int getWIDTH() { | ||
| return WIDTH; | ||
| } | ||
|
|
||
| public int getHEIGHT() { | ||
| return HEIGHT; | ||
| } | ||
| public List<Point> getListOfSpawn(){ | ||
| return ListOfSpawn; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import java.util.Set; | ||
|
|
||
| public class Point { | ||
| private int x; | ||
| private int y; | ||
|
|
||
| public Point(int x, int y) { | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| public int getX() { | ||
| return x; | ||
| } | ||
|
|
||
| public int getY() { | ||
| return y; | ||
| } | ||
|
|
||
| public String toString() { | ||
| return x + " " + y; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.io.Writer; | ||
| import java.util.List; | ||
|
|
||
| public class WriteMap { | ||
| private boolean[][] MAP; | ||
| private int CountBot; | ||
| private List<Point> ListOfSpawn; | ||
|
|
||
| public WriteMap(boolean[][] MAP,int countBot,List<Point> listOfSpawn) { | ||
| this.MAP = MAP; | ||
| CountBot=countBot; | ||
| ListOfSpawn=listOfSpawn; | ||
| } | ||
|
|
||
|
|
||
| public void writeMap() throws IOException { | ||
| InitSettings settings=new InitSettings(MAP.length,MAP[0].length,CountBot,ListOfSpawn); | ||
| String nameOfFile="maze_"+CountBot+"_6.map"; | ||
| Writer writerOnTxt=new FileWriter("/Users/alekseyzhizhin/Documents/GitHub/hypernull/CreateMap/src/main/java/maze/"+nameOfFile); | ||
| writerOnTxt.write("map_size "+settings.getWIDTH()+" "+settings.getHEIGHT()+"\n"); | ||
| writerOnTxt.write("view_radius "+settings.getViewRadius()+"\n"); | ||
| writerOnTxt.write("mining_radius "+settings.getMiningRadius()+"\n"); | ||
| writerOnTxt.write("attack_radius "+settings.getAtackRadius()+"\n"); | ||
|
|
||
| for (int i = 0; i < MAP.length; i++) { | ||
| for (int j = 0; j < MAP[0].length; j++) { | ||
| if (MAP[i][j]) { | ||
| writerOnTxt.write("Block " + i + " " + j + "\n"); | ||
| }else { | ||
| } | ||
| } | ||
| } | ||
| for (int i=0;i<ListOfSpawn.size();i++){ | ||
| writerOnTxt.write("spawn_position "+ListOfSpawn.get(i).getX()+" "+ListOfSpawn.get(i).getY()+"\n"); | ||
| } | ||
| writerOnTxt.flush(); | ||
| writerOnTxt.close(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| public class main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Карта успешно создана"); | ||
| CreateMap map=new CreateMap(180,240); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не стоит инициализировать статические поля параметрами конструктора