This repository has been archived on 2022-12-21. You can view files and clone it, but cannot push or open issues or pull requests.
sdm03/src/main/java/com/github/dtschust/zork/Zork.java

155 lines
4.6 KiB
Java

/*Drew Schuster
dtschust
ECE462
*/
package com.github.dtschust.zork;
import com.github.dtschust.zork.parser.ZorkGame;
import com.github.dtschust.zork.parser.ZorkReader;
import com.github.dtschust.zork.repl.ActionDispatcher;
import com.github.dtschust.zork.types.ZorkContainer;
import com.github.dtschust.zork.types.ZorkObject;
import java.util.Scanner;
/* And away we go*/
public class Zork {
public String userInput;
public ZorkGame game;
Scanner source = new Scanner(System.in);
public Zork(String filename) {
game = new ZorkReader(filename).build();
game.changeRoom("Entrance");
/* Print out the first entrance description, starting the game!*/
System.out.println(game.getCurrentRoom().description);
ActionDispatcher d = new ActionDispatcher(game);
/* There is no stopping in Zork, until we're done!!*/
while (game.isRunning()) {
userInput = source.nextLine();
/*Now that we have the user command, check the input*/
if (!executeTriggers()) {
/* If we haven't skipped, perform the user action*/
d.dispatch(userInput);
/* Clear the user input, and check the triggers again (various states have changed, gnomes need to be found!*/
userInput = "";
executeTriggers();
}
}
// single point of termination
System.exit(0);
}
/* I love how basic java main functions are sometimes.*/
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java Zork [filename]");
return;
}
new Zork(args[0]);
}
/* Check triggers */
public boolean executeTriggers() {
/*Variable initialization*/
boolean skip;
/*Check Room triggers*/
skip = doTriggersRoom();
/* Check items in the containers in the room */
skip = skip || doTriggersItemsInContainersInRoom();
/* Check all containers in the room*/
skip = skip || doTriggersContainersInRoom();
/* Check all creatures in the room */
skip = skip || doTriggersCreaturesInRoom();
/* Check items in inventory */
skip = skip || doTriggersItemsInInventory();
/* Check items in room */
skip = skip || doTriggersItemsInRoom();
return skip;
}
private boolean doTriggersContainersInRoom() {
boolean skip = false;
for (String key : game.getCurrentRoom().container) {
skip = skip || doZorkTriggers(game.get("container", key));
}
return skip;
}
private boolean doTriggersItemsInContainersInRoom() {
boolean skip = false;
for (String key : game.getCurrentRoom().container) {
ZorkContainer tempContainer = (ZorkContainer) game.get("container", key);
for (String key2 : tempContainer.item) {
skip = skip || doZorkTriggers(game.get("item", key2));
}
}
return skip;
}
private boolean doTriggersItemsInRoom() {
boolean skip = false;
for (String key : game.getCurrentRoom().item) {
skip = skip || doZorkTriggers(game.get("item", key));
}
return skip;
}
private boolean doTriggersItemsInInventory() {
boolean skip = false;
for (String key : game.inventory) {
skip = skip || doZorkTriggers(game.get("item", key));
}
return skip;
}
private boolean doTriggersCreaturesInRoom() {
boolean skip = false;
for (String key : game.getCurrentRoom().creature) {
skip = skip || doZorkTriggers(game.get("creature", key));
}
return skip;
}
private boolean doTriggersRoom() {
return doZorkTriggers(game.getCurrentRoom());
}
private boolean doZorkTriggers(ZorkObject zorkObject) {
boolean skip = false;
for (int x = zorkObject.trigger.size() - 1; x >= 0; x--) {
ZorkTrigger tempTrigger = zorkObject.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (String print : tempTrigger.print) {
System.out.println(print);
}
final ActionDispatcher d = new ActionDispatcher(game);
for (String action : tempTrigger.action) {
d.dispatch(action);
}
skip = skip || tempTrigger.hasCommand();
if (tempTrigger.type.equals("single")) {
zorkObject.trigger.remove(x);
}
}
}
return skip;
}
}