Skip to content

Commit 17a72be

Browse files
authored
Add map generating source code (#4)
* UPDATE: Added new generated maps * UPDATE: Fixed maps with unassigned attack radius * Add map generating code sources
1 parent d223189 commit 17a72be

File tree

8 files changed

+279
-0
lines changed

8 files changed

+279
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package MapGenerator;
2+
3+
public class CellAutomataStrategy implements Strategy {
4+
5+
public void makeLikeNeighbours(Map map, int y, int x) {
6+
int difference = 0; //насколько больше стенок, чем полов
7+
8+
int height = map.getHeight();
9+
int width = map.getWidth();
10+
11+
for(int dx=-1; dx<2; dx++) {
12+
for (int dy=-1; dy < 2; dy++) {
13+
14+
if(dx==0 && dy==0) continue;
15+
16+
int Y=0, X=0;
17+
if(0 <= y+dy && y+dy < height) Y = y+dy;
18+
if(0 <= x+dx && x+dx < width) X = x+dx;
19+
if(y+dy < 0 ) Y = height+y+dy;
20+
if(y+dy >= height) Y = y+dy-height;
21+
if(x+dx < 0) X = width+x+dx;
22+
if(x+dx >= width) X = x+dx-width;
23+
24+
difference += checkNeighbour(map, Y,X);
25+
}
26+
}
27+
if(difference >= -1 )
28+
map.set(y, x, Parameter.WALL);
29+
else
30+
map.set(y, x, Parameter.FLOOR);
31+
}
32+
33+
public int checkNeighbour(Map map, int y, int x) {
34+
if(map.get(y,x) == Parameter.FLOOR)
35+
return -1;
36+
if(map.get(y,x) == Parameter.WALL)
37+
return +1;
38+
else return 0;
39+
}
40+
41+
public void mirrorCell(Map map, int h, int w) {
42+
Boolean value = map.get(h, w);
43+
map.set(map.getHeight() - 1 - h, w, value);
44+
map.set(h, map.getWidth() - 1 - w, value);
45+
map.set(map.getHeight() - 1 - h, map.getWidth() - 1 - w, value);
46+
}
47+
48+
@Override
49+
public Map generateMap(int originalHeight, int originalWidth) {
50+
Map resultMap = new Map(originalHeight, originalWidth);
51+
52+
int height = originalHeight/2 + originalHeight%2;
53+
int width = originalWidth/2 + originalWidth%2;
54+
55+
for(int k=0; k< Parameter.LAYER_DEPTH; ++k) { //наложение сгенерированных карт друг на друга
56+
Map map = new Map(originalHeight, originalWidth);
57+
for (int initials = 0; initials < height * width * Parameter.INITIAL_PERCENTAGE / 100.0; initials++) {
58+
int y = (int) (Math.random() * (height-2)+1);
59+
int x = (int) (Math.random() * (width-2)+1);
60+
map.set(y, x, Parameter.WALL);
61+
}
62+
for (int i = 0; i < Parameter.AUTOMATA_DEPTH; ++i) { // глубина просчета клеток автоматом
63+
for (int h = height - 1; h >= 0; h--) {
64+
for (int w = 0; w < width; w++) {
65+
makeLikeNeighbours(map, h, w);
66+
mirrorCell(map, h, w);
67+
}
68+
}
69+
}
70+
resultMap.impose(map);
71+
}
72+
return resultMap;
73+
}
74+
}

MapGenerator/Main.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package MapGenerator;
2+
3+
import java.io.IOException;
4+
import java.util.Scanner;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) throws IOException {
9+
while(true) {
10+
11+
MapGenerator mapgen = new MapGeneratorImpl();
12+
mapgen.setStrategy(new CellAutomataStrategy());
13+
14+
Scanner scan = new Scanner(System.in);
15+
16+
int height = scan.nextInt(), width = scan.nextInt();
17+
Map map = mapgen.generateMap(height, width);
18+
map.print();
19+
20+
if (scan.nextInt() == 1)
21+
Saver.save(map);
22+
}
23+
}
24+
}

MapGenerator/Map.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package MapGenerator;
2+
3+
import java.util.ArrayList;
4+
5+
public class Map {
6+
private Boolean[][] body;
7+
private ArrayList<ArrayList<Integer>> spawnpoints = new ArrayList<ArrayList<Integer> >();
8+
private int height;
9+
private int width;
10+
11+
Map(int height, int width) {
12+
this.height = height;
13+
this.width = width;
14+
body = new Boolean[height][width];
15+
for(int h=0; h<height; h++) {
16+
for (int w = 0; w < width; w++) {
17+
body[h][w] = Parameter.FLOOR;
18+
}
19+
}
20+
}
21+
22+
public Boolean get(int height, int width) {
23+
return body[height][width];
24+
}
25+
public Character getSymbol(int height, int width) {
26+
if(body[height][width] == Parameter.FLOOR)
27+
return Parameter.FLOOR_SYMBOL;
28+
else
29+
return Parameter.WALL_SYMBOL;
30+
31+
}
32+
33+
public int getHeight() {
34+
return height;
35+
}
36+
37+
public int getWidth() {
38+
return width;
39+
}
40+
41+
public void set(int height, int width, Boolean value) {
42+
body[height][width] = value;
43+
}
44+
45+
public ArrayList<ArrayList<Integer>> getSpawnpoints() {
46+
return this.spawnpoints;
47+
}
48+
49+
public void setSpawnpoint(int y, int x) {
50+
ArrayList<Integer> point = new ArrayList<>();
51+
point.add(y);
52+
point.add(x);
53+
spawnpoints.add(point);
54+
}
55+
56+
public void print() {
57+
for(int h=height-1; h>=0; h--) {;
58+
for (int w = 0; w < width; w++) {
59+
boolean notSpawnpoint = true;
60+
for(int i=0; i<spawnpoints.size(); i++) {
61+
if(spawnpoints.get(i).get(0) == h && spawnpoints.get(i).get(1) == w) {
62+
System.out.print("O ");
63+
notSpawnpoint = false;
64+
break;
65+
}
66+
}
67+
if(notSpawnpoint) System.out.print(this.getSymbol(h,w)+" ");
68+
}
69+
System.out.println();
70+
}
71+
}
72+
73+
public boolean impose(Map map) {
74+
if(map.getHeight() != this.height && map.getWidth() != this.width)
75+
return false;
76+
for(int h=0; h < height; h++) {
77+
for(int w = 0; w < width; w++) {
78+
if(this.body[h][w] == Parameter.FLOOR && map.get(h,w) == Parameter.FLOOR)
79+
this.body[h][w] = Parameter.FLOOR;
80+
else
81+
this.body[h][w] = Parameter.WALL;
82+
}
83+
}
84+
return true;
85+
}
86+
}

MapGenerator/MapGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package MapGenerator;
2+
3+
public interface MapGenerator {
4+
5+
public MapGenerator setStrategy(Strategy strategy);
6+
public Map generateMap(int height, int width);
7+
}

MapGenerator/MapGeneratorImpl.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package MapGenerator;
2+
3+
public class MapGeneratorImpl implements MapGenerator {
4+
private Strategy strategy;
5+
6+
@Override
7+
public MapGenerator setStrategy(Strategy strategy) {
8+
this.strategy = strategy;
9+
return this;
10+
}
11+
@Override
12+
public Map generateMap(int height, int width) {
13+
Map map = strategy.generateMap(height, width);
14+
setSpawnpoints(map, Parameter.SPAWNPOINTS);
15+
return map;
16+
}
17+
18+
public void setSpawnpoints(Map map, int num) {
19+
int y, x;
20+
for(int point=0; point < num/2; point++) {
21+
do {
22+
y = (int) (Math.random() * (map.getHeight()-2)+2);
23+
x = (int) (Math.random() * (map.getWidth()-2)+2);
24+
} while(!map.get(y,x));
25+
26+
map.setSpawnpoint(y,x);
27+
map.setSpawnpoint(map.getHeight()-1-y,map.getWidth()-1-x);
28+
}
29+
}
30+
}

MapGenerator/Parameter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package MapGenerator;
2+
3+
public class Parameter {
4+
final static Character FLOOR_SYMBOL = '.';
5+
final static Boolean FLOOR = true;
6+
final static Character WALL_SYMBOL = '■';
7+
final static Boolean WALL = false;
8+
final static int INITIAL_PERCENTAGE = 35;
9+
final static int AUTOMATA_DEPTH = 1;
10+
final static int LAYER_DEPTH = 7;
11+
final static int SPAWNPOINTS = 4;
12+
}

MapGenerator/Saver.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package MapGenerator;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.util.ArrayList;
7+
8+
public class Saver {
9+
private static int id=1;
10+
11+
public static void save(Map map) throws IOException {
12+
try(BufferedWriter writer = new BufferedWriter(new FileWriter("map_2_"+String.valueOf(id)+".map"))) {
13+
14+
writer.write("map_size " + map.getHeight() + " " + map.getWidth());
15+
writer.newLine();
16+
int viewRadius = Math.min(map.getHeight(),map.getWidth())/7;
17+
writer.write("view_radius "+ viewRadius);
18+
writer.newLine();
19+
writer.write("mining_radius "+ (viewRadius-1));
20+
writer.newLine();
21+
writer.write("attack_radius "+ (viewRadius-2));
22+
writer.newLine();
23+
24+
for(int h = map.getHeight()-1; h>=0; h--) {
25+
for (int w = 0; w < map.getWidth(); w++) {
26+
if(map.get(h,w) == Parameter.WALL) {
27+
writer.write("block "+h+" "+w);
28+
writer.newLine();
29+
}
30+
}
31+
}
32+
33+
for(ArrayList<Integer> point : map.getSpawnpoints()) {
34+
writer.write("spawn_position "+point.get(0)+" "+point.get(1));
35+
writer.newLine();
36+
}
37+
38+
id++;
39+
}
40+
}
41+
}

MapGenerator/Strategy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package MapGenerator;
2+
3+
public interface Strategy {
4+
public Map generateMap(int height, int width);
5+
}

0 commit comments

Comments
 (0)