diff --git a/Assignment 2/src/ca/cosc3p91/a2/Main.java b/Assignment 2/src/ca/cosc3p91/a2/Main.java index 1747fdb..18130b1 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/Main.java +++ b/Assignment 2/src/ca/cosc3p91/a2/Main.java @@ -7,7 +7,6 @@ public class Main { public static void main(String[] args) { GameEngine engine = new GameEngine(); - engine.printState(); engine.run(); } diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java index 05fd90a..14a37a2 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java @@ -2,10 +2,14 @@ package ca.cosc3p91.a2.game; import ca.cosc3p91.a2.gameobjects.*; import ca.cosc3p91.a2.player.*; +import ca.cosc3p91.a2.util.Print; + +import java.util.Scanner; public class GameEngine implements Runnable { private Player player; + boolean running = true; private int pillageFactor; @@ -16,22 +20,54 @@ public class GameEngine implements Runnable { public GameEngine() { player = new Player(); VillageStage vInitialStage = new VillageStage(100, 0, 2, 30, 0, - 0, 12, 12, 12); - map = new Map(new Village_Hall(1, vInitialStage), 30); + 0, 1000, 2500, 5000); + map = new Map(new VillageHall(1, vInitialStage), 30); } - public void printState() { - // Print toPrint = new Print("~ Current Village Buildings ~",2); + private void printState() { + Print resourcesPrinter = new Print("Current Village Resources", 2); - System.out.println("In Map:\n"); - System.out.println("\t~ Current Village Buildings ~\n"); - for (Building b : map.contains) { - System.out.println("\t|> " + b.getClass().getSimpleName() + " lvl: " + b.getLevel() + " health: " + b.getHealth()); - } - System.out.println("\n\t~ Current Village Inhabitants ~\n\n"); - for (Inhabitant i : map.inhabitants) { - System.out.println("\t|> " + i.getClass().getSimpleName() + " lvl: " + i.getLevel()); - } + 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()), + 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()))); + + Print.print(inhabs.createTable(true, true, true)); } public void attackVillage(Map map) { @@ -46,6 +82,10 @@ public class GameEngine implements Runnable { @Override public void run() { - + Scanner sc = new Scanner(System.in); + while (running) { + printState(); + int in = sc.nextInt(); + } } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/Map.java b/Assignment 2/src/ca/cosc3p91/a2/game/Map.java index fbacf13..9d64419 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/Map.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/Map.java @@ -9,7 +9,7 @@ public class Map { static int MAXSIZE = 400; - private Village_Hall townHall; + private VillageHall townHall; private int guardTime; @@ -17,7 +17,7 @@ public class Map { public List inhabitants; - public Map(Village_Hall villageHall, int gTime) { + public Map(VillageHall villageHall, int gTime) { contains = new ArrayList<>(); inhabitants = new ArrayList<>(); this.townHall = villageHall; @@ -45,4 +45,8 @@ public class Map { this.guardTime = gTime; } + public VillageHall getTownHall(){ + return townHall; + } + } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java index b1bded8..eb1437d 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java @@ -12,5 +12,5 @@ public class Farm extends ResourceBuilding { } @Override - public void harvest(Village_Hall hall) {} + public void harvest(VillageHall hall) {} } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java index a77a0d8..390791d 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java @@ -10,7 +10,7 @@ public class IronMine extends ResourceBuilding { } @Override - public void harvest(Village_Hall hall) { + public void harvest(VillageHall hall) { hall.addCurrentIron(getHarvestRate()); } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java index 5f87ba3..7c2c4cc 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java @@ -9,7 +9,7 @@ public class LumberMine extends ResourceBuilding { upgrade(baseStage); } - public void harvest(Village_Hall hall) { + public void harvest(VillageHall hall) { hall.addCurrentWood(getHarvestRate()); } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceBuilding.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceBuilding.java index 04f38dd..5763b16 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceBuilding.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceBuilding.java @@ -20,14 +20,14 @@ public abstract class ResourceBuilding extends Building { this.harvest_rate += stage.getHarvestRateIncrease(); } - public void update(Village_Hall hall){ + public void update(VillageHall hall){ if (nextHarvestTime.occurred()){ harvest(hall); nextHarvestTime = Time.getTime().offsetTime(harvestMinTime); } } - protected abstract void harvest(Village_Hall hall); + protected abstract void harvest(VillageHall hall); public int getHarvestRate(){ return harvest_rate; diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java index a4135f3..600b143 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/SaulGoodMine.java @@ -1,7 +1,5 @@ package ca.cosc3p91.a2.gameobjects; -import ca.cosc3p91.a2.util.Time; - public class SaulGoodMine extends ResourceBuilding { public static String resource = "gold"; @@ -12,7 +10,7 @@ public class SaulGoodMine extends ResourceBuilding { } @Override - public void harvest(Village_Hall hall) { + public void harvest(VillageHall hall) { hall.addCurrentGold(getHarvestRate()); } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Village_Hall.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/VillageHall.java similarity index 92% rename from Assignment 2/src/ca/cosc3p91/a2/gameobjects/Village_Hall.java rename to Assignment 2/src/ca/cosc3p91/a2/gameobjects/VillageHall.java index 13515ac..edd8d81 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Village_Hall.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/VillageHall.java @@ -2,7 +2,7 @@ package ca.cosc3p91.a2.gameobjects; import java.util.ArrayList; -public class Village_Hall extends Building { +public class VillageHall extends Building { private int goldCapacity = 0; private int ironCapacity = 0; @@ -12,7 +12,7 @@ public class Village_Hall extends Building { private int currentIron; private int currentWood; - public Village_Hall(int lvl, VillageStage baseStage) { + public VillageHall(int lvl, VillageStage baseStage) { setLevel(lvl); upgrade(baseStage); } diff --git a/Assignment 2/src/ca/cosc3p91/a2/util/Print.java b/Assignment 2/src/ca/cosc3p91/a2/util/Print.java index cae2953..bb406df 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/util/Print.java +++ b/Assignment 2/src/ca/cosc3p91/a2/util/Print.java @@ -1,6 +1,7 @@ package ca.cosc3p91.a2.util; import java.util.ArrayList; +import java.util.Arrays; /** * Ported from blt::TableFormatter (C++) @@ -19,13 +20,18 @@ public class Print { 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 final String columnName; + private String columnName; private long maxColumnLength = 0; public Column(String columnName) { @@ -38,15 +44,21 @@ public class Print { private final String tableName; private final int columnPadding; - private int maxColumnWidth; + private int width; + private final int targetWidth; - public Print(String tableName, int columnPadding) { + 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("", 2); + this(""); } private String createPadding(int amount) { @@ -114,7 +126,7 @@ public class Print { StringBuilder wholeWidthSeparator = new StringBuilder(); for (int i = 0; i < size; i++) { if (i == nextIndex) { - System.out.println(currentColumnIndex + " " + nextIndex + " " + size); + //System.out.println(currentColumnIndex + " " + nextIndex + " " + size); int currentColumnSize = (int) (columns.get(currentColumnIndex++).maxColumnLength + columnPadding * 2); nextIndex += currentColumnSize + 1; wholeWidthSeparator.append('+'); @@ -130,7 +142,7 @@ public class Print { 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() - 1); + column.maxColumnLength = Math.max(column.maxColumnLength, row.values.get(i).length()); } } } @@ -159,12 +171,38 @@ public class Print { } public ArrayList createTable(boolean top, boolean bottom, boolean separators) { - ArrayList lines = new ArrayList<>(); - 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 leftleft = (int) Math.floor(left / 2.0); + int leftright = (int) Math.ceil(left / 2.0); + + int right = (int) Math.ceil(diff / 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); @@ -200,6 +238,10 @@ public class Print { return createTable(true, true, true); } + public int getWidth(){ + return width; + } + public static void print(ArrayList lines) { for (String line : lines) System.out.println(line);