Browse Source

Merge pull request #1 from RedEagle-dh/develop into master

Features of this pull request
Interaction loop
JSON parser
Extern boot class
Command interface
Command listener
Help command
Quit command
2 unit tests for the commands
Interaction loop
The main game engine. This engine is looping through interactions and is asking for a user input every time.

JSON parser
A help class to read keys and values from JSON files. With that comes the library "org.json".

Extern boot class
An instance of this class is generated from the main class. This is to make the command handling easier.

Command interface
This is an interface for general commands. Every command has the same method and different own methods.

Command listener
All commands are registered here.

Help command
This command prints a list of all available commands with description for the player.

Quit command
This command ends the game.

Unit tests
The two unit tests for the two commands are checking if the output string is correct or if the output string exists.
remotes/origin/Julia
RedEagle 2 years ago
committed by GitHub
parent
commit
e2d8e6ffb2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      .gitignore
  2. 2
      build-project.sh
  3. 9
      pom.xml
  4. 23
      src/main/java/org/bitbiome/Boot.java
  5. 21
      src/main/java/org/bitbiome/InteractionLoop.java
  6. 30
      src/main/java/org/bitbiome/JsonParser.java
  7. 12
      src/main/java/org/bitbiome/Main.java
  8. 8
      src/main/java/org/bitbiome/commands/CommandAPI.java
  9. 35
      src/main/java/org/bitbiome/commands/CommandListener.java
  10. 23
      src/main/java/org/bitbiome/commands/HelpCommand.java
  11. 16
      src/main/java/org/bitbiome/commands/QuitCommand.java
  12. 13
      src/main/java/org/example/Main.java
  13. 3
      src/main/resources/crafting.json
  14. 38
      src/main/resources/gameconfig.json
  15. 26
      src/main/resources/items.json
  16. 17
      src/main/resources/playerconfig.json
  17. 15
      src/test/java/org/bitbiome/commands/HelpCommandTest.java
  18. 15
      src/test/java/org/bitbiome/commands/QuitCommandTest.java
  19. 13
      src/test/java/org/example/TestMain.java

4
.gitignore

@ -24,6 +24,6 @@ hs_err_pid*
replay_pid* replay_pid*
.DS_Store .DS_Store
/.idea/ /.idea/
/.out/
/.target/
/out/
/target/
*.iml *.iml

2
build-project.sh

@ -1,3 +1,3 @@
mvn test mvn test
java src/main/java/org/example/Main.java
mvn compile exec:java -Dexec.mainClass="org.bitbiome.Main"

9
pom.xml

@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 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> <modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<groupId>org.bitbiome</groupId>
<artifactId>BitBiome</artifactId> <artifactId>BitBiome</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
@ -24,6 +24,13 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- Working with JSON Files -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

23
src/main/java/org/bitbiome/Boot.java

@ -0,0 +1,23 @@
package org.bitbiome;
import org.bitbiome.commands.CommandListener;
public class Boot {
private CommandListener cmdListener;
public static Boot instance;
public Boot() {
instance = this;
cmdListener = new CommandListener();
InteractionLoop game = new InteractionLoop();
game.run();
}
public CommandListener getCmdListener(){
return cmdListener;
}
}

21
src/main/java/org/bitbiome/InteractionLoop.java

@ -0,0 +1,21 @@
package org.bitbiome;
import java.util.Scanner;
public class InteractionLoop {
Scanner input = new Scanner(System.in);
public void run() {
boolean isRunning = true;
while (isRunning) {
String line = input.nextLine().toLowerCase();
if (!Boot.instance.getCmdListener().perform(line.toLowerCase().split(" ")[0], input, isRunning, line)) {
System.out.println("Unknown Command");
}
}
}
}

30
src/main/java/org/bitbiome/JsonParser.java

@ -0,0 +1,30 @@
package org.bitbiome;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.InputStream;
public class JsonParser {
public static JSONObject getJSONObject(String fileName) {
String resourceName = "./../../" + fileName;
InputStream is = JsonParser.class.getResourceAsStream(resourceName);
if (is == null) {
throw new NullPointerException("Cannot find resource file " + resourceName);
}
JSONTokener tokener = new JSONTokener(is);
JSONObject object = new JSONObject(tokener);
System.out.println("Name: " + object.getString("name"));
System.out.println("Inventory: ");
JSONArray inventory = object.getJSONArray("inventory");
for (int i = 0; i < inventory.length(); i++) {
JSONObject invObj = inventory.getJSONObject(i);
System.out.println(" - " + invObj.getString("name"));
}
return object;
}
}

12
src/main/java/org/bitbiome/Main.java

@ -0,0 +1,12 @@
package org.bitbiome;
public class Main {
public static void main(String[] args) {
//JSONObject playerConfig = JsonParser.getJSONObject("playerconfig.json");
new Boot();
}
}

8
src/main/java/org/bitbiome/commands/CommandAPI.java

@ -0,0 +1,8 @@
package org.bitbiome.commands;
import java.util.Scanner;
public interface CommandAPI {
public void performCommand(Scanner scanner, boolean isRunning, String message);
}

35
src/main/java/org/bitbiome/commands/CommandListener.java

@ -0,0 +1,35 @@
package org.bitbiome.commands;
import java.util.HashMap;
import java.util.Scanner;
public class CommandListener {
private HashMap<String, CommandAPI> commands;
public CommandListener() {
commands = new HashMap<>();
commands.put("help", new HelpCommand());
commands.put("exit", new QuitCommand());
commands.put("quit", new QuitCommand());
}
public HashMap<String, CommandAPI> returnCommands() {
return commands;
}
public boolean perform(String command, Scanner scanner, boolean isRunning, String message) {
CommandAPI cmd;
if ((cmd = commands.get(command)) != null) {
cmd.performCommand(scanner, isRunning, message);
return true;
}
return false;
}
}

23
src/main/java/org/bitbiome/commands/HelpCommand.java

@ -0,0 +1,23 @@
package org.bitbiome.commands;
import org.bitbiome.Boot;
import java.util.HashMap;
import java.util.Scanner;
public class HelpCommand implements CommandAPI {
@Override
public void performCommand(Scanner scanner, boolean isRunning, String message) {
System.out.println(getHelpMessage());
}
public static String getHelpMessage() {
StringBuilder outputMessage = new StringBuilder();
outputMessage.append("Hier ist eine Liste der Commands:\n").append("- help -> Gibt diese Nachricht aus\n").append("- exit/quit -> Beendet das Spiel\n");
return outputMessage.toString();
}
}

16
src/main/java/org/bitbiome/commands/QuitCommand.java

@ -0,0 +1,16 @@
package org.bitbiome.commands;
import java.util.Scanner;
public class QuitCommand implements CommandAPI {
@Override
public void performCommand(Scanner scanner, boolean isRunning, String message) {
System.out.println(getQuitMessage());
System.exit(0);
}
public static String getQuitMessage() {
return "You quitted!";
}
}

13
src/main/java/org/example/Main.java

@ -1,13 +0,0 @@
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
public static int getLucky() {
return 7;
}
}

3
src/main/resources/crafting.json

@ -0,0 +1,3 @@
{
}

38
src/main/resources/gameconfig.json

@ -0,0 +1,38 @@
{
"shopitems": [
{
"name": "Holz",
"amount": 10,
"gold": 10
},
{
"name": "Heiliges Schwert der Engel",
"amount": 1,
"gold": 1000
},
{
"name": "Stein",
"amount": 5,
"gold": 100
}
],
"locations": [
{
"name": "Wald",
"items": [
"Holz",
"Stein"
],
"mobs": [
{
"name": "Big Foot",
"hp": 50,
"damage": "10-15",
"items": [
"Fell"
]
}
]
}
]
}

26
src/main/resources/items.json

@ -0,0 +1,26 @@
[
{
"name": "Holz",
"damage": "1-3",
"crafting": true,
"durability": 1000
},
{
"name": "Heiliges Schwert der Engel",
"damage": "1000",
"crafting": false,
"durability": 1000
},
{
"name": "Stein",
"damage": "5-10",
"crafting": true,
"durability": 1000
},
{
"name": "Fell",
"damage": "0",
"crafting": true,
"durability": 1000
}
]

17
src/main/resources/playerconfig.json

@ -0,0 +1,17 @@
{
"name": "null",
"gold": 0,
"hp": 10,
"currentLocation": "Wald",
"inventory": [
{
"name": "Holz",
"amount": "5",
"durability": 1000
},{
"name": "Stein",
"amount": "5",
"durability": 1000
}
]
}

15
src/test/java/org/bitbiome/commands/HelpCommandTest.java

@ -0,0 +1,15 @@
package org.bitbiome.commands;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class HelpCommandTest {
@Test
public void testHelpCommand() {
String helpMessage = HelpCommand.getHelpMessage();
assertEquals("Hier ist eine Liste der Commands:\n- help -> Gibt diese Nachricht aus\n- exit/quit -> Beendet das Spiel\n", helpMessage);
}
}

15
src/test/java/org/bitbiome/commands/QuitCommandTest.java

@ -0,0 +1,15 @@
package org.bitbiome.commands;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class QuitCommandTest {
@Test
public void testQuitCommand() {
assertEquals("You quitted!", QuitCommand.getQuitMessage());
}
}

13
src/test/java/org/example/TestMain.java

@ -1,13 +0,0 @@
package org.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestMain {
@Test
public void testLucky() {
assertEquals(7, Main.getLucky());
}
}
Loading…
Cancel
Save