From 5b59d9f930bf4bc34a20bdc406c6926313cc1608 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 3 Mar 2023 16:08:23 -0500 Subject: [PATCH] Building --- Assignment 2/src/ca/cosc3p91/a2/Main.java | 1 + .../src/ca/cosc3p91/a2/game/GameEngine.java | 108 +++++++++++++++++- Assignment 2/src/ca/cosc3p91/a2/game/Map.java | 14 ++- .../ca/cosc3p91/a2/gameobjects/IronMine.java | 3 +- .../cosc3p91/a2/gameobjects/LumberMine.java | 3 +- .../cosc3p91/a2/gameobjects/SaulGoodMine.java | 3 +- .../src/ca/cosc3p91/a2/gameobjects/Stage.java | 4 +- .../src/ca/cosc3p91/a2/util/Util.java | 15 +++ 8 files changed, 139 insertions(+), 12 deletions(-) create mode 100644 Assignment 2/src/ca/cosc3p91/a2/util/Util.java diff --git a/Assignment 2/src/ca/cosc3p91/a2/Main.java b/Assignment 2/src/ca/cosc3p91/a2/Main.java index 6b832ae..b06e6cb 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/Main.java +++ b/Assignment 2/src/ca/cosc3p91/a2/Main.java @@ -1,6 +1,7 @@ package ca.cosc3p91.a2; import ca.cosc3p91.a2.game.GameEngine; +import ca.cosc3p91.a2.gameobjects.Stage; public class Main { diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java index 78e0603..f3ca9de 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java @@ -4,10 +4,12 @@ import ca.cosc3p91.a2.gameobjects.*; import ca.cosc3p91.a2.player.*; import ca.cosc3p91.a2.util.Print; import ca.cosc3p91.a2.util.Time; +import ca.cosc3p91.a2.util.Util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.util.Random; import java.util.Scanner; public class GameEngine implements Runnable { @@ -23,11 +25,13 @@ public class GameEngine implements Runnable { private int currentTime; + private final Random random = new Random(System.nanoTime()); + public Map map; public GameEngine() { player = new Player(); - map = generateMap(); + map = generateInitialMap(); } private void printState() { @@ -86,7 +90,70 @@ public class GameEngine implements Runnable { public Map generateMap() { Map initialMap = generateInitialMap(); - initialMap.getTownHall().upgrade(VillageHallStages.villageStages[this.map.getTownHall().getLevel()]); + // generate a similar town hall + int levelChange = random.nextInt(2) - 1; + int nextLevel = this.map.getTownHall().getLevel() + levelChange; + // only need to change if the new village level is higher than initial + if (nextLevel > 0) + initialMap.getTownHall().upgrade(VillageHallStages.villageStages[nextLevel]); + + int buildingCount = this.map.contains.size(); + + int saulGoodMines = 0; + int ironMines = 0; + int woodMines = 0; + int archerTowers = 0; + int cannons = 0; + + // count buildings in our map + for (Building b : this.map.contains){ + if (b instanceof SaulGoodMine) + saulGoodMines++; + else if (b instanceof IronMine) + ironMines++; + else if (b instanceof LumberMine) + woodMines++; + else if (b instanceof ArcherTower) + archerTowers++; + else if (b instanceof Cannon) + cannons++; + } + + // variate + saulGoodMines += random.nextInt();; + ironMines += random.nextInt(); + woodMines += random.nextInt(); + archerTowers += random.nextInt(); + cannons += random.nextInt(); + + // generate a map with a similar number of buildings + for (int i = 0; i < Math.max(buildingCount + random.nextInt(5) - 2, 1); i++) { + int selection = random.nextInt(5); + // select a random building. Doing it this way because if we build based on buildings we have + // then the maps will be VERY boring as they would be all the same + switch (selection) { + case 0: + initialMap.contains.add(new LumberMine(ResourceStages.woodStages[random.nextInt(ResourceStages.woodStages.length)])); + break; + case 1: + if (ironMines > 0) + initialMap.contains.add(new IronMine(ResourceStages.ironStages[random.nextInt(ResourceStages.ironStages.length)])); + break; + case 2: + if (saulGoodMines > 0) + initialMap.contains.add(new SaulGoodMine(ResourceStages.goldStages[random.nextInt(ResourceStages.goldStages.length)])); + break; + case 3: + initialMap.contains.add(new ArcherTower()); + break; + case 4: + initialMap.contains.add(new Cannon()); + break; + default: + break; + } + } + return initialMap; } @@ -113,14 +180,28 @@ public class GameEngine implements Runnable { //System.out.println("Updating"); try { if (rd.ready()) { - char in = sc.next().charAt(0); - switch (in) { + String in = sc.nextLine(); + String[] args = in.split(" "); + System.out.println("Your Input: "); + System.out.println("\t->" + in); + switch (in.charAt(0)) { case 'p': printState(); break; case 'q': System.exit(0); break; + case 'b': + if (args.length < 2) { + System.err.println("Args must include type!"); + } else { + Building type = determineType(args[1]); + if (type == null) + System.err.println("Args are not a valid building!"); + else + map.build(new Tile(), type); + } + break; default: break; } @@ -130,4 +211,23 @@ public class GameEngine implements Runnable { } } } + + private static Building determineType(String building){ + building = building.toLowerCase(); + char c = ' '; + if (building.trim().length() == 1) + c = building.charAt(0); + if (building.contains("gold") || building.contains("good") || c == 'g') { + return new SaulGoodMine(ResourceStages.goldStages[0]); + } else if (building.contains("iron") || c == 'i') { + return new IronMine(ResourceStages.ironStages[0]); + } else if (building.contains("wood") || building.contains("lumber") || c == 'w' || c == 'l') { + return new LumberMine(ResourceStages.woodStages[0]); + } else if (building.contains("archer") || c == 'a') { + return new ArcherTower(); + } else if (building.contains("can") || c == 'c'){ + return new Cannon(); + } + return null; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/Map.java b/Assignment 2/src/ca/cosc3p91/a2/game/Map.java index 028469d..97d9d9f 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/Map.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/Map.java @@ -34,7 +34,19 @@ public class Map { } public void build(Tile t, Building b) { - contains.add(b); + int goldCost = b.getStage().getCost(SaulGoodMine.resource); + int ironCost = b.getStage().getCost(SaulGoodMine.resource); + int woodCost = b.getStage().getCost(SaulGoodMine.resource); + CasaDeNarino hall = getTownHall(); + if (hall.getCurrentGold() >= goldCost && hall.getCurrentIron() >= ironCost && hall.getCurrentWood() >= woodCost) { + if(!hall.addGold(-goldCost)) + throw new RuntimeException("Unable to subtract gold despite valid check!"); + if(!hall.addIron(-ironCost)) + throw new RuntimeException("Unable to subtract iron despite valid check!"); + if(!hall.addWood(-woodCost)) + throw new RuntimeException("Unable to subtract wood despite valid check!"); + contains.add(b); + } } public int getGuardTime() { diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java index b9a6859..9ee9473 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java @@ -4,8 +4,7 @@ public class IronMine extends ResourceBuilding { public static String resource = "iron"; - public IronMine(int lvl, ResourceStage baseStage) { - setLevel(lvl); + public IronMine(ResourceStage baseStage) { upgrade(baseStage); } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java index 0fd2086..113c52e 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java @@ -4,8 +4,7 @@ public class LumberMine extends ResourceBuilding { public static String resource = "wood"; - public LumberMine(int lvl, ResourceStage baseStage) { - setLevel(lvl); + public LumberMine(ResourceStage baseStage) { upgrade(baseStage); } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java index bb1e088..eab7c70 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java @@ -4,8 +4,7 @@ public class SaulGoodMine extends ResourceBuilding { public static String resource = "gold"; - public SaulGoodMine(int lvl, ResourceStage baseStage) { - setLevel(lvl); + public SaulGoodMine(ResourceStage baseStage) { upgrade(baseStage); } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Stage.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Stage.java index 3e5973b..60fe1c6 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Stage.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Stage.java @@ -2,7 +2,9 @@ package ca.cosc3p91.a2.gameobjects; import ca.cosc3p91.a2.util.Time; -abstract class Stage { +import java.util.HashMap; + +public abstract class Stage { protected int dHealth; diff --git a/Assignment 2/src/ca/cosc3p91/a2/util/Util.java b/Assignment 2/src/ca/cosc3p91/a2/util/Util.java new file mode 100644 index 0000000..e909ac0 --- /dev/null +++ b/Assignment 2/src/ca/cosc3p91/a2/util/Util.java @@ -0,0 +1,15 @@ +package ca.cosc3p91.a2.util; + +import java.util.Random; + +public class Util { + + public static int randomInt(int min, int max, Random random) { + return random.nextInt(max - min) + min; + } + + public static int clamp(int min, int max, int i){ + return Math.min(Math.max(i, min), max); + } + +}