From 9325ab21ad7ff51032d380f3af07ee9e9f049e1b Mon Sep 17 00:00:00 2001 From: mike Date: Thu, 23 Mar 2023 21:04:21 -0400 Subject: [PATCH] Added prototype view class 'GameDisplay' to fulfil MVC Design Pattern --- .../src/ca/cosc3p91/a2/game/GameEngine.java | 85 +++---------------- .../a2/userinterface/GameDisplay.java | 85 +++++++++++++++++++ .../cosc3p91/a2/userinterface/GuiManager.java | 4 - 3 files changed, 97 insertions(+), 77 deletions(-) create mode 100644 Assignment 3/src/ca/cosc3p91/a2/userinterface/GameDisplay.java delete mode 100644 Assignment 3/src/ca/cosc3p91/a2/userinterface/GuiManager.java diff --git a/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java b/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java index f47122b..8758d4c 100644 --- a/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java +++ b/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java @@ -4,15 +4,9 @@ import ca.cosc3p91.a2.gameobjects.*; import ca.cosc3p91.a2.gameobjects.factory.BuildingFactory; import ca.cosc3p91.a2.gameobjects.factory.InhabitantFactory; import ca.cosc3p91.a2.player.*; -import ca.cosc3p91.a2.util.Print; -import ca.cosc3p91.a2.util.Time; -import ca.cosc3p91.a2.util.Util; +import ca.cosc3p91.a2.userinterface.GameDisplay; -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 { @@ -30,69 +24,13 @@ public class GameEngine implements Runnable { private final Random random = new Random(System.nanoTime()); public Map map; + public GameDisplay view; 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; @@ -202,10 +140,12 @@ public class GameEngine implements Runnable { @Override public void run() { - BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); - Scanner sc = new Scanner(rd); - printState(this.map,"Current Village State"); - printMenuOptions(); + String in; + + view = new GameDisplay(); + view.printVillageState(this.map,"Current Village State"); + view.printGameMenu(); + System.out.println(); Map exploringMap = null; boolean deleteMyHeart = true; @@ -216,8 +156,7 @@ public class GameEngine implements Runnable { } } try { - if (rd.ready()) { - String in = sc.nextLine(); + if ((in = view.nextInput()) != null) { String[] args = in.split(" "); System.out.println("Your Input: "); System.out.println("\t->" + in + '\n'); @@ -279,7 +218,7 @@ public class GameEngine implements Runnable { case '4': deleteMyHeart = false; exploringMap = generateMap(); - printState(exploringMap, "Other Village"); + view.printVillageState(exploringMap,"Other Village"); break; case '7': if (exploringMap != null) @@ -288,7 +227,7 @@ public class GameEngine implements Runnable { System.out.println("Error: Explored map is null. Have you explored last command?"); break; case '5': - printState(this.map,"Home Village"); + view.printVillageState(this.map,"Home Village"); break; case '6': System.exit(0); @@ -296,7 +235,7 @@ public class GameEngine implements Runnable { default: break; } - printMenuOptions(); + view.printGameMenu(); } } catch (Exception e) { throw new RuntimeException(e); diff --git a/Assignment 3/src/ca/cosc3p91/a2/userinterface/GameDisplay.java b/Assignment 3/src/ca/cosc3p91/a2/userinterface/GameDisplay.java new file mode 100644 index 0000000..cb58f3d --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/userinterface/GameDisplay.java @@ -0,0 +1,85 @@ +package ca.cosc3p91.a2.userinterface; + +import ca.cosc3p91.a2.game.Map; +import ca.cosc3p91.a2.gameobjects.Building; +import ca.cosc3p91.a2.gameobjects.Inhabitant; +import ca.cosc3p91.a2.util.Print; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Scanner; + +public class GameDisplay { + private BufferedReader reader; + private Scanner scanner; + private String input; + + public GameDisplay() { + reader = new BufferedReader(new InputStreamReader(System.in)); + scanner = new Scanner(reader); + } + + public String nextInput() throws IOException { + if (reader.ready()) { + return scanner.nextLine(); + } else return null; + } + + public void printVillageState(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)); + } + + public void printGameMenu() { + 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"); + } +} \ 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 deleted file mode 100644 index 45e4b54..0000000 --- a/Assignment 3/src/ca/cosc3p91/a2/userinterface/GuiManager.java +++ /dev/null @@ -1,4 +0,0 @@ -package ca.cosc3p91.a2.userinterface; - -public class GuiManager { -} \ No newline at end of file