equipments #22

Merged
viud3133 merged 40 commits from equipments into main 2025-10-30 12:09:43 +01:00
7 changed files with 274 additions and 0 deletions
Showing only changes of commit ba72342b61 - Show all commits

1
.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
World.java

View 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;
}
}

23
src/main/java/Tile.java Normal file
View File

@@ -0,0 +1,23 @@
public class Tile {
private String tileName;
private int staminaCost;
private String tileID;
public Tile(String tileName, int staminaCost, String tileID) {
this.tileName = tileName;
this.staminaCost = staminaCost;
this.tileID = tileID;
}
public void setStaminaCost(int newStaminaCost) {
staminaCost = newStaminaCost;
}
public String getName() {
return tileName;
}
public int getStaminaCost() {
return staminaCost;
}
public String getTileID() {
return tileID;
}
}

97
src/main/java/World.java Normal file
View File

@@ -0,0 +1,97 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class World {
private HashMap<Position, Tile> tileMap = new HashMap<>();
private HashMap<Entity, Position> entityMap = new HashMap<>();
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 void addTile(Tile tile, Position pos) {
tileMap.put(pos, tile);
}
/*
M in array is monster
C in array is chest
X in array is player.
*/
public void printMap() {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.print(map[i][j]);
}
}
}
public String[][] getMap() {
return map;
}
public HashMap<Entity, Position> getEntityList() {
return entityMap;
}
public HashMap<Position, Tile> getTileMap() {
return tileMap;
}
public Tile getTileAtPosition(Position pos) {
return tileMap.get(pos);
}
public Entity getEntityAtPosition(Position position) {
for (HashMap.Entry<Entity, Position> entry : entityMap.entrySet()) {
Position pos = entry.getValue();
Entity e = entry.getKey();
if (pos.equals(position)) {
return e;
}
}
return null;
}
public void changePosition(Entity Character, Position newPosition) {
entityMap.put(Character, newPosition);
}
}

View 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));
}
}

View File

@@ -0,0 +1,34 @@
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class TileTest {
private final String TILE_NAME = "Name test";
private final int STAMINA_COST = 1;
private final String ID_STRING = "Mountain test";
@Test
void getTileNameTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, ID_STRING);
String tileNameTwo = tileTest.getName();
assertEquals(TILE_NAME, tileNameTwo);
}
@Test
void getStaminaCostTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, ID_STRING);
assertEquals(STAMINA_COST, tileTest.getStaminaCost());
}
@Test
void printTileStringTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, ID_STRING);
assertEquals(ID_STRING, tileTest.getTileID());
}
@Test
void setStaminaCostTest() {
Tile tileTest = new Tile(TILE_NAME, STAMINA_COST, ID_STRING);
tileTest.setStaminaCost(10);
int newStaminaCost = tileTest.getStaminaCost();
assertEquals(10, newStaminaCost);
}
}

View File

@@ -0,0 +1,40 @@
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import static org.junit.jupiter.api.Assertions.*;
class WorldTest {
@Test
void addEntityToMapTest() {
World test = new World(5, 5);
Entity character = new Player("Name");
character.setPosition(new Position(2, 2));
test.addEntityToMap(character);
}
@Test
void getTileAtPositionTest() {
World test = new World(5, 5);
HashMap<Integer, Tile> tileMap = new HashMap<>();
}
@Test
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], "@");
}
}