equipments #22
46
src/main/java/MapGenerator.java
Normal file
46
src/main/java/MapGenerator.java
Normal file
@@ -0,0 +1,46 @@
|
||||
import java.util.Random;
|
||||
public class MapGenerator {
|
||||
private int x;
|
||||
private int y;
|
||||
private World gameWorld;
|
||||
|
||||
public MapGenerator(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
if (x <= 0 || y <= 0) {
|
||||
throw new IllegalArgumentException("X and Y coordinates must be positive");
|
||||
}
|
||||
this.gameWorld = new World(x, y);
|
||||
}
|
||||
|
||||
public void randomWorldGeneration() {
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < x; i++) {
|
||||
for (int j = 0; j < y; j++) {
|
||||
int tileDice = rand.nextInt(4);
|
||||
if (tileDice == 0) {
|
||||
Tile newTile = new Tile("Mountain", 3, "Future development");
|
||||
gameWorld.addTile(newTile, new Position(i, j));
|
||||
} else if (tileDice == 1) {
|
||||
Tile newTile = new Tile("plain", 1, "Future development");
|
||||
gameWorld.addTile(newTile, new Position(i, j));
|
||||
} else if (tileDice == 2) {
|
||||
Tile newTile = new Tile("Lava", 2, "Future development");
|
||||
gameWorld.addTile(newTile, new Position(i, j));
|
||||
} else if (tileDice == 3) {
|
||||
Tile newTile = new Tile("Water", 4, "Future development");
|
||||
gameWorld.addTile(newTile, new Position(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addTile(String name, int staminaCost, String description, Position position) {
|
||||
Tile newTile = new Tile(name, staminaCost, description);
|
||||
gameWorld.addTile(newTile, position);
|
||||
}
|
||||
|
||||
public World getWorld() {
|
||||
return gameWorld;
|
||||
}
|
||||
}
|
||||
@@ -3,38 +3,54 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
public class World {
|
||||
// Används för att printa mappen.
|
||||
private String[][] map = {
|
||||
{"@","@","@","@","@","@","@","@","@","@","@"},
|
||||
{"@","X","|"," ", "|", " ","|"," ","|"," ","@"},
|
||||
{"@"," ","|"," ", "|", " ","|"," ","|"," ","@"},
|
||||
{"@"," ","|"," ", "|", " ","|"," ","|"," ","@"},
|
||||
{"@"," ","|"," ", "|", " ","|"," ","|"," ","@"},
|
||||
{"@"," ","|"," ", "|", " ","|"," ","|"," ","@"},
|
||||
{"@","@","@","@","@","@","@","@","@","@","@"}
|
||||
};
|
||||
HashMap<Integer, Tile> tileMap = new HashMap<>();
|
||||
|
||||
private HashMap<Position, Tile> tileMap = new HashMap<>();
|
||||
private HashMap<Entity, Position> entityMap = new HashMap<>();
|
||||
public final int TILE_SIZE = 25;
|
||||
private String[][] map;
|
||||
|
||||
public World(int x, int y) {
|
||||
this.map = new String[(x * 2) + 1][y + 2];
|
||||
addWorldToMap();
|
||||
}
|
||||
|
||||
private void addWorldToMap() {
|
||||
for (int i = 0; i < map.length; i++) {
|
||||
for (int j = 0; j < map[i].length; j++) {
|
||||
if (i == 0 || j == 0 || i == map.length || j == map[j].length) {
|
||||
map[i][j] = "@";
|
||||
} else if (j % 2 == 1) {
|
||||
map[i][j] = " ";
|
||||
} else {
|
||||
map[i][j] = "|";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void printWorld() {
|
||||
for (Entity e : entityMap.keySet()) {
|
||||
Position pos = entityMap.get(e);
|
||||
if (e instanceof Player) {
|
||||
map[pos.x()][pos.y()] = "X";
|
||||
} /* else if (e instanceof Monster) {
|
||||
map[pos.x()][pos.y()] = "M";
|
||||
*/
|
||||
}
|
||||
for (int i = 0; i < map.length; i++) {
|
||||
System.out.println();
|
||||
for (int j = 0; j < map[i].length; j++) {
|
||||
System.out.print(map[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void addEntityToMap(Entity e) {
|
||||
entityMap.put(e, e.getPosition());
|
||||
}
|
||||
|
||||
public World() {
|
||||
autoGenerateTiles();
|
||||
}
|
||||
|
||||
private void autoGenerateTiles() {
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < TILE_SIZE; i++) {
|
||||
int x = rand.nextInt(4);
|
||||
}
|
||||
}
|
||||
|
||||
public void addTile(Tile tile, int xCoordinate, int yCoordinate) {
|
||||
int key = xCoordinate * 10 + yCoordinate;
|
||||
tileMap.put(key, tile);
|
||||
public void addTile(Tile tile, Position pos) {
|
||||
tileMap.put(pos, tile);
|
||||
}
|
||||
/*
|
||||
M in array is monster
|
||||
@@ -56,10 +72,12 @@ public class World {
|
||||
public HashMap<Entity, Position> getEntityList() {
|
||||
return entityMap;
|
||||
}
|
||||
public HashMap<Position, Tile> getTileMap() {
|
||||
return tileMap;
|
||||
}
|
||||
|
||||
public Tile getTileAtPosition(int xCoordinate, int yCoordinate) {
|
||||
int tileKey = xCoordinate * 10 + yCoordinate;
|
||||
return tileMap.get(tileKey);
|
||||
public Tile getTileAtPosition(Position pos) {
|
||||
return tileMap.get(pos);
|
||||
}
|
||||
|
||||
public Entity getEntityAtPosition(Position position) {
|
||||
|
||||
33
src/test/java/MapGeneratorTest.java
Normal file
33
src/test/java/MapGeneratorTest.java
Normal file
@@ -0,0 +1,33 @@
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MapGeneratorTest {
|
||||
private final int X_FIVE = 5;
|
||||
private final int Y_FIVE = 5;
|
||||
@Test
|
||||
void mapBeingGeneratedTest() {
|
||||
MapGenerator mapGenerate = new MapGenerator(X_FIVE, Y_FIVE);
|
||||
mapGenerate.randomWorldGeneration();
|
||||
World world = mapGenerate.getWorld();
|
||||
HashMap<Position, Tile> map = world.getTileMap();
|
||||
assertEquals(map.size(), X_FIVE * Y_FIVE);
|
||||
}
|
||||
// Valid klass
|
||||
@Test
|
||||
void addTileAtPositionTest() {
|
||||
MapGenerator mapGenerate = new MapGenerator(X_FIVE, Y_FIVE);
|
||||
mapGenerate.addTile("Test", 10, "test description", new Position(2, 2));
|
||||
World testWorld = mapGenerate.getWorld();
|
||||
assertTrue(testWorld.getTileAtPosition(new Position(2, 2)) instanceof Tile);
|
||||
assertFalse(testWorld.getTileAtPosition(new Position(2, 1)) instanceof Tile);
|
||||
}
|
||||
// Invalid klass
|
||||
@Test
|
||||
void negativeCoordinatesTest() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new MapGenerator(-1, 5));
|
||||
assertThrows(IllegalArgumentException.class, () -> new MapGenerator(5, 0));
|
||||
}
|
||||
}
|
||||
@@ -5,35 +5,36 @@ import java.util.HashMap;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
class WorldTest {
|
||||
final int TILE_SIZE = 25;
|
||||
@Test
|
||||
void addTileToMapTest() {
|
||||
World test = new World();
|
||||
Tile tile = new Tile("test", 3, "test");
|
||||
int xCoordinate = 1;
|
||||
int yCoordinate = 1;
|
||||
test.addTile(tile, xCoordinate, yCoordinate);
|
||||
Tile tileTest = test.getTileAtPosition(1, 1);
|
||||
assertEquals(tile, tileTest);
|
||||
}
|
||||
@Test
|
||||
void addEntityToMapTest() {
|
||||
World test = new World();
|
||||
World test = new World(5, 5);
|
||||
Entity character = new Player("Name");
|
||||
character.setPosition(new Position(2, 2));
|
||||
test.addEntityToMap(character);
|
||||
}
|
||||
@Test
|
||||
void randomTileGeneratorTest() {
|
||||
World test = new World();
|
||||
void getTileAtPositionTest() {
|
||||
World test = new World(5, 5);
|
||||
HashMap<Integer, Tile> tileMap = new HashMap<>();
|
||||
int tileSize = tileMap.size();
|
||||
assertEquals(TILE_SIZE, tileSize);
|
||||
}
|
||||
@Test
|
||||
void getTileAtPositionTest() {
|
||||
World test = new World();
|
||||
HashMap<Integer, Tile> tileMap = new HashMap<>();
|
||||
void worldCreationTest() {
|
||||
World test = new World(5, 5);
|
||||
String[][] mapTest = test.getMap();
|
||||
int counter = 0;
|
||||
for (int i = 0; i < mapTest.length; i++) {
|
||||
for (int j = 0; j < mapTest[i].length; j++) {
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
assertEquals(counter, 5 * 5);
|
||||
}
|
||||
@Test
|
||||
void mapCreationTest() {
|
||||
World test = new World(5, 5);
|
||||
String[][] mapTest = test.getMap();
|
||||
assertEquals(mapTest[1][1], " ");
|
||||
assertEquals(mapTest[1][2], "|");
|
||||
assertEquals(mapTest[0][0], "@");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user