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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with
293 additions and
30 deletions
.gitignore
build-project.sh
pom.xml
src/main/java/org/bitbiome/Boot.java
src/main/java/org/bitbiome/InteractionLoop.java
src/main/java/org/bitbiome/JsonParser.java
src/main/java/org/bitbiome/Main.java
src/main/java/org/bitbiome/commands/CommandAPI.java
src/main/java/org/bitbiome/commands/CommandListener.java
src/main/java/org/bitbiome/commands/HelpCommand.java
src/main/java/org/bitbiome/commands/QuitCommand.java
src/main/java/org/example/Main.java
src/main/resources/crafting.json
src/main/resources/gameconfig.json
src/main/resources/items.json
src/main/resources/playerconfig.json
src/test/java/org/bitbiome/commands/HelpCommandTest.java
src/test/java/org/bitbiome/commands/QuitCommandTest.java
src/test/java/org/example/TestMain.java
@ -24,6 +24,6 @@ hs_err_pid*
replay_pid*
.DS_Store
/.idea/
/. out/
/. target/
/out/
/target/
*.iml
@ -1,3 +1,3 @@
mvn test
java src/main/java/org/example/Main.java
mvn compile exec:java -Dexec.mainClass= "org.bitbiome.Main"
@ -4,7 +4,7 @@
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 > org.exampl e</groupId>
<groupId > org.bitbiom e</groupId>
<artifactId > BitBiome</artifactId>
<version > 1.0-SNAPSHOT</version>
@ -24,6 +24,13 @@
<scope > test</scope>
</dependency>
<!-- Working with JSON Files -->
<dependency >
<groupId > org.json</groupId>
<artifactId > json</artifactId>
<version > 20180130</version>
</dependency>
</dependencies>
<build >
@ -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 ;
}
}
@ -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" ) ;
}
}
}
}
@ -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 ;
}
}
@ -0,0 +1,12 @@
package org.bitbiome ;
public class Main {
public static void main ( String [ ] args ) {
/ / JSONObject playerConfig = JsonParser . getJSONObject ( "playerconfig.json" ) ;
new Boot ( ) ;
}
}
@ -0,0 +1,8 @@
package org.bitbiome.commands ;
import java.util.Scanner ;
public interface CommandAPI {
public void performCommand ( Scanner scanner , boolean isRunning , String message ) ;
}
@ -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 ;
}
}
@ -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 ( ) ;
}
}
@ -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!" ;
}
}
@ -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 ;
}
}
@ -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"
]
}
]
}
]
}
@ -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
}
]
@ -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
}
]
}
@ -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 ) ;
}
}
@ -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 ( ) ) ;
}
}
@ -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 ( ) ) ;
}
}