From 7106da8e950f1e0257cfe022c26ed6d91865c88d Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 1 Mar 2023 13:31:16 -0500 Subject: [PATCH 1/5] reformat code --- .../src/ca/cosc3p91/a2/game/GameEngine.java | 34 ++++++------ Assignment 2/src/ca/cosc3p91/a2/game/Map.java | 31 ++++++----- .../ca/cosc3p91/a2/gameobjects/Building.java | 54 +++++++++---------- .../ca/cosc3p91/a2/gameobjects/Collector.java | 8 +-- .../a2/gameobjects/DefenseBuilding.java | 8 +-- .../cosc3p91/a2/gameobjects/DefenseStage.java | 12 ++--- .../src/ca/cosc3p91/a2/gameobjects/Farm.java | 12 ++--- .../ca/cosc3p91/a2/gameobjects/GoldMine.java | 8 +-- .../ca/cosc3p91/a2/gameobjects/Infantry.java | 22 ++++---- .../cosc3p91/a2/gameobjects/Inhabitant.java | 8 +-- .../ca/cosc3p91/a2/gameobjects/IronMine.java | 8 +-- .../cosc3p91/a2/gameobjects/LumberMine.java | 8 +-- .../a2/gameobjects/ResourceBuidling.java | 6 +-- .../a2/gameobjects/ResourceStage.java | 8 +-- .../src/ca/cosc3p91/a2/gameobjects/Stage.java | 36 ++++++------- .../src/ca/cosc3p91/a2/gameobjects/Tile.java | 6 +-- .../cosc3p91/a2/gameobjects/VillageStage.java | 24 ++++----- .../cosc3p91/a2/gameobjects/Village_Hall.java | 24 ++++----- .../ca/cosc3p91/a2/gameobjects/Worker.java | 8 +-- .../src/ca/cosc3p91/a2/player/Player.java | 6 +-- 20 files changed, 167 insertions(+), 164 deletions(-) diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java index 167c740..54552b6 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java @@ -4,26 +4,30 @@ import ca.cosc3p91.a2.player.Player; public class GameEngine implements Runnable { - private Player player; + private final boolean running = true; - private int pillageFactor; + private Player player; - private int currentTime; + private int pillageFactor; - public Map map; + private int currentTime; - public void attackVIllage(Map map) { - } + public Map map; - public Map generateMap() { - return null; - } + public void attackVIllage(Map map) { + } - public void getScore(Map map) { - } + public Map generateMap() { + return null; + } - @Override - public void run() { - - } + public void getScore(Map map) { + } + + @Override + public void run() { + while (running) { + + } + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/Map.java b/Assignment 2/src/ca/cosc3p91/a2/game/Map.java index 4f1deb9..c216d2e 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/Map.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/Map.java @@ -5,31 +5,30 @@ import ca.cosc3p91.a2.gameobjects.*; import java.util.List; - public class Map { - private Village_Hall townHall; + private Village_Hall townHall; - private int guardTime; + private int guardTime; - private List contains; + private List contains; - public List inhabitants; + public List inhabitants; - public void move(Infantry i, Tile t) { - } + public void move(Infantry i, Tile t) { + } - public void inRange(Infantry i, Building b) { - } + public void inRange(Infantry i, Building b) { + } - public void build(Village_Hall hall, Tile t, Building b) { - } + public void build(Village_Hall hall, Tile t, Building b) { + } - public int getGuardTime() { - return guardTime; - } + public int getGuardTime() { + return guardTime; + } - public void setGuardTime() { - } + public void setGuardTime() { + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Building.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Building.java index 13d2f1b..cddb71e 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Building.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Building.java @@ -4,47 +4,47 @@ import java.util.List; public abstract class Building { - private static int level; + private static int level; - private int health; + private int health; - private static Stage stages; + private static Stage stages; - private int goldCost; + private int goldCost; - private int ironCost; + private int ironCost; - private int woodCost; + private int woodCost; - private int buildTime; + private int buildTime; - public List stage; - public Tile tile; + public List stage; + public Tile tile; - public List inhabitant; + public List inhabitant; - public int getLevel() { - return level; - } + public int getLevel() { + return level; + } - public int getHealth() { - return health; - } + public int getHealth() { + return health; + } - public int getCost(String type) { - return 0; - } + public int getCost(String type) { + return 0; + } - public int getUpgradeCost() { - return 0; - } + public int getUpgradeCost() { + return 0; + } - public void upgrade() { + public void upgrade() { - } + } - public int getBuildTime() { - return 0; - } + public int getBuildTime() { + return 0; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Collector.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Collector.java index 7d63c27..dcf9a4f 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Collector.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Collector.java @@ -2,10 +2,10 @@ package ca.cosc3p91.a2.gameobjects; public class Collector { - private int averageCollectionRate; + private int averageCollectionRate; - public int getCollectionRate() { - return averageCollectionRate; - } + public int getCollectionRate() { + return averageCollectionRate; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java index 669945f..92dc8fe 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseBuilding.java @@ -2,12 +2,12 @@ package ca.cosc3p91.a2.gameobjects; public class DefenseBuilding extends Building { - public int damage; + public int damage; - public int range; + public int range; - public void attack(Infantry attacker) { + public void attack(Infantry attacker) { - } + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStage.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStage.java index ea2c8fa..09083cd 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStage.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStage.java @@ -2,14 +2,14 @@ package ca.cosc3p91.a2.gameobjects; class DefenseStage extends Stage { - protected int dDamge; + protected int dDamge; - protected int dRange; + protected int dRange; - public void getDamageChange() { - } + public void getDamageChange() { + } - public void getRangeChange() { - } + public void getRangeChange() { + } } \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java index 3f39711..b8e78ea 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Farm.java @@ -2,12 +2,12 @@ package ca.cosc3p91.a2.gameobjects; public class Farm extends ResourceBuidling { - public int getPopulationContribution() { - return 0; - } + public int getPopulationContribution() { + return 0; + } - @Override - public void harvest(Village_Hall hall) { + @Override + public void harvest(Village_Hall hall) { - } + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/GoldMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/GoldMine.java index 43e25de..648bc90 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/GoldMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/GoldMine.java @@ -2,10 +2,10 @@ package ca.cosc3p91.a2.gameobjects; public class GoldMine extends ResourceBuidling { - public static String resource = "good"; + public static String resource = "good"; - @Override - public void harvest(Village_Hall hall) { + @Override + public void harvest(Village_Hall hall) { - } + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Infantry.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Infantry.java index 9a3c078..a9c4f7c 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Infantry.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Infantry.java @@ -2,22 +2,22 @@ package ca.cosc3p91.a2.gameobjects; public abstract class Infantry { - private int health; + private int health; - private int damage; + private int damage; - private int range; + private int range; - public void attack(Building b) { - } + public void attack(Building b) { + } - public void getHealth() { - } + public void getHealth() { + } - public void getDamage() { - } + public void getDamage() { + } - public void getRange() { - } + public void getRange() { + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java index 96c6da8..3a10a28 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Inhabitant.java @@ -4,11 +4,11 @@ import ca.cosc3p91.a2.game.Map; public interface Inhabitant { - public Map map = null; - public Building building = null; + Map map = null; + Building building = null; - public void move(Tile t); + void move(Tile t); - public void getPosition(); + void getPosition(); } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java index 296fc76..69e1faf 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/IronMine.java @@ -2,10 +2,10 @@ package ca.cosc3p91.a2.gameobjects; public class IronMine extends ResourceBuidling { - public static String resource = "iron"; + public static String resource = "iron"; - @Override - public void harvest(Village_Hall hall) { + @Override + public void harvest(Village_Hall hall) { - } + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java index 658839f..68c7492 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/LumberMine.java @@ -2,10 +2,10 @@ package ca.cosc3p91.a2.gameobjects; public class LumberMine extends ResourceBuidling { - public static String resource = "wood"; + public static String resource = "wood"; - public void harvest(Village_Hall hall) { + public void harvest(Village_Hall hall) { + + } - } - } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceBuidling.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceBuidling.java index 3560777..c0c9fbc 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceBuidling.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceBuidling.java @@ -2,10 +2,10 @@ package ca.cosc3p91.a2.gameobjects; public abstract class ResourceBuidling extends Building { - public static String resource; + public static String resource; - private int harvest_rate; + private int harvest_rate; - public abstract void harvest(Village_Hall hall); + public abstract void harvest(Village_Hall hall); } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceStage.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceStage.java index 90401bf..07f14bb 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceStage.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/ResourceStage.java @@ -2,10 +2,10 @@ package ca.cosc3p91.a2.gameobjects; public class ResourceStage extends Stage { - protected int harvestRateIncrease; + protected int harvestRateIncrease; - public int getHarvestRateIncrease() { - return harvestRateIncrease; - } + public int getHarvestRateIncrease() { + return harvestRateIncrease; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Stage.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Stage.java index 2779c75..8a78efa 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Stage.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Stage.java @@ -2,33 +2,33 @@ package ca.cosc3p91.a2.gameobjects; abstract class Stage { - protected int dHealth; + protected int dHealth; - protected int goldCost; + protected int goldCost; - protected int requiredVillageLevel; + protected int requiredVillageLevel; - protected int upgradeTime; + protected int upgradeTime; - protected int ironCost; + protected int ironCost; - protected int woodCost; + protected int woodCost; - public Building building; + public Building building; - public void getHealthChange() { - } + public void getHealthChange() { + } - public int getCost(String type) { - return 0; - } + public int getCost(String type) { + return 0; + } - public int getRequiredVillageLevel() { - return requiredVillageLevel; - } + public int getRequiredVillageLevel() { + return requiredVillageLevel; + } - public int getUpgradeTime() { - return upgradeTime; - } + public int getUpgradeTime() { + return upgradeTime; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Tile.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Tile.java index 708c2e4..e29ec41 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Tile.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Tile.java @@ -4,10 +4,10 @@ import java.util.List; public class Tile { - public int x; + public int x; - public int y; + public int y; - public List building; + public List building; } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/VillageStage.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/VillageStage.java index 225a504..e4e4f95 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/VillageStage.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/VillageStage.java @@ -2,22 +2,22 @@ package ca.cosc3p91.a2.gameobjects; class VillageStage extends Stage { - protected int goldCapacityIncrease; + protected int goldCapacityIncrease; - protected int ironCapacityIncrease; + protected int ironCapacityIncrease; - protected int woodCapacityIncrease; + protected int woodCapacityIncrease; - public int getGoldCapacityIncrease() { - return goldCapacityIncrease; - } + public int getGoldCapacityIncrease() { + return goldCapacityIncrease; + } - public int getIronCapacityIncrease() { - return ironCapacityIncrease; - } + public int getIronCapacityIncrease() { + return ironCapacityIncrease; + } - public int getWoodCapacityIncrease() { - return woodCapacityIncrease; - } + public int getWoodCapacityIncrease() { + return woodCapacityIncrease; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Village_Hall.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Village_Hall.java index ca5a801..f651385 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Village_Hall.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Village_Hall.java @@ -2,22 +2,22 @@ package ca.cosc3p91.a2.gameobjects; public class Village_Hall extends Building { - private int goldCapacity; + private int goldCapacity; - private int ironCapacity; + private int ironCapacity; - private int woodCapacity; + private int woodCapacity; - public int getGoldCapacity() { - return goldCapacity; - } + public int getGoldCapacity() { + return goldCapacity; + } - public int getIronCapacity() { - return ironCapacity; - } + public int getIronCapacity() { + return ironCapacity; + } - public int getWoodCapacity() { - return woodCapacity; - } + public int getWoodCapacity() { + return woodCapacity; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Worker.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Worker.java index ae319b1..90255c0 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Worker.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/Worker.java @@ -2,10 +2,10 @@ package ca.cosc3p91.a2.gameobjects; public class Worker { - private boolean currentlyBuilding; + private boolean currentlyBuilding; - public boolean isCurrentlyBuilding() { - return currentlyBuilding; - } + public boolean isCurrentlyBuilding() { + return currentlyBuilding; + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/player/Player.java b/Assignment 2/src/ca/cosc3p91/a2/player/Player.java index 6508591..f6121ab 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/player/Player.java +++ b/Assignment 2/src/ca/cosc3p91/a2/player/Player.java @@ -2,10 +2,10 @@ package ca.cosc3p91.a2.player; public class Player { - public int currentGold; + public int currentGold; - public int currentIron; + public int currentIron; - public int currentWood; + public int currentWood; } \ No newline at end of file From 3b03bee19abd8ed1a4e9e93b970b08836a8399e5 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 1 Mar 2023 13:55:46 -0500 Subject: [PATCH 2/5] Add time --- .idea/codeStyles/codeStyleConfig.xml | 5 ++ .../src/ca/cosc3p91/a2/util/Print.java | 7 +++ .../src/ca/cosc3p91/a2/util/Time.java | 48 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 Assignment 2/src/ca/cosc3p91/a2/util/Print.java create mode 100644 Assignment 2/src/ca/cosc3p91/a2/util/Time.java diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/util/Print.java b/Assignment 2/src/ca/cosc3p91/a2/util/Print.java new file mode 100644 index 0000000..0cf0ac4 --- /dev/null +++ b/Assignment 2/src/ca/cosc3p91/a2/util/Print.java @@ -0,0 +1,7 @@ +package ca.cosc3p91.a2.util; + +public class Print { + + + +} diff --git a/Assignment 2/src/ca/cosc3p91/a2/util/Time.java b/Assignment 2/src/ca/cosc3p91/a2/util/Time.java new file mode 100644 index 0000000..d006449 --- /dev/null +++ b/Assignment 2/src/ca/cosc3p91/a2/util/Time.java @@ -0,0 +1,48 @@ +package ca.cosc3p91.a2.util; + +public class Time { + + private long timeSeconds; + + protected Time(long timeSeconds) { + this.timeSeconds = timeSeconds; + } + + public Time offsetSeconds(long seconds){ + 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 long get(){ + return timeSeconds; + } + + public static class FutureTime { + private final Time futureTime; + + public FutureTime(Time futureTime){ + this.futureTime = futureTime; + } + + public boolean occurred(){ + return getTime().timeSeconds >= futureTime.timeSeconds; + } + } + + public static Time getTime(){ + return new Time(System.currentTimeMillis() / 1000); + } + +} From e8e137694740a017f557da1e2506d996d2f514e0 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 1 Mar 2023 15:38:51 -0500 Subject: [PATCH 3/5] printy printing of tables --- .idea/uiDesigner.xml | 124 +++++++++++ Assignment 2/src/ca/cosc3p91/a2/Main.java | 18 ++ .../src/ca/cosc3p91/a2/util/Print.java | 202 ++++++++++++++++++ .../src/ca/cosc3p91/a2/util/Time.java | 22 +- 4 files changed, 359 insertions(+), 7 deletions(-) create mode 100644 .idea/uiDesigner.xml diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment 2/src/ca/cosc3p91/a2/Main.java b/Assignment 2/src/ca/cosc3p91/a2/Main.java index 18130b1..e808fc1 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/Main.java +++ b/Assignment 2/src/ca/cosc3p91/a2/Main.java @@ -1,12 +1,30 @@ package ca.cosc3p91.a2; import ca.cosc3p91.a2.game.GameEngine; +import ca.cosc3p91.a2.util.Print; public class Main { public static void main(String[] args) { GameEngine engine = new GameEngine(); + Print test = new Print("Hello There", 2, 500); + + test.addColumn(new Print.Column("I am value 1")); + test.addColumn(new Print.Column("Super Value")); + test.addColumn(new Print.Column("SOLD!")); + + Print.Row row = new Print.Row(); + row.add("HelloThere"); + row.add("Goodbye"); + row.add("3"); + + test.addRow(row); + test.addRow(row); + test.addRow(row); + + Print.print(test.createTable()); + engine.run(); } diff --git a/Assignment 2/src/ca/cosc3p91/a2/util/Print.java b/Assignment 2/src/ca/cosc3p91/a2/util/Print.java index 0cf0ac4..e8dd54f 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/util/Print.java +++ b/Assignment 2/src/ca/cosc3p91/a2/util/Print.java @@ -1,7 +1,209 @@ package ca.cosc3p91.a2.util; +import java.util.ArrayList; + +/** + * 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 void add(String value){ + values.add(value); + } + } + + public static class Column { + private final 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 String tableName; + private int columnPadding; + private int maxColumnWidth; + + public Print(String tableName, int columnPadding, int maxColumnWidth){ + this.tableName = tableName; + this.columnPadding = columnPadding; + this.maxColumnWidth = maxColumnWidth; + } + + public Print(){ + this("", 2, 500); + } + + 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() - 1); + } + } + } + + 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){ + ArrayList lines = new ArrayList<>(); + + String header = generateColumnHeader(); + String topSeparator = generateTopSelector(header.length()); + String lineSeparator = generateSeparator(header.length() - 1); + + 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 static void print(ArrayList lines){ + for (String line : lines) + System.out.println(line); + } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/util/Time.java b/Assignment 2/src/ca/cosc3p91/a2/util/Time.java index d006449..60d5f93 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/util/Time.java +++ b/Assignment 2/src/ca/cosc3p91/a2/util/Time.java @@ -4,12 +4,16 @@ public class Time { private long timeSeconds; + public Time() { + this.timeSeconds = 0; + } + protected Time(long timeSeconds) { this.timeSeconds = timeSeconds; } - public Time offsetSeconds(long seconds){ - timeSeconds += seconds; + public Time offsetSeconds(long seconds) { + this.timeSeconds += seconds; return this; } @@ -21,27 +25,31 @@ public class Time { return offsetMinutes(hours * 60); } - public Time offsetDays(long days){ + public Time offsetDays(long days) { return offsetHours(days * 24); } - public long get(){ + public Time offsetTime(Time time) { + return offsetSeconds(time.timeSeconds); + } + + public long get() { return timeSeconds; } public static class FutureTime { private final Time futureTime; - public FutureTime(Time futureTime){ + public FutureTime(Time futureTime) { this.futureTime = futureTime; } - public boolean occurred(){ + public boolean occurred() { return getTime().timeSeconds >= futureTime.timeSeconds; } } - public static Time getTime(){ + public static Time getTime() { return new Time(System.currentTimeMillis() / 1000); } From 168c5364d153147938861d5d5010e2a97120a058 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 1 Mar 2023 15:47:09 -0500 Subject: [PATCH 4/5] ignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index e69de29..62f45e8 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,2 @@ +Assignment 2/out/ +out/ From abf784868daa920a9ca8b3b9d291a7cf521aa9c7 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 1 Mar 2023 16:00:40 -0500 Subject: [PATCH 5/5] remove unused vars --- Assignment 2/src/ca/cosc3p91/a2/Main.java | 2 +- Assignment 2/src/ca/cosc3p91/a2/util/Print.java | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Assignment 2/src/ca/cosc3p91/a2/Main.java b/Assignment 2/src/ca/cosc3p91/a2/Main.java index e808fc1..b74c2e5 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/Main.java +++ b/Assignment 2/src/ca/cosc3p91/a2/Main.java @@ -8,7 +8,7 @@ public class Main { public static void main(String[] args) { GameEngine engine = new GameEngine(); - Print test = new Print("Hello There", 2, 500); + Print test = new Print("Hello There", 2); test.addColumn(new Print.Column("I am value 1")); test.addColumn(new Print.Column("Super Value")); diff --git a/Assignment 2/src/ca/cosc3p91/a2/util/Print.java b/Assignment 2/src/ca/cosc3p91/a2/util/Print.java index e8dd54f..2d8ef59 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/util/Print.java +++ b/Assignment 2/src/ca/cosc3p91/a2/util/Print.java @@ -36,18 +36,17 @@ public class Print { private final ArrayList rows = new ArrayList<>(); private final ArrayList columns = new ArrayList<>(); - private String tableName; - private int columnPadding; + private final String tableName; + private final int columnPadding; private int maxColumnWidth; - public Print(String tableName, int columnPadding, int maxColumnWidth){ + public Print(String tableName, int columnPadding){ this.tableName = tableName; this.columnPadding = columnPadding; - this.maxColumnWidth = maxColumnWidth; } public Print(){ - this("", 2, 500); + this("", 2); } private String createPadding(int amount) {