diff --git a/Assignment 3/COSC3P91-A2_Description_Brett&Michael.pdf b/Assignment 3/COSC3P91-A2_Description_Brett&Michael.pdf new file mode 100644 index 0000000..4747bef Binary files /dev/null and b/Assignment 3/COSC3P91-A2_Description_Brett&Michael.pdf differ diff --git a/Assignment 3/assignment2.pdf b/Assignment 3/assignment2.pdf new file mode 100644 index 0000000..7ff37aa Binary files /dev/null and b/Assignment 3/assignment2.pdf differ diff --git a/Assignment 3/src/ca/cosc3p91/a2/Main.java b/Assignment 3/src/ca/cosc3p91/a2/Main.java new file mode 100644 index 0000000..b06e6cb --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/Main.java @@ -0,0 +1,12 @@ +package ca.cosc3p91.a2; + +import ca.cosc3p91.a2.game.GameEngine; +import ca.cosc3p91.a2.gameobjects.Stage; + +public class Main { + + public static void main(String[] args) { + new GameEngine().run(); + } + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java b/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java new file mode 100644 index 0000000..19457e3 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java @@ -0,0 +1,352 @@ +package ca.cosc3p91.a2.game; + +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 { + + public static final double GOLD_FACTOR = 5; + public static final double IRON_FACTOR = 1; + public static final double WOOD_FACTOR = 0.1; + + private Player player; + boolean running = true; + + private float pillageFactor = 0.5f; + + private int currentTime; + + private final Random random = new Random(System.nanoTime()); + + public Map map; + + public GameEngine() { + player = new Player(); + map = generateInitialMap(); + } + + private void printState(Map map, String displayName) { + Print resourcesPrinter = new Print(displayName, 2); + + resourcesPrinter.addColumn(new Print.Column("Resource Type")); + resourcesPrinter.addColumn(new Print.Column("Max")); + resourcesPrinter.addColumn(new Print.Column("Amount")); + + resourcesPrinter.addRow(new Print.Row( + "Wood", + Integer.toString(map.getTownHall().getWoodCapacity()), + Integer.toString(map.getTownHall().getCurrentWood()))); + + resourcesPrinter.addRow(new Print.Row( + "Iron", + Integer.toString(map.getTownHall().getIronCapacity()), + Integer.toString(map.getTownHall().getCurrentIron()))); + + resourcesPrinter.addRow(new Print.Row( + "Gold", + Integer.toString(map.getTownHall().getGoldCapacity()), + Integer.toString(map.getTownHall().getCurrentGold()))); + + Print.print(resourcesPrinter.createTable(true, false, true)); + + Print buildingPrinter = new Print("Village Buildings", 2, resourcesPrinter.getWidth()); + buildingPrinter.addColumn(new Print.Column("Name")); + buildingPrinter.addColumn(new Print.Column("Level")); + buildingPrinter.addColumn(new Print.Column("Health")); + + for (Building b : map.contains) + buildingPrinter.addRow(new Print.Row(b.getClass().getSimpleName(), + Integer.toString(b.getLevel() + 1), + Integer.toString(b.getHealth()))); + + Print.print(buildingPrinter.createTable(true, false, true)); + + Print inhabs = new Print("Village Inhabitants", 2, buildingPrinter.getWidth()); + inhabs.addColumn(new Print.Column("Name")); + inhabs.addColumn(new Print.Column("Level")); + + for (Inhabitant i : map.inhabitants) + inhabs.addRow(new Print.Row(i.getClass().getSimpleName(), Integer.toString(i.getLevel() + 1))); + + Print.print(inhabs.createTable(true, true, true)); + } + + private void printMenuOptions() { + System.out.println("\n~ Player Options:\n" + + "1. Build {command: '1 '}\n" + + "2. Train inhabitants {command: '2 '}\n"+ + "3. Upgrade {command: '3 i'} / {command: '3 b'}\n"+ + "4. Explore\n"+ + "5. Print Village Stats\n"+ + "6. Quit\n" + + "7. Attack last explored\n"); + } + + public void attackVillage(Map map) { + int defenseiveCounter = 1; + int inhabCounter = 0; + for (Building b : map.contains) + if (b instanceof DefenseBuilding) + defenseiveCounter++; + for (Inhabitant i : map.inhabitants) + if (i instanceof Infantry) + inhabCounter++; + pillageFactor = (float) inhabCounter / (float) defenseiveCounter; + if (pillageFactor < 0) + pillageFactor = 0; + if (pillageFactor > 1) + pillageFactor = 1; + this.map.getTownHall().addWood((int) (map.getTownHall().getCurrentWood() * pillageFactor)); + this.map.getTownHall().addIron((int) (map.getTownHall().getCurrentIron() * pillageFactor)); + this.map.getTownHall().addGold((int) (map.getTownHall().getCurrentGold() * pillageFactor)); + } + + private Map generateInitialMap(){ + return new Map(new CasaDeNarino(1, VillageHallStages.villageStages[0]), 30); + } + + public Map generateMap() { + Map initialMap = generateInitialMap(); + + CasaDeNarino hall = initialMap.getTownHall(); + + // 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) + hall.upgrade(VillageHallStages.villageStages[nextLevel]); + + hall.addWood(this.map.getTownHall().getCurrentWood() + random.nextInt(500) - 150); + hall.addIron(this.map.getTownHall().getCurrentIron() + random.nextInt(500) - 150); + hall.addGold(this.map.getTownHall().getCurrentGold() + random.nextInt(500) - 150); + + 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; + } + + public int getScore(Map map) { + CasaDeNarino hall = map.getTownHall(); + int score = (int)(hall.getCurrentGold() * GOLD_FACTOR + hall.getCurrentIron() * IRON_FACTOR + hall.getCurrentWood() * WOOD_FACTOR); + score += map.contains.size(); + score += map.inhabitants.size(); + return score; + } + + @Override + public void run() { + BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); + Scanner sc = new Scanner(rd); + printState(this.map,"Current Village State"); + printMenuOptions(); + System.out.println(); + Map exploringMap = null; + boolean deleteMyHeart = true; + while (running) { + for (Building b : this.map.contains){ + if ((b instanceof ResourceBuilding)) { + ((ResourceBuilding) b).update(this.map.getTownHall()); + } + } + try { + if (rd.ready()) { + String in = sc.nextLine(); + String[] args = in.split(" "); + System.out.println("Your Input: "); + System.out.println("\t->" + in + '\n'); + // reset the map if they aren't exploring + if (in.charAt(0) != '4') + deleteMyHeart = true; + switch (in.charAt(0)) { + case '1': + if (args.length < 2) { + System.err.println("Args must include type!"); + } else { + Building type = determineBuildingType(args[1]); + if (type == null) + System.err.println("Args are not a valid building!"); + else if (this.map.build(new Tile(), type) ) { + System.out.println(type.getClass().getSimpleName()+" successfully built\n"); + } else + System.out.println("Missing resources to build "+type.getClass().getSimpleName()); + } + break; + case '2': + if (args.length < 2) { + System.err.println("Args must include type!"); + } else { + Inhabitant type = determineInhabitantType(args[1]); + if (type == null) + System.err.println("Args are not a valid inhabitant!"); + else if (this.map.train(type) ) { + System.out.println("successfully trained a(n) "+type.getClass().getSimpleName()); + } else System.out.println("Missing gold to train "+type.getClass().getSimpleName()); + } + break; + case '3': + if (args.length < 2) { + System.err.println("Args must include type!"); + } else { + int unitIndex = Integer.parseInt(args[1].substring(1)); + + if (unitIndex < 0) { + System.err.println("Invalid Index"); + break; + } + + if (args[1].contains("i") && (unitIndex < map.inhabitants.size()) ) { + if ( map.upgradeInhabitant(unitIndex) ) { + System.out.println("successfully upgraded a(n) "+map.inhabitants.get(unitIndex).getClass().getSimpleName()); + } else System.out.println("Missing Resources to upgrade "+map.inhabitants.get(unitIndex).getClass().getSimpleName()); + } else if (args[1].contains("b") && (unitIndex < map.contains.size()) ) { + if ( map.upgradeBuilding(unitIndex) ) { + System.out.println("successfully upgraded a(n) "+map.contains.get(unitIndex).getClass().getSimpleName()); + } else System.out.println("Missing Resources to upgrade "+map.contains.get(unitIndex).getClass().getSimpleName()); + } else { + System.err.println("Args are not a valid unit!"); + } + } + break; + case '4': + deleteMyHeart = false; + exploringMap = generateMap(); + printState(exploringMap, "Other Village"); + break; + case '7': + if (exploringMap != null) + attackVillage(exploringMap); + else + System.out.println("Error: Explored map is null. Have you explored last command?"); + break; + case '5': + printState(this.map,"Home Village"); + break; + case '6': + System.exit(0); + break; + default: + break; + } + printMenuOptions(); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + if (deleteMyHeart) + exploringMap = null; + } + } + + private static char determineChar(String str){ + char c = ' '; + if (str.trim().length() == 1) + c = str.charAt(0); + return c; + } + + private static Building determineBuildingType(String argument){ + argument = argument.toLowerCase(); + char c = determineChar(argument); + + if (argument.contains("gold") || argument.contains("good") || c == 'g') { + return new SaulGoodMine(ResourceStages.goldStages[0]); + } else if (argument.contains("iron") || c == 'i') { + return new IronMine(ResourceStages.ironStages[0]); + } else if (argument.contains("wood") || argument.contains("lumber") || c == 'w' || c == 'l') { + return new LumberMine(ResourceStages.woodStages[0]); + } else if (argument.contains("archer") || c == 'a') { + return new ArcherTower(); + } else if (argument.contains("can") || c == 'c') { + return new Cannon(); + } + + return null; + } + + private static Inhabitant determineInhabitantType(String argument) { + argument = argument.toLowerCase(); + char c = determineChar(argument); + + if (argument.contains("soldier") || c == 's') { + return new Soldier(); + } else if (argument.contains("knight") || c == 'k') { + return new Knight(); + } else if (argument.contains("work") || c == 'w') { + return new Worker(); + } else if (argument.contains("collect") || c == 'c') { + return new Collector(); + } else if (argument.contains("cat")) { + return new Catapult(); + } else if (argument.contains("arch") || c == 'a') { + return new Archer(); + } + + return null; + } + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/game/Map.java b/Assignment 3/src/ca/cosc3p91/a2/game/Map.java new file mode 100644 index 0000000..2a7b664 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/game/Map.java @@ -0,0 +1,142 @@ +package ca.cosc3p91.a2.game; + +import ca.cosc3p91.a2.gameobjects.*; + +import java.util.ArrayList; +import java.util.List; + +public class Map { + + static int MAXSIZE = 50; + + private CasaDeNarino townHall; + + private int guardTime; + + public List contains; + + public List inhabitants; + + public Map(CasaDeNarino casaDeNarino, int gTime) { + contains = new ArrayList<>(); + inhabitants = new ArrayList<>(); + this.townHall = casaDeNarino; + this.contains.add(casaDeNarino); + this.inhabitants.add(new Worker()); this.inhabitants.add(new Worker()); + this.inhabitants.add(new Collector()); + this.guardTime = gTime; + } + + public void move(Infantry i, Tile t) { + + } + + public void inRange(Infantry i, Building b) { + + } + + public boolean build(Tile t, Building b) { + int goldCost = b.getStage().getCost(SaulGoodMine.resource); + int ironCost = b.getStage().getCost(IronMine.resource); + int woodCost = b.getStage().getCost(LumberMine.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); + return true; + } else + return false; + } + + public boolean upgradeBuilding(int buildingIndex) { + + if (buildingIndex >= contains.size()) return false; + + Building b = contains.get(buildingIndex); + + int currentLevel = b.getLevel(); + CasaDeNarino hall = getTownHall(); + + if (currentLevel >= 2) return false; + else if (b instanceof Farm) return true; + + int goldCost = b.getUpgradeStage().getCost(SaulGoodMine.resource); + int ironCost = b.getUpgradeStage().getCost(IronMine.resource); + int woodCost = b.getUpgradeStage().getCost(LumberMine.resource); + + if (hall.getCurrentGold() >= goldCost && hall.getCurrentIron() >= ironCost && hall.getCurrentWood() >= woodCost) { + + if (b instanceof DefenseBuilding) { + ((DefenseBuilding) b).upgrade( + (b instanceof ArcherTower) ? (DefenseStages.archerTowerStages[currentLevel + 1]) : + (DefenseStages.cannonStages[currentLevel + 1]) + ); + } else if (b instanceof ResourceBuilding) { + ((ResourceBuilding) b).upgrade( + (b instanceof IronMine) ? (ResourceStages.ironStages[currentLevel + 1]) : + (b instanceof SaulGoodMine) ? (ResourceStages.goldStages[currentLevel + 1]) : + (ResourceStages.woodStages[currentLevel + 1]) + ); + } else { + b.upgrade(VillageHallStages.villageStages[currentLevel + 1]); + } + } else return false; + + return true; + } + + public boolean upgradeInhabitant(int inhabitantIndex) { + + if (inhabitantIndex >= inhabitants.size()) return false; + + Inhabitant i = inhabitants.get(inhabitantIndex); + + int currentLevel = i.getLevel(); + CasaDeNarino hall = getTownHall(); + + if (currentLevel >= 2 || hall.getCurrentGold() < 5) return false; + + i.setLevel(++currentLevel); + hall.addGold(-5); + + if (i instanceof Infantry) { + ((Infantry) i).setHealth(((Infantry) i).getHealth() + 1); + ((Infantry) i).setDamage(((Infantry) i).getDamage() + 1); + ((Infantry) i).setRange(((Infantry) i).getRange() + 1); + } else if (i instanceof Collector) { + ((Collector) i).setCollectionRate(((Collector) i).getCollectionRate() + 1); + } + + return true; + } + + public boolean train(Inhabitant i) { + CasaDeNarino hall = getTownHall(); + int goldCost = i.getCost(); + + if (hall.getCurrentGold() >= goldCost) { + if(!hall.addGold(-goldCost)) + throw new RuntimeException("Unable to subtract gold despite valid check!"); + inhabitants.add(i); + return true; + } else return false; + } + + public int getGuardTime() { + return guardTime; + } + + public void setGuardTime(int gTime) { + this.guardTime = gTime; + } + + public CasaDeNarino getTownHall(){ + return townHall; + } + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Archer.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Archer.java new file mode 100644 index 0000000..ae55d04 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Archer.java @@ -0,0 +1,38 @@ +package ca.cosc3p91.a2.gameobjects; + +public class Archer extends Infantry { + + static int cost = 4; + + private int lvl = 0; + + public Archer() { + super(90, 2, 10); + } + + @Override + public void move(Tile t) { + + } + + @Override + public void getPosition() { + + } + + @Override + public int getLevel() { + return lvl; + } + + @Override + public int getCost() { + return cost; + } + + @Override + public void setLevel(int level) { + lvl = level; + } + +} \ No newline at end of file diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ArcherTower.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ArcherTower.java new file mode 100644 index 0000000..2961a6c --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ArcherTower.java @@ -0,0 +1,14 @@ +package ca.cosc3p91.a2.gameobjects; + +public class ArcherTower extends DefenseBuilding { + + public ArcherTower() { + setLevel(1); + upgrade(DefenseStages.archerTowerStages[0]); + } + + @Override + public Stage getUpgradeStage() { + return DefenseStages.archerTowerStages[getLevel()+1]; + } +} \ No newline at end of file diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Building.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Building.java new file mode 100644 index 0000000..bd028f3 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Building.java @@ -0,0 +1,72 @@ +package ca.cosc3p91.a2.gameobjects; + +import java.util.ArrayList; + +public abstract class Building { + + // members + private int level; + private int health; + + private Stage stage; + + private int goldCost; + private int ironCost; + private int woodCost; + + private int buildTime; + + public ArrayList tiles = new ArrayList<>(); + public ArrayList inhabitants = new ArrayList<>(); + + // functions + + public int getLevel() { + return level; + } + + public int getHealth() { + return health; + } + + public int getCost(String type) { + return (type.equals("gold")) ? (goldCost) : + (type.equals("iron")) ? (ironCost) : woodCost; + } + + public Stage getStage() { + return stage; + } + + public abstract Stage getUpgradeStage(); + + public void setLevel(int level) { + this.level = level; + } + + public void setHealth(int health) { + this.health = health; + } + + public void setStage(Stage stage) { + this.stage = stage; + } + + public void addInhabitant(Inhabitant newMember) { + inhabitants.add(newMember); + // newMember.setBuilding(this); + } + + public void upgrade(Stage stage) { + this.stage = stage; + this.health += stage.dHealth; + // evil hack + String name = stage.getClass().getSimpleName(); + this.level = Integer.parseInt(name.charAt(name.length()-1) + "") - 1; + // interact with the timer regarding Upgrade time + } + + public int getBuildTime() { + return buildTime; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Cannon.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Cannon.java new file mode 100644 index 0000000..d6ae7b2 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Cannon.java @@ -0,0 +1,14 @@ +package ca.cosc3p91.a2.gameobjects; + +public class Cannon extends DefenseBuilding { + + public Cannon() { + setLevel(1); + upgrade(DefenseStages.cannonStages[0]); + } + + @Override + public Stage getUpgradeStage() { + return DefenseStages.cannonStages[getLevel()+1]; + } +} \ No newline at end of file diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/CasaDeNarino.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/CasaDeNarino.java new file mode 100644 index 0000000..ebc4b6a --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/CasaDeNarino.java @@ -0,0 +1,79 @@ +package ca.cosc3p91.a2.gameobjects; + +public class CasaDeNarino extends Building { + + private int goldCapacity = 0; + private int ironCapacity = 0; + private int woodCapacity = 0; + + private int currentGold = 0; + private int currentIron = 0; + private int currentWood = 50; + + public CasaDeNarino(int lvl, VillageStage baseStage) { + setLevel(lvl); + upgrade(baseStage); + } + + @Override + public Stage getUpgradeStage() { + return VillageHallStages.villageStages[getLevel()+1]; + } + + public void upgrade(VillageStage stage) { + super.upgrade(stage); + this.goldCapacity += stage.getGoldCapacityIncrease(); + this.ironCapacity += stage.getIronCapacityIncrease(); + this.woodCapacity += stage.getWoodCapacityIncrease(); + } + + + public int getGoldCapacity() { + return goldCapacity; + } + + public int getIronCapacity() { + return ironCapacity; + } + + public int getWoodCapacity() { + return woodCapacity; + } + + public int getCurrentGold() { + return currentGold; + } + + public int getCurrentIron() { + return currentIron; + } + + public int getCurrentWood() {return currentWood;} + + public boolean addGold(int amount) { + int newGold = this.currentGold + amount; + if (newGold <= goldCapacity && this.currentGold + amount >= 0){ + this.currentGold += amount; + return true; + } + return false; + } + + public boolean addIron(int amount) { + int newIron = this.currentIron + amount; + if (newIron <= ironCapacity && newIron >= 0) { + this.currentIron += amount; + return true; + } + return false; + } + + public boolean addWood(int amount) { + int newWood = this.currentWood + amount; + if (newWood <= woodCapacity && newWood >= 0) { + this.currentWood += amount; + return true; + } + return false; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Catapult.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Catapult.java new file mode 100644 index 0000000..97065b0 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Catapult.java @@ -0,0 +1,37 @@ +package ca.cosc3p91.a2.gameobjects; + +public class Catapult extends Infantry { + + static int cost = 6; + + private int lvl = 0; + + public Catapult() { + super(80, 12, 12); + } + + @Override + public void move(Tile t) { + + } + + @Override + public void getPosition() { + + } + + @Override + public int getLevel() { + return lvl; + } + + @Override + public int getCost() { + return cost; + } + + @Override + public void setLevel(int level) { + lvl = level; + } +} \ No newline at end of file diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Collector.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Collector.java new file mode 100644 index 0000000..b7e6627 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Collector.java @@ -0,0 +1,43 @@ +package ca.cosc3p91.a2.gameobjects; + +public class Collector implements Inhabitant { + + private int averageCollectionRate; + + static int cost = 2; + + private int lvl = 0; + + public int getCollectionRate() { + return averageCollectionRate; + } + + public void setCollectionRate(int rate) { + averageCollectionRate = rate; + } + + @Override + public void move(Tile t) { + + } + + @Override + public void getPosition() { + + } + + @Override + public int getLevel() { + return lvl; + } + + @Override + public int getCost() { + return cost; + } + + @Override + public void setLevel(int level) { + lvl = level; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java new file mode 100644 index 0000000..c6a8d84 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java @@ -0,0 +1,18 @@ +package ca.cosc3p91.a2.gameobjects; + +public abstract class DefenseBuilding extends Building { + + private int damage = 0; + private int range = 0; + + public void upgrade(DefenseStage stage) { + super.upgrade(stage); + this.damage += stage.getDamageChange(); + this.range += stage.getRangeChange(); + } + + public void attack(Infantry attacker) { + + } + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/DefenseStage.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/DefenseStage.java new file mode 100644 index 0000000..26cd7cf --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/DefenseStage.java @@ -0,0 +1,26 @@ +package ca.cosc3p91.a2.gameobjects; + +import ca.cosc3p91.a2.util.Time; + +class DefenseStage extends Stage { + + protected int dDamage; + + protected int dRange; + + public DefenseStage(int dHealth, int goldCost, int requiredVillageLevel, Time upgradeTime, int ironCost, int woodCost, + int damageIncrease, int rangeIncrease) { + super(dHealth, goldCost, requiredVillageLevel, upgradeTime, ironCost, woodCost); + this.dDamage = damageIncrease; + this.dRange = rangeIncrease; + } + + public int getDamageChange() { + return dDamage; + } + + public int getRangeChange() { + return dRange; + } + +} \ No newline at end of file diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java new file mode 100644 index 0000000..396073d --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java @@ -0,0 +1,46 @@ +package ca.cosc3p91.a2.gameobjects; + +import ca.cosc3p91.a2.util.Time; + +public class DefenseStages { + + public static class ArcherTowerStage1 extends DefenseStage { + public ArcherTowerStage1() { + super(100, 0, 0, new Time().offsetMinutes(1), 25, 75, 4,6); + } + } + + public static class ArcherTowerStage2 extends DefenseStage { + public ArcherTowerStage2() { + super(150, 50, 1, new Time().offsetMinutes(15), 25, 200, 8,8); + } + } + + public static class ArcherTowerStage3 extends DefenseStage { + public ArcherTowerStage3() { + super(200, 100, 2, new Time().offsetHours(1), 50, 300, 12,12); + } + } + + public static class CannonStage1 extends DefenseStage { + public CannonStage1() { + super(125, 0, 0, new Time().offsetMinutes(2), 100, 25, 8,4); + } + } + + public static class CannonStage2 extends DefenseStage { + public CannonStage2() { + super(175, 50, 1, new Time().offsetMinutes(20), 125, 25, 12,6); + } + } + + public static class CannonStage3 extends DefenseStage { + public CannonStage3() { + super(225, 100, 2, new Time().offsetHours(1), 200, 50, 14,8); + } + } + + public static DefenseStage[] archerTowerStages = {new DefenseStages.ArcherTowerStage1(), new DefenseStages.ArcherTowerStage2(), new DefenseStages.ArcherTowerStage3()}; + public static DefenseStage[] cannonStages = {new DefenseStages.CannonStage1(), new DefenseStages.CannonStage2(), new DefenseStages.CannonStage3()}; + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Farm.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Farm.java new file mode 100644 index 0000000..4eacb1c --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Farm.java @@ -0,0 +1,23 @@ +package ca.cosc3p91.a2.gameobjects; + +public class Farm extends ResourceBuilding { + + public Farm(int lvl, ResourceStage baseStage) { + setLevel(lvl); + upgrade(baseStage); + } + + public int getPopulationContribution() { + return getHarvestRate(); + } + + @Override + protected ResourceHarvestHandler getHarvestHandler() { + return hall -> {}; + } + + @Override + public Stage getUpgradeStage() { + return ResourceStages.goldStages[getLevel()+1]; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Infantry.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Infantry.java new file mode 100644 index 0000000..450d50c --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Infantry.java @@ -0,0 +1,43 @@ +package ca.cosc3p91.a2.gameobjects; + +public abstract class Infantry implements Inhabitant { + + private int health; + + private int damage; + + private int range; + + public Infantry(int hitPoints, int damage, int range) { + this.health = hitPoints; + this.damage = damage; + this.range = range; + } + + public void attack(Building b) { + } + + public int getHealth() { + return health; + } + + public int getDamage() { + return damage; + } + + public int getRange() { + return range; + } + + public void setHealth(int health) { + this.health = health; + } + + public void setDamage(int damage) { + this.damage = damage; + } + + public void setRange(int range) { + this.range = range; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java new file mode 100644 index 0000000..997c5a5 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java @@ -0,0 +1,19 @@ +package ca.cosc3p91.a2.gameobjects; + +import ca.cosc3p91.a2.game.Map; + +public interface Inhabitant { + + Map map = null; + Building building = null; + int lvl = 0; + + void move(Tile t); + + void getPosition(); + + int getLevel(); + void setLevel(int level); + int getCost(); + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/IronMine.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/IronMine.java new file mode 100644 index 0000000..b949894 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/IronMine.java @@ -0,0 +1,20 @@ +package ca.cosc3p91.a2.gameobjects; + +public class IronMine extends ResourceBuilding { + + public static String resource = "iron"; + + public IronMine(ResourceStage baseStage) { + upgrade(baseStage); + } + + @Override + protected ResourceHarvestHandler getHarvestHandler() { + return hall -> hall.addIron(getHarvestRate()); + } + + @Override + public Stage getUpgradeStage() { + return ResourceStages.ironStages[getLevel()+1]; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Knight.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Knight.java new file mode 100644 index 0000000..e01248e --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Knight.java @@ -0,0 +1,37 @@ +package ca.cosc3p91.a2.gameobjects; + +public class Knight extends Infantry { + + static int cost = 6; + + private int lvl = 0; + + public Knight() { + super(150, 6, 6); + } + + @Override + public void move(Tile t) { + + } + + @Override + public void getPosition() { + + } + + @Override + public int getLevel() { + return lvl; + } + + @Override + public int getCost() { + return cost; + } + + @Override + public void setLevel(int level) { + lvl = level; + } +} \ No newline at end of file diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/LumberMine.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/LumberMine.java new file mode 100644 index 0000000..514621b --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/LumberMine.java @@ -0,0 +1,20 @@ +package ca.cosc3p91.a2.gameobjects; + +public class LumberMine extends ResourceBuilding { + + public static String resource = "wood"; + + public LumberMine(ResourceStage baseStage) { + upgrade(baseStage); + } + + @Override + protected ResourceHarvestHandler getHarvestHandler() { + return hall -> hall.addWood(getHarvestRate()); + } + + @Override + public Stage getUpgradeStage() { + return ResourceStages.woodStages[getLevel()+1]; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ResourceBuilding.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ResourceBuilding.java new file mode 100644 index 0000000..400446e --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ResourceBuilding.java @@ -0,0 +1,40 @@ +package ca.cosc3p91.a2.gameobjects; + +import ca.cosc3p91.a2.util.Time; + +public abstract class ResourceBuilding extends Building { + + protected interface ResourceHarvestHandler { + void harvest(CasaDeNarino hall); + } + + public static String resource; + + private final Time harvestMinTime = new Time().offsetSeconds(10); + + private int harvest_rate; + private Time nextHarvestTime; + + public ResourceBuilding() { + nextHarvestTime = Time.getTime().offsetTime(harvestMinTime); + } + + public void upgrade(ResourceStage stage) { + super.upgrade(stage); + this.harvest_rate += stage.getHarvestRateIncrease(); + } + + public void update(CasaDeNarino hall){ + if (nextHarvestTime.occurred()){ + getHarvestHandler().harvest(hall); + nextHarvestTime = Time.getTime().offsetTime(harvestMinTime); + } + } + + protected abstract ResourceHarvestHandler getHarvestHandler(); + + public int getHarvestRate(){ + return harvest_rate; + } + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ResourceStage.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ResourceStage.java new file mode 100644 index 0000000..7585e2d --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ResourceStage.java @@ -0,0 +1,19 @@ +package ca.cosc3p91.a2.gameobjects; + +import ca.cosc3p91.a2.util.Time; + +public class ResourceStage extends Stage { + + protected int harvestRateIncrease; + + public ResourceStage(int dHealth, int goldCost, int requiredVillageLevel, Time upgradeTime, int ironCost, int woodCost, + int harvestRateIncr) { + super(dHealth, goldCost, requiredVillageLevel, upgradeTime, ironCost, woodCost); + this.harvestRateIncrease = harvestRateIncr; + } + + public int getHarvestRateIncrease() { + return harvestRateIncrease; + } + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ResourceStages.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ResourceStages.java new file mode 100644 index 0000000..33add9e --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/ResourceStages.java @@ -0,0 +1,65 @@ +package ca.cosc3p91.a2.gameobjects; + +import ca.cosc3p91.a2.util.Time; + +public class ResourceStages { + + public static class GoldStage1 extends ResourceStage { + public GoldStage1() { + super(100, 0, 0, new Time().offsetMinutes(1), 25, 250, 25); + } + } + + public static class GoldStage2 extends ResourceStage { + public GoldStage2() { + super(100, 100, 1, new Time().offsetMinutes(15), 25, 275, 35); + } + } + + public static class GoldStage3 extends ResourceStage { + public GoldStage3() { + super(100, 150, 2, new Time().offsetHours(1), 50, 325, 50); + } + } + + public static class IronStage1 extends ResourceStage { + public IronStage1() { + super(100, 0, 0, new Time().offsetMinutes(1), 0, 125, 25); + } + } + + public static class IronStage2 extends ResourceStage { + public IronStage2() { + super(100, 15, 1, new Time().offsetMinutes(15), 25, 155, 35); + } + } + + public static class IronStage3 extends ResourceStage { + public IronStage3() { + super(100, 50, 2, new Time().offsetHours(1), 50, 250, 50); + } + } + + public static class WoodStage1 extends ResourceStage { + public WoodStage1() { + super(100, 0, 0, new Time().offsetMinutes(1), 0, 50, 25); + } + } + + public static class WoodStage2 extends ResourceStage { + public WoodStage2() { + super(100, 5, 1, new Time().offsetMinutes(15), 5, 75, 35); + } + } + + public static class WoodStage3 extends ResourceStage { + public WoodStage3() { + super(100, 10, 2, new Time().offsetHours(1), 25, 100, 50); + } + } + + public static ResourceStage[] goldStages = {new GoldStage1(), new GoldStage2(), new GoldStage3()}; + public static ResourceStage[] ironStages = {new IronStage1(), new IronStage2(), new IronStage3()}; + public static ResourceStage[] woodStages = {new WoodStage1(), new WoodStage2(), new WoodStage3()}; + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java new file mode 100644 index 0000000..2c3cbff --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java @@ -0,0 +1,20 @@ +package ca.cosc3p91.a2.gameobjects; + +public class SaulGoodMine extends ResourceBuilding { + + public static String resource = "gold"; + + public SaulGoodMine(ResourceStage baseStage) { + upgrade(baseStage); + } + + @Override + protected ResourceHarvestHandler getHarvestHandler() { + return hall -> hall.addGold(getHarvestRate()); + } + + @Override + public Stage getUpgradeStage() { + return ResourceStages.goldStages[getLevel()+1]; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Soldier.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Soldier.java new file mode 100644 index 0000000..3a11d8c --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Soldier.java @@ -0,0 +1,37 @@ +package ca.cosc3p91.a2.gameobjects; + +public class Soldier extends Infantry { + + static int cost = 4; + + int lvl = 0; + + public Soldier() { + super(100, 4, 4); + } + + @Override + public void move(Tile t) { + + } + + @Override + public void getPosition() { + + } + + @Override + public int getLevel() { + return lvl; + } + + @Override + public int getCost() { + return cost; + } + + @Override + public void setLevel(int level) { + lvl = level; + } +} \ No newline at end of file diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Stage.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Stage.java new file mode 100644 index 0000000..60fe1c6 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Stage.java @@ -0,0 +1,47 @@ +package ca.cosc3p91.a2.gameobjects; + +import ca.cosc3p91.a2.util.Time; + +import java.util.HashMap; + +public abstract class Stage { + + protected int dHealth; + + protected int goldCost; + + protected int requiredVillageLevel; + + protected Time upgradeTime; + + protected int ironCost; + + protected int woodCost; + + public Stage(int dHealth, int goldCost, int requiredVillageLevel, Time upgradeTime, int ironCost, int woodCost) { + this.dHealth = dHealth; + this.goldCost = goldCost; + this.requiredVillageLevel = requiredVillageLevel; + this.upgradeTime = upgradeTime; + this.ironCost = ironCost; + this.woodCost = woodCost; + } + + public int getHealthChange() { + return dHealth; + } + + public int getCost(String type) { + return (type.equals("gold")) ? (goldCost) : + (type.equals("iron")) ? (ironCost) : woodCost; + } + + public int getRequiredVillageLevel() { + return requiredVillageLevel; + } + + public Time getUpgradeTime() { + return upgradeTime; + } + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Tile.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Tile.java new file mode 100644 index 0000000..d6b3dae --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Tile.java @@ -0,0 +1,11 @@ +package ca.cosc3p91.a2.gameobjects; + +import java.util.List; + +public class Tile { + + public int x; + + public int y; + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/VillageHallStages.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/VillageHallStages.java new file mode 100644 index 0000000..d1f2c3b --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/VillageHallStages.java @@ -0,0 +1,30 @@ +package ca.cosc3p91.a2.gameobjects; + +import ca.cosc3p91.a2.util.Time; + +public class VillageHallStages { + + public static class VillageStage1 extends VillageStage { + public VillageStage1() { + super(100, 0, 0, new Time(), 0, + 0, 1000, 2500, 5000); + } + } + + public static class VillageStage2 extends VillageStage { + public VillageStage2() { + super(550, 1000, 0, new Time().offsetHours(2), 2500, + 5000, 2500, 5000, 10000); + } + } + + public static class VillageStage3 extends VillageStage { + public VillageStage3() { + super(550, 2500, 0, new Time().offsetHours(2), 5000, + 10000, 5000, 7500, 15000); + } + } + + public static VillageStage[] villageStages = {new VillageStage1(), new VillageStage2(), new VillageStage3()}; + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/VillageStage.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/VillageStage.java new file mode 100644 index 0000000..8600fc2 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/VillageStage.java @@ -0,0 +1,33 @@ +package ca.cosc3p91.a2.gameobjects; + +import ca.cosc3p91.a2.util.Time; + +public class VillageStage extends Stage { + + protected int goldCapacityIncrease; + + protected int ironCapacityIncrease; + + protected int woodCapacityIncrease; + + public VillageStage(int dHealth, int goldCost, int requiredVillageLevel, Time upgradeTime, int ironCost, int woodCost, + int goldCapIncrease, int ironCapIncrease, int woodCapIncrease) { + super(dHealth, goldCost, requiredVillageLevel, upgradeTime, ironCost, woodCost); + this.goldCapacityIncrease = goldCapIncrease; + this.ironCapacityIncrease = ironCapIncrease; + this.woodCapacityIncrease = woodCapIncrease; + } + + public int getGoldCapacityIncrease() { + return goldCapacityIncrease; + } + + public int getIronCapacityIncrease() { + return ironCapacityIncrease; + } + + public int getWoodCapacityIncrease() { + return woodCapacityIncrease; + } + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Worker.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Worker.java new file mode 100644 index 0000000..357b0b2 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/Worker.java @@ -0,0 +1,43 @@ +package ca.cosc3p91.a2.gameobjects; + +public class Worker implements Inhabitant { + + private boolean currentlyBuilding; + + static int cost = 2; + + private int lvl = 0; + + public void set_IsBuilding(boolean state) { + currentlyBuilding = state; + } + + public boolean isCurrentlyBuilding() { + return currentlyBuilding; + } + + @Override + public void move(Tile t) { + + } + + @Override + public void getPosition() { + + } + + @Override + public int getLevel() { + return lvl; + } + + @Override + public int getCost() { + return cost; + } + + @Override + public void setLevel(int level) { + lvl = level; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/player/Player.java b/Assignment 3/src/ca/cosc3p91/a2/player/Player.java new file mode 100644 index 0000000..817cfd2 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/player/Player.java @@ -0,0 +1,6 @@ +package ca.cosc3p91.a2.player; + +public class Player { + + +} \ No newline at end of file diff --git a/Assignment 3/src/ca/cosc3p91/a2/userinterface/GuiManager.java b/Assignment 3/src/ca/cosc3p91/a2/userinterface/GuiManager.java new file mode 100644 index 0000000..45e4b54 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/userinterface/GuiManager.java @@ -0,0 +1,4 @@ +package ca.cosc3p91.a2.userinterface; + +public class GuiManager { +} \ No newline at end of file diff --git a/Assignment 3/src/ca/cosc3p91/a2/util/Print.java b/Assignment 3/src/ca/cosc3p91/a2/util/Print.java new file mode 100644 index 0000000..27e7398 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/util/Print.java @@ -0,0 +1,249 @@ +package ca.cosc3p91.a2.util; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Ported from blt::TableFormatter (C++) + * https://github.com/Tri11Paragon/BLT/ + */ +public class Print { + + public static class Row { + private final ArrayList values; + + public Row(ArrayList row) { + this.values = row; + } + + public Row() { + this.values = new ArrayList<>(); + } + + public Row(String... values){ + this(); + this.values.addAll(Arrays.asList(values)); + } + + public void add(String value) { + values.add(value); + } + } + + public static class Column { + private String columnName; + private long maxColumnLength = 0; + + public Column(String columnName) { + this.columnName = columnName; + } + } + + private final ArrayList rows = new ArrayList<>(); + private final ArrayList columns = new ArrayList<>(); + + private final String tableName; + private final int columnPadding; + private int width; + private final int targetWidth; + + public Print(String tableName, int columnPadding, int targetWidth) { + this.tableName = tableName; + this.columnPadding = columnPadding; + this.targetWidth = targetWidth; + } + + public Print(String tableName, int columnPadding){this(tableName, columnPadding, -1);} + + public Print(String tableName){this(tableName, 2);} + + public Print() { + this(""); + } + + private String createPadding(int amount) { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < amount; i++) { + builder.append(' '); + } + return builder.toString(); + } + + private String generateTopSelector(long size) { + long sizeOfName = tableName.isEmpty() ? 0 : tableName.length() + 4; + long sizeNameRemoved = size - sizeOfName; + + StringBuilder halfWidthLeftSeparator = new StringBuilder(); + StringBuilder halfWidthRightSeparator = new StringBuilder(); + + long sizeNameFloor = (long) Math.floor((double) sizeNameRemoved / 2.0); + long sizeNameCeil = (long) Math.ceil((double) sizeNameRemoved / 2.0); + + halfWidthLeftSeparator.append('+'); + + for (int i = 0; i < sizeNameFloor - 1; i++) + halfWidthLeftSeparator.append('-'); + for (int i = 0; i < sizeNameCeil - 1; i++) + halfWidthRightSeparator.append('-'); + + halfWidthRightSeparator.append('+'); + + StringBuilder separator = new StringBuilder(); + separator.append(halfWidthLeftSeparator.toString()); + if (sizeOfName != 0) { + separator.append("{ "); + separator.append(tableName); + separator.append(" }"); + } + separator.append(halfWidthRightSeparator); + return separator.toString(); + } + + private String generateColumnHeader() { + updateColumnLengths(); + StringBuilder header = new StringBuilder(); + header.append('|'); + + for (int i = 0; i < columns.size(); i++) { + Column column = columns.get(i); + double columnPaddingLength = ((int) (column.maxColumnLength) - (int) (column.columnName.length())) / 2.0; + header.append(createPadding((int) (columnPadding + (int) Math.floor(columnPaddingLength)))); + + header.append(column.columnName); + + header.append(createPadding((int) (columnPadding + (int) Math.ceil(columnPaddingLength)))); + if (i < columns.size() - 1) + header.append('|'); + } + header.append('|'); + + return header.toString(); + } + + private String generateSeparator(long size) { + int nextIndex = 0; + int currentColumnIndex = 0; + StringBuilder wholeWidthSeparator = new StringBuilder(); + for (int i = 0; i < size; i++) { + if (i == nextIndex) { + //System.out.println(currentColumnIndex + " " + nextIndex + " " + size); + int currentColumnSize = (int) (columns.get(currentColumnIndex++).maxColumnLength + columnPadding * 2); + nextIndex += currentColumnSize + 1; + wholeWidthSeparator.append('+'); + } else + wholeWidthSeparator.append('-'); + } + wholeWidthSeparator.append('+'); + return wholeWidthSeparator.toString(); + } + + private void updateColumnLengths() { + for (int i = 0; i < columns.size(); i++) { + Column column = columns.get(i); + column.maxColumnLength = column.columnName.length(); + for (Row row : rows) { + column.maxColumnLength = Math.max(column.maxColumnLength, row.values.get(i).length()); + } + } + } + + public long columnSize(Column column) { + return column.columnName.length() + columnPadding * 2L; + } + + public Print addColumn(Column column) { + columns.add(column); + return this; + } + + public Print addRow(Row row) { + if (row.values.size() > columns.size()) + throw new RuntimeException("Unable to create row with more values than columns!"); + if (row.values.size() < columns.size()) + for (int i = row.values.size(); i < columns.size(); i++) + row.values.add(""); + rows.add(row); + return this; + } + + public Print addRow(ArrayList row) { + return addRow(new Row(row)); + } + + public ArrayList createTable(boolean top, boolean bottom, boolean separators) { + String header = generateColumnHeader(); + String topSeparator = generateTopSelector(header.length()); + String lineSeparator = generateSeparator(header.length() - 1); + + if (targetWidth > 0) { + int diff = targetWidth - header.length(); + + if (diff > 0) { + + int left = (int) Math.floor(diff / 2.0); + int right = (int) Math.ceil(diff / 2.0); + + int leftleft = (int) Math.floor(left / 2.0); + int leftright = (int) Math.ceil(left / 2.0); + + int rightleft = (int) Math.floor(right / 2.0); + int rightright = (int) Math.ceil(right / 2.0); + + columns.get(0).columnName = createPadding(leftleft) + columns.get(0).columnName + createPadding(leftright); + columns.get(columns.size() - 1).columnName = createPadding(rightleft) + columns.get(columns.size() - 1).columnName + createPadding(rightright); + + header = generateColumnHeader(); + topSeparator = generateTopSelector(header.length()); + lineSeparator = generateSeparator(header.length() - 1); + } + } + + width = header.length(); + + ArrayList lines = new ArrayList<>(); + + if (top) + lines.add(topSeparator); + + lines.add(header); + lines.add(lineSeparator); + + for (Row row : rows) { + StringBuilder rowLine = new StringBuilder(); + rowLine.append('|'); + for (int i = 0; i < row.values.size(); i++) { + String value = row.values.get(i); + Column column = columns.get(i); + int spaceLeft = (int) (column.maxColumnLength - value.length()); + rowLine.append(createPadding((int) Math.floor(spaceLeft / 2.0) + columnPadding)); + rowLine.append(value); + rowLine.append(createPadding((int) Math.ceil(spaceLeft / 2.0) + columnPadding)); + rowLine.append('|'); + } + lines.add(rowLine.toString()); + } + + if (bottom) + lines.add(lineSeparator); + + return lines; + } + + public ArrayList createBox() { + return new ArrayList<>(); + } + + public ArrayList createTable() { + return createTable(true, true, true); + } + + public int getWidth(){ + return width; + } + + public static void print(ArrayList lines) { + for (String line : lines) + System.out.println(line); + } + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/util/Time.java b/Assignment 3/src/ca/cosc3p91/a2/util/Time.java new file mode 100644 index 0000000..439d17f --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/util/Time.java @@ -0,0 +1,48 @@ +package ca.cosc3p91.a2.util; + +public class Time { + + private long timeSeconds; + + public Time() { + this.timeSeconds = 0; + } + + protected Time(long timeSeconds) { + this.timeSeconds = timeSeconds; + } + + public Time offsetSeconds(long seconds) { + this.timeSeconds += seconds; + return this; + } + + public Time offsetMinutes(long minutes) { + return offsetSeconds(minutes * 60); + } + + public Time offsetHours(long hours) { + return offsetMinutes(hours * 60); + } + + public Time offsetDays(long days) { + return offsetHours(days * 24); + } + + public Time offsetTime(Time time) { + return offsetSeconds(time.timeSeconds); + } + + public long get() { + return timeSeconds; + } + + public boolean occurred() { + return getTime().timeSeconds >= timeSeconds; + } + + public static Time getTime() { + return new Time(System.currentTimeMillis() / 1000); + } + +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/util/Util.java b/Assignment 3/src/ca/cosc3p91/a2/util/Util.java new file mode 100644 index 0000000..e909ac0 --- /dev/null +++ b/Assignment 3/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); + } + +} diff --git a/Assignment 3/suggestedSolutionA1.png b/Assignment 3/suggestedSolutionA1.png new file mode 100644 index 0000000..f656bf4 Binary files /dev/null and b/Assignment 3/suggestedSolutionA1.png differ