154 lines
4.8 KiB
Java
154 lines
4.8 KiB
Java
/*Drew Schuster
|
|
dtschust
|
|
ECE462
|
|
*/
|
|
|
|
package com.github.dtschust.zork;
|
|
|
|
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.Iterator;
|
|
import java.util.Scanner;
|
|
|
|
import static com.github.dtschust.zork.Zork.Type.*;
|
|
|
|
/* And away we go*/
|
|
public class Zork {
|
|
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()) {
|
|
String userInput = source.nextLine();
|
|
|
|
/*Now that we have the user command, check the input*/
|
|
if (!executeTriggers(userInput)) {
|
|
/* 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!*/
|
|
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(String input) {
|
|
|
|
/*Variable initialization*/
|
|
boolean skip;
|
|
|
|
/*Check Room triggers*/
|
|
skip = doTriggersRoom(input);
|
|
/* Check items in the containers in the room */
|
|
skip = skip || doTriggersItemsInContainersInRoom(input);
|
|
/* Check all containers in the room*/
|
|
skip = skip || doTriggersContainersInRoom(input);
|
|
/* Check all creatures in the room */
|
|
skip = skip || doTriggersCreaturesInRoom(input);
|
|
/* Check items in inventory */
|
|
skip = skip || doTriggersItemsInInventory(input);
|
|
/* Check items in room */
|
|
skip = skip || doTriggersItemsInRoom(input);
|
|
|
|
return skip;
|
|
}
|
|
|
|
private boolean doTriggersContainersInRoom(String input) {
|
|
boolean skip = false;
|
|
for (String key : game.getCurrentRoom().container) {
|
|
skip = skip || doZorkTriggers(game.get(CONTAINER, key), input);
|
|
}
|
|
return skip;
|
|
}
|
|
|
|
private boolean doTriggersItemsInContainersInRoom(String input) {
|
|
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), input);
|
|
}
|
|
}
|
|
return skip;
|
|
}
|
|
|
|
private boolean doTriggersItemsInRoom(String input) {
|
|
boolean skip = false;
|
|
for (String key : game.getCurrentRoom().item) {
|
|
skip = skip || doZorkTriggers(game.get(ITEM, key), input);
|
|
}
|
|
return skip;
|
|
}
|
|
|
|
private boolean doTriggersItemsInInventory(String input) {
|
|
boolean skip = false;
|
|
for (String key : game.inventory) {
|
|
skip = skip || doZorkTriggers(game.get(ITEM, key), input);
|
|
}
|
|
return skip;
|
|
}
|
|
|
|
private boolean doTriggersCreaturesInRoom(String input) {
|
|
boolean skip = false;
|
|
for (String key : game.getCurrentRoom().creature) {
|
|
skip = skip || doZorkTriggers(game.get(CREATURE, key), input);
|
|
}
|
|
return skip;
|
|
}
|
|
|
|
private boolean doTriggersRoom(String input) {
|
|
return doZorkTriggers(game.getCurrentRoom(), input);
|
|
}
|
|
|
|
private boolean doZorkTriggers(ZorkObject zorkObject, String input) {
|
|
boolean skip = false;
|
|
Iterator<ZorkTrigger> iterator = zorkObject.trigger.iterator();
|
|
while (iterator.hasNext()) {
|
|
ZorkTrigger tempTrigger = iterator.next();
|
|
if (tempTrigger.evaluate(game, input)) {
|
|
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")) {
|
|
iterator.remove();
|
|
}
|
|
}
|
|
}
|
|
return skip;
|
|
}
|
|
|
|
|
|
public enum Type {ROOM, ITEM, CONTAINER, CREATURE}
|
|
|
|
}
|