chore: migrate to Maven project, update README

This commit is contained in:
Claudio Maggioni 2022-11-16 16:18:00 +01:00
parent 1ba801d9e3
commit 2406dac418
16 changed files with 1285 additions and 1460 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/*.iml
/.idea/
/target/

View File

@ -1,5 +0,0 @@
all:
javac *.java
clean:
rm -rf *.class

View File

@ -2,3 +2,21 @@
Original and refactored sources for the https://github.com/dtschust/Zork Github
project.
# Building
Build the project with the command:
```shell
mvn clean package
```
# Executing
Example inputs can be found in RunThroughResults.txt as to how to beat the sample game. To run the sample game execute:
```shell
mvn exc:java
```
This command will load the `sampleGame.xml` XML game spec.

1455
Zork.java

File diff suppressed because it is too large Load Diff

55
pom.xml Normal file
View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.dtschust</groupId>
<artifactId>zork</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>com.github.dtschust.zork.Zork</mainClass>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>${mainClass}</mainClass>
<arguments>
<argument>${project.basedir}/sampleGame.xml</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,13 @@
package com.github.dtschust.zork;
/* Special Command condition*/
class ZorkCommand extends ZorkCondition {
String command;
public boolean evaluate(Zork zork) {
if (command.equals(zork.userInput))
return true;
else
return false;
}
}

View File

@ -0,0 +1,8 @@
package com.github.dtschust.zork;
/* Generic condition*/
abstract class ZorkCondition {
String object;
public abstract boolean evaluate(Zork zork);
}

View File

@ -0,0 +1,42 @@
package com.github.dtschust.zork;
/* Has conditions*/
class ZorkConditionHas extends ZorkCondition {
String has;
String owner;
public boolean evaluate(Zork zork) {
/*Inventory is a special case as it isn't the name of any object in the game, check for it specifically*/
if (owner.equals("inventory")) {
if (zork.Inventory.get(object) != null && has.equals("yes") || zork.Inventory.get(object) == null && has.equals("no")) {
return true;
} else {
return false;
}
} else {
/* is it a room?*/
ZorkRoom roomObject = zork.Rooms.get(owner);
if (roomObject != null) {
if ((roomObject).item.get(object) != null && has.equals("yes") || (roomObject).item.get(object) == null && has.equals("no")) {
return true;
} else {
return false;
}
}
/* is it a container?*/
else {
ZorkContainer containerObject = zork.Containers.get(owner);
if (containerObject != null) {
if ((containerObject).item.get(object) != null && has.equals("yes") || (containerObject).item.get(object) == null && has.equals("no")) {
return true;
} else {
return false;
}
}
}
}
return false;
}
}

View File

@ -0,0 +1,14 @@
package com.github.dtschust.zork;
/* Status conditions*/
class ZorkConditionStatus extends ZorkCondition {
String status;
public boolean evaluate(Zork zork) {
ZorkObject tested = zork.Objects.get(object);
if (tested != null && tested.status.equals(status))
return true;
else
return false;
}
}

View File

@ -0,0 +1,16 @@
package com.github.dtschust.zork;
import java.util.ArrayList;
import java.util.HashMap;
/* Container*/
class ZorkContainer extends ZorkObject {
public String name;
public HashMap<String, String> item = new HashMap<String, String>();
public String description;
public ArrayList<String> accept = new ArrayList<String>();
boolean isOpen;
public ZorkContainer() {
}
}

View File

@ -0,0 +1,30 @@
package com.github.dtschust.zork;
import java.util.ArrayList;
import java.util.HashMap;
/* Creature*/
class ZorkCreature extends ZorkObject {
public String name;
public String description;
public HashMap<String, String> vulnerability = new HashMap<String, String>();
public ArrayList<ZorkCondition> conditions = new ArrayList<ZorkCondition>();
public ArrayList<String> print = new ArrayList<String>();
public ArrayList<String> action = new ArrayList<String>();
public ZorkCreature() {
}
/* Evaluate the success of an attack*/
public boolean attack(Zork zork, String weapon) {
if (vulnerability.get(weapon) == null) {
return false;
}
for (int i = 0; i < conditions.size(); i++) {
if (!conditions.get(i).evaluate(zork)) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,15 @@
package com.github.dtschust.zork;
import java.util.ArrayList;
/* Item*/
class ZorkItem extends ZorkObject {
public String name;
public String description;
public String writing;
public ArrayList<String> turnOnPrint = new ArrayList<String>();
public ArrayList<String> turnOnAction = new ArrayList<String>();
public ZorkItem() {
}
}

View File

@ -0,0 +1,12 @@
package com.github.dtschust.zork;
import java.util.ArrayList;
/* Generic object, everything inherits from this*/
class ZorkObject {
public String status;
public ArrayList<ZorkTrigger> trigger = new ArrayList<ZorkTrigger>();
public ZorkObject() {
}
}

View File

@ -0,0 +1,17 @@
package com.github.dtschust.zork;
import java.util.HashMap;
/* Room*/
class ZorkRoom extends ZorkObject {
public String name;
public String type = "regular";
public String description;
public HashMap<String, String> border = new HashMap<String, String>();
public HashMap<String, String> container = new HashMap<String, String>();
public HashMap<String, String> item = new HashMap<String, String>();
public HashMap<String, String> creature = new HashMap<String, String>();
public ZorkRoom() {
}
}

View File

@ -0,0 +1,21 @@
package com.github.dtschust.zork;
import java.util.ArrayList;
/*Trigger*/
class ZorkTrigger {
public ArrayList<ZorkCondition> conditions = new ArrayList<ZorkCondition>();
public ArrayList<String> print = new ArrayList<String>();
public ArrayList<String> action = new ArrayList<String>();
String type = "single"; /*By default, single*/
boolean hasCommand = false;
public boolean evaluate(Zork zork) {
for (int i = 0; i < conditions.size(); i++) {
if (!conditions.get(i).evaluate(zork)) {
return false;
}
}
return true;
}
}