Added prototype view class 'GameDisplay' to fulfil MVC Design Pattern

main
mike 2023-03-23 21:04:21 -04:00
parent a58273d787
commit 9325ab21ad
3 changed files with 97 additions and 77 deletions

View File

@ -4,15 +4,9 @@ import ca.cosc3p91.a2.gameobjects.*;
import ca.cosc3p91.a2.gameobjects.factory.BuildingFactory; import ca.cosc3p91.a2.gameobjects.factory.BuildingFactory;
import ca.cosc3p91.a2.gameobjects.factory.InhabitantFactory; import ca.cosc3p91.a2.gameobjects.factory.InhabitantFactory;
import ca.cosc3p91.a2.player.*; import ca.cosc3p91.a2.player.*;
import ca.cosc3p91.a2.util.Print; import ca.cosc3p91.a2.userinterface.GameDisplay;
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.Random;
import java.util.Scanner;
public class GameEngine<T> implements Runnable { public class GameEngine<T> implements Runnable {
@ -30,69 +24,13 @@ public class GameEngine<T> implements Runnable {
private final Random random = new Random(System.nanoTime()); private final Random random = new Random(System.nanoTime());
public Map map; public Map map;
public GameDisplay view;
public GameEngine() { public GameEngine() {
player = new Player(); player = new Player();
map = generateInitialMap(); 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 <building name>'}\n" +
"2. Train inhabitants {command: '2 <unit name>'}\n"+
"3. Upgrade {command: '3 i<index>'} / {command: '3 b<index>'}\n"+
"4. Explore\n"+
"5. Print Village Stats\n"+
"6. Quit\n" +
"7. Attack last explored\n");
}
public void attackVillage(Map map) { public void attackVillage(Map map) {
int defenseiveCounter = 1; int defenseiveCounter = 1;
int inhabCounter = 0; int inhabCounter = 0;
@ -202,10 +140,12 @@ public class GameEngine<T> implements Runnable {
@Override @Override
public void run() { public void run() {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); String in;
Scanner sc = new Scanner(rd);
printState(this.map,"Current Village State"); view = new GameDisplay();
printMenuOptions(); view.printVillageState(this.map,"Current Village State");
view.printGameMenu();
System.out.println(); System.out.println();
Map exploringMap = null; Map exploringMap = null;
boolean deleteMyHeart = true; boolean deleteMyHeart = true;
@ -216,8 +156,7 @@ public class GameEngine<T> implements Runnable {
} }
} }
try { try {
if (rd.ready()) { if ((in = view.nextInput()) != null) {
String in = sc.nextLine();
String[] args = in.split(" "); String[] args = in.split(" ");
System.out.println("Your Input: "); System.out.println("Your Input: ");
System.out.println("\t->" + in + '\n'); System.out.println("\t->" + in + '\n');
@ -279,7 +218,7 @@ public class GameEngine<T> implements Runnable {
case '4': case '4':
deleteMyHeart = false; deleteMyHeart = false;
exploringMap = generateMap(); exploringMap = generateMap();
printState(exploringMap, "Other Village"); view.printVillageState(exploringMap,"Other Village");
break; break;
case '7': case '7':
if (exploringMap != null) if (exploringMap != null)
@ -288,7 +227,7 @@ public class GameEngine<T> implements Runnable {
System.out.println("Error: Explored map is null. Have you explored last command?"); System.out.println("Error: Explored map is null. Have you explored last command?");
break; break;
case '5': case '5':
printState(this.map,"Home Village"); view.printVillageState(this.map,"Home Village");
break; break;
case '6': case '6':
System.exit(0); System.exit(0);
@ -296,7 +235,7 @@ public class GameEngine<T> implements Runnable {
default: default:
break; break;
} }
printMenuOptions(); view.printGameMenu();
} }
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@ -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 <building name>'}\n" +
"2. Train inhabitants {command: '2 <unit name>'}\n"+
"3. Upgrade {command: '3 i<index>'} / {command: '3 b<index>'}\n"+
"4. Explore\n"+
"5. Print Village Stats\n"+
"6. Quit\n" +
"7. Attack last explored\n");
}
}

View File

@ -1,4 +0,0 @@
package ca.cosc3p91.a2.userinterface;
public class GuiManager {
}