From ad42f3ff857bbaa07157e013ba37dbceacebad85 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Wed, 18 Jan 2023 22:23:44 +0100 Subject: [PATCH 01/80] Added writeObject method to the JSON Parser to write json objects --- .../java/org/bitbiome/classes/JsonParser.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/classes/JsonParser.java b/src/main/java/org/bitbiome/classes/JsonParser.java index be0e9b8..56a9535 100644 --- a/src/main/java/org/bitbiome/classes/JsonParser.java +++ b/src/main/java/org/bitbiome/classes/JsonParser.java @@ -2,8 +2,14 @@ package org.bitbiome.classes; import org.json.JSONObject; import org.json.JSONTokener; +import org.json.JSONWriter; +import java.io.FileWriter; +import java.io.IOException; import java.io.InputStream; +import java.io.StringWriter; + + public class JsonParser { @@ -30,7 +36,20 @@ public class JsonParser { } JSONTokener tokener = new JSONTokener(is); - return new JSONObject(tokener); } + + public static void writeObject(String fileName, JSONObject object) { + + String resourceName = System.getProperty("user.dir") + "/src/main/resources/" + fileName; + try { + FileWriter fw = new FileWriter(resourceName, false); + fw.write(object.toString(1)); + fw.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + + } } From 6405c41b32652033eb184ff97006ac34cc48f6c3 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Wed, 18 Jan 2023 22:25:46 +0100 Subject: [PATCH 02/80] Added new questions --- src/main/resources/quiz.json | 204 +++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 src/main/resources/quiz.json diff --git a/src/main/resources/quiz.json b/src/main/resources/quiz.json new file mode 100644 index 0000000..cc58a3f --- /dev/null +++ b/src/main/resources/quiz.json @@ -0,0 +1,204 @@ +{ + "Quiz": [ + { + "frage": "Wie lang ist der Äquator der Erde?", + "antworten": [ + "20.000m", + "30km", + "60.000km", + "40.000km" + ], + "korrekteAntwort": "40.000km" + }, + { + "frage": "Was ist der längste Fluss der Welt?", + "antworten": [ + "Amazonas", + "Nil", + "Rhein", + "Niger" + ], + "korrekteAntwort": "Nil" + }, + { + "frage": "Wie viele Tasten hat ein Klavier?", + "antworten": [ + "74", + "86", + "82", + "88" + ], + "korrekteAntwort": "88" + }, + { + "frage": "Von wem wird der Bundespräsident gewählt?", + "antworten": [ + "Vom Europäischen Parlament", + "Vom Bundeskanzler", + "Vom Bundestag", + "Von der Bundesversammlung" + ], + "korrekteAntwort": "von der Bundesversammlung" + }, + { + "frage": "Welches Land produziert jährlich die meisten Filme?", + "antworten": [ + "USA", + "Indien", + "Japan", + "Nigeria" + ], + "korrekteAntwort": "Indien" + }, + { + "frage": "Wie heißt der am schnellsten schwimmende Fisch auf Erden?", + "antworten": [ + "Flugfisch", + "Tigerhai", + "Segelfisch", + "Windfisch" + ], + "korrekteAntwort": "Segelfisch" + }, + { + "frage": "Was ist KEIN Gewächs?", + "antworten": [ + "Geranie", + "Moosfarn", + "Incolornis", + "Strandflieder" + ], + "korrekteAntwort": "Incolornis" + }, + { + "frage": "Was nutzt eine Fledermaus zur Orientierung in der Luft?", + "antworten": [ + "Infrarot", + "Röntgenstrahlen", + "Speichel", + "Ultraschall" + ], + "korrekteAntwort": "Ultraschall" + }, + { + "frage": "Von wem stammt der berühmte Satz: 'Ich denke, also bin ich'?", + "antworten": [ + "John Fitzgerald Kennedy", + "George Walker Bush", + "René Descartes", + "Julius Caesar" + ], + "korrekteAntwort": "René Descartes" + }, + { + "frage": "Welches Lebensmittel enthält das meiste Wasser?", + "antworten": [ + "Gurke", + "Wassermelone", + "Zitrone", + "Paprika" + ], + "korrekteAntwort": "Gurke" + }, + { + "frage": "Welches Lebensmittel gehört im botanischen Sinne zu den Früchten?", + "antworten": [ + "Möhre", + "Kartoffel", + "Tomate", + "Weißkohl" + ], + "korrekteAntwort": "Tomate" + }, + { + "frage": "Haptische Wahrnehmung beruht auf dem...?", + "antworten": [ + "Greifreflex", + "Gleichgewichtssinn", + "Hörsinn", + "Tastsinn" + ], + "korrekteAntwort": "Tastsinn" + }, + { + "frage": "Wie nennt man den letzten Tanz einer Tanzveranstaltung?", + "antworten": [ + "Voraus", + "Garaus", + "Kehraus", + "Durchaus" + ], + "korrekteAntwort": "Kehraus" + }, + { + "frage": "Wie nennt man ein tiefes, enges Tal, durch das ein Gebirgsbach fließt?", + "antworten": [ + "Klamm", + "Feucht", + "Nass", + "Schwamm" + ], + "korrekteAntwort": "Klamm" + }, + { + "frage": "Wer oder was ist Gerbera?", + "antworten": [ + "eine europäische Landschaft", + "eine Pflanze", + "die erste Präsidentin von Südafrika", + "eine Stadt in Lichtenstein" + ], + "korrekteAntwort": "eine Pflanze" + }, + { + "frage": "Nach wem wurde ein Gesellschaftsanzug benannt?", + "antworten": [ + "Richard von Weizsäcker", + "Gustav Heinemann", + "Jürgen Klinsmann", + "Gustav Stresemann" + ], + "korrekteAntwort": "Gustav Stresemann" + }, + { + "frage": "Was ist Speckstein?", + "antworten": [ + "eine Fischart, die sich als Stein tarnt", + "ein Gericht aus dem Mittelalter", + "eine Skulptur im Römischen Reich unter Nero", + "ein besonders weicher Stein" + ], + "korrekteAntwort": "ein besonders weicher Stein" + }, + { + "frage": "In welcher Religion gibt es Gurus?", + "antworten": [ + "im Christentum", + "im Hinduismus", + "im Islam", + "im Judentum" + ], + "korrekteAntwort": "im Hinduismus" + }, + { + "frage": "Was versteht man unter Brunsbüttel?", + "antworten": [ + "eine Industriestadt an der Unterelbe", + "einen Plakatkleber", + "eine 630 Mark-Kraft", + "ein Staatssekretär" + ], + "korrekteAntwort": "eine Industriestadt an der Unterelbe" + }, + { + "frage": "Welcher im 11. Jahrhundert gegründeter Orden rettet und pfelgt auch noch heute Verletzte und Kranke?", + "antworten": [ + "die Dominikaner", + "die Augustiner", + "die Zisterzienser", + "die Johanniter" + ], + "korrekteAntwort": "die Johanniter" + } + ] +} \ No newline at end of file From 67822c0a8e9196d2fd4eef9a7e537420b47ac3dd Mon Sep 17 00:00:00 2001 From: David Hermann Date: Sat, 28 Jan 2023 13:18:56 +0100 Subject: [PATCH 03/80] refactoring: Deleted unused imports --- src/main/java/org/bitbiome/classes/JsonParser.java | 2 -- src/main/java/org/bitbiome/commands/HelpCommand.java | 4 ++-- src/main/java/org/bitbiome/commands/TravelCommand.java | 2 +- src/main/java/org/bitbiome/entities/Player.java | 2 +- src/test/java/org/bitbiome/commands/HelpCommandTest.java | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/bitbiome/classes/JsonParser.java b/src/main/java/org/bitbiome/classes/JsonParser.java index 0eb23d5..45111ef 100644 --- a/src/main/java/org/bitbiome/classes/JsonParser.java +++ b/src/main/java/org/bitbiome/classes/JsonParser.java @@ -2,12 +2,10 @@ package org.bitbiome.classes; import org.json.JSONObject; import org.json.JSONTokener; -import org.json.JSONWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.StringWriter; diff --git a/src/main/java/org/bitbiome/commands/HelpCommand.java b/src/main/java/org/bitbiome/commands/HelpCommand.java index 90702cc..ee4f237 100644 --- a/src/main/java/org/bitbiome/commands/HelpCommand.java +++ b/src/main/java/org/bitbiome/commands/HelpCommand.java @@ -1,10 +1,10 @@ package org.bitbiome.commands; -import org.bitbiome.Boot; + import org.bitbiome.classes.Colors; import org.bitbiome.classes.TravelEngine; -import java.util.HashMap; + import java.util.Scanner; public class HelpCommand implements CommandAPI { diff --git a/src/main/java/org/bitbiome/commands/TravelCommand.java b/src/main/java/org/bitbiome/commands/TravelCommand.java index ca68bf5..da5cbe7 100644 --- a/src/main/java/org/bitbiome/commands/TravelCommand.java +++ b/src/main/java/org/bitbiome/commands/TravelCommand.java @@ -1,7 +1,7 @@ package org.bitbiome.commands; import org.bitbiome.classes.Colors; -import org.bitbiome.classes.CreateLocations; + import org.bitbiome.classes.TravelEngine; import org.bitbiome.entities.Item; import org.bitbiome.entities.Location; diff --git a/src/main/java/org/bitbiome/entities/Player.java b/src/main/java/org/bitbiome/entities/Player.java index 6fe3152..ff8dffb 100644 --- a/src/main/java/org/bitbiome/entities/Player.java +++ b/src/main/java/org/bitbiome/entities/Player.java @@ -1,6 +1,6 @@ package org.bitbiome.entities; -import org.bitbiome.classes.CreateLocations; + import org.bitbiome.classes.JsonParser; import java.util.ArrayList; diff --git a/src/test/java/org/bitbiome/commands/HelpCommandTest.java b/src/test/java/org/bitbiome/commands/HelpCommandTest.java index dab4d90..5caac07 100644 --- a/src/test/java/org/bitbiome/commands/HelpCommandTest.java +++ b/src/test/java/org/bitbiome/commands/HelpCommandTest.java @@ -3,7 +3,7 @@ package org.bitbiome.commands; import org.bitbiome.classes.Colors; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; + public class HelpCommandTest { From c7b8855442a315e34ba71f46e954020f897d0199 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Sat, 28 Jan 2023 13:29:02 +0100 Subject: [PATCH 04/80] refactoring: Language change, added color When a command is unkown, the language is now german and the message is red. --- src/main/java/org/bitbiome/classes/InteractionLoop.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/classes/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java index 2523644..d5c4c98 100644 --- a/src/main/java/org/bitbiome/classes/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -3,6 +3,8 @@ package org.bitbiome.classes; import org.bitbiome.Boot; import org.json.JSONObject; import java.util.Scanner; +import org.bitbiome.classes.Colors; + public class InteractionLoop { @@ -23,7 +25,7 @@ public class InteractionLoop { while (isRunning) { String line = input.nextLine().toLowerCase(); if (!Boot.instance.getCmdListener().perform(line.toLowerCase().split(" ")[0], input, isRunning, line, travelEngine)) { - System.out.println("Unknown Command"); + System.out.println(Colors.ANSI_RED + "Unbekannter Befehl - Siehe " + Colors.ANSI_PURPLE + "help\n" + Colors.ANSI_RESET); } } } From d09e3b1c2dd3497cfede95753332600562ed0964 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Sat, 28 Jan 2023 13:33:21 +0100 Subject: [PATCH 05/80] refactoring: Removed unused imports --- src/main/java/org/bitbiome/classes/InteractionLoop.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/bitbiome/classes/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java index d5c4c98..a0b5017 100644 --- a/src/main/java/org/bitbiome/classes/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -3,7 +3,6 @@ package org.bitbiome.classes; import org.bitbiome.Boot; import org.json.JSONObject; import java.util.Scanner; -import org.bitbiome.classes.Colors; public class InteractionLoop { From ce6247312e943b4e1d1eae3a62594745aef06ca1 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 19:43:06 +0100 Subject: [PATCH 06/80] Update Item.java Changed the privacy of the variables Added empty constructor for unit tests --- src/main/java/org/bitbiome/entities/Item.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/bitbiome/entities/Item.java b/src/main/java/org/bitbiome/entities/Item.java index 2817cd5..0ded3ff 100644 --- a/src/main/java/org/bitbiome/entities/Item.java +++ b/src/main/java/org/bitbiome/entities/Item.java @@ -2,9 +2,9 @@ package org.bitbiome.entities; public class Item { - public String name; - public boolean doesDamage; - public float damage; + private String name; + private boolean doesDamage; + private float damage; public Item(String name, boolean doesDamage, float damage) { @@ -13,6 +13,10 @@ public class Item { this.damage = damage; } + public Item() { + + } + public String getName() { return name; } From 5e723f41b3afbc014dfc3aedfb4d6b0259444cb6 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 19:43:27 +0100 Subject: [PATCH 07/80] Update Location.java Added empty constructor for unit tests Added setName() method for unit tests --- src/main/java/org/bitbiome/entities/Location.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/org/bitbiome/entities/Location.java b/src/main/java/org/bitbiome/entities/Location.java index 98c04d1..664509d 100644 --- a/src/main/java/org/bitbiome/entities/Location.java +++ b/src/main/java/org/bitbiome/entities/Location.java @@ -15,6 +15,10 @@ public class Location { this.itemList = itemList; } + public Location() { + + } + public String getName() { return name; @@ -28,6 +32,10 @@ public class Location { return itemList; } + public void setName(String name) { + this.name = name; + } + } From 7131083fd9a97e583ea3542b49452c3c5233dda3 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 19:43:46 +0100 Subject: [PATCH 08/80] Update Mob.java Added empty constructor for unit tests --- src/main/java/org/bitbiome/entities/Mob.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/bitbiome/entities/Mob.java b/src/main/java/org/bitbiome/entities/Mob.java index af479ce..4bb0d03 100644 --- a/src/main/java/org/bitbiome/entities/Mob.java +++ b/src/main/java/org/bitbiome/entities/Mob.java @@ -15,6 +15,10 @@ public class Mob { this.damage = damage; } + public Mob() { + + } + public String getName() { return name; } From 62ddc0bd3ce33994843d12f2c29915f47e66dabc Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 19:43:56 +0100 Subject: [PATCH 09/80] Update Player.java Added empty constructor for unit tests --- src/main/java/org/bitbiome/entities/Player.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/bitbiome/entities/Player.java b/src/main/java/org/bitbiome/entities/Player.java index ff8dffb..3d46e17 100644 --- a/src/main/java/org/bitbiome/entities/Player.java +++ b/src/main/java/org/bitbiome/entities/Player.java @@ -22,6 +22,10 @@ public class Player { inventory = new ArrayList<>(); } + public Player() { + + } + public String getName() { return name; } From 56fb3c546cb826871798f4688f1a62bfb91d5747 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 19:51:18 +0100 Subject: [PATCH 10/80] unittest: created test for item class This is testing the name of the item --- .../org/bitbiome/entitiesTest/ItemTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/test/java/org/bitbiome/entitiesTest/ItemTest.java diff --git a/src/test/java/org/bitbiome/entitiesTest/ItemTest.java b/src/test/java/org/bitbiome/entitiesTest/ItemTest.java new file mode 100644 index 0000000..56335cc --- /dev/null +++ b/src/test/java/org/bitbiome/entitiesTest/ItemTest.java @@ -0,0 +1,23 @@ +package org.bitbiome.entitiesTest; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.bitbiome.entities.Item; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + + +public class ItemTest { + + private static Item item; + + @BeforeAll + public static void setItem() { + item = new Item(); + item.setName("Unit"); + } + + @Test + public void testGetName() { + assertEquals("Unit", item.getName()); + } +} From 8a380d1ef3e81db1dd79392744fed716a4e0202b Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 20:02:21 +0100 Subject: [PATCH 11/80] unittest: updated item test This additionally tests the damage of the items --- src/test/java/org/bitbiome/entitiesTest/ItemTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/org/bitbiome/entitiesTest/ItemTest.java b/src/test/java/org/bitbiome/entitiesTest/ItemTest.java index 56335cc..ef6093c 100644 --- a/src/test/java/org/bitbiome/entitiesTest/ItemTest.java +++ b/src/test/java/org/bitbiome/entitiesTest/ItemTest.java @@ -14,10 +14,16 @@ public class ItemTest { public static void setItem() { item = new Item(); item.setName("Unit"); + item.setDamage(12.5F); } @Test public void testGetName() { assertEquals("Unit", item.getName()); } + + @Test + public void testGetDamage() { + assertEquals(12.5, item.getDamage()); + } } From 432c37c27826acabcada79ed58222c9d7147af9f Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 20:07:24 +0100 Subject: [PATCH 12/80] unittest: updated item test This additionally tests whether the item does any damage at all. --- src/test/java/org/bitbiome/entitiesTest/ItemTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/org/bitbiome/entitiesTest/ItemTest.java b/src/test/java/org/bitbiome/entitiesTest/ItemTest.java index ef6093c..4214043 100644 --- a/src/test/java/org/bitbiome/entitiesTest/ItemTest.java +++ b/src/test/java/org/bitbiome/entitiesTest/ItemTest.java @@ -1,5 +1,6 @@ package org.bitbiome.entitiesTest; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import org.bitbiome.entities.Item; import org.junit.jupiter.api.BeforeAll; @@ -15,6 +16,7 @@ public class ItemTest { item = new Item(); item.setName("Unit"); item.setDamage(12.5F); + item.changeDoesDamage(true); } @Test @@ -26,4 +28,11 @@ public class ItemTest { public void testGetDamage() { assertEquals(12.5, item.getDamage()); } + + @Test + public void testDoesDamage() { + boolean doesDamage = item.doesDamage(); + assumeTrue(item.getDamage() > 0); + assumeTrue(doesDamage); + } } From 562bd31373f758a280ac57165568509fa2a2910a Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 20:09:20 +0100 Subject: [PATCH 13/80] unittest: created test for mob class This tests whether the mob is friendly --- .../org/bitbiome/entitiesTest/MobTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/test/java/org/bitbiome/entitiesTest/MobTest.java diff --git a/src/test/java/org/bitbiome/entitiesTest/MobTest.java b/src/test/java/org/bitbiome/entitiesTest/MobTest.java new file mode 100644 index 0000000..b11f4ce --- /dev/null +++ b/src/test/java/org/bitbiome/entitiesTest/MobTest.java @@ -0,0 +1,24 @@ +package org.bitbiome.entitiesTest; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.bitbiome.entities.Mob; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + + +public class MobTest { + + private static Mob mob; + + @BeforeAll + public static void setMob() { + mob = new Mob(); + mob.setFriendly(true); + } + + + @Test + public void testFriendly() { + assertEquals(mob.isFriendly(), true); + } + +} From 21f5b7d44c8dcec7a72ec3250f28e704654b54f5 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 20:11:05 +0100 Subject: [PATCH 14/80] unittest: updated mob test This additionally tests how much damage the mob does --- src/test/java/org/bitbiome/entitiesTest/MobTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/org/bitbiome/entitiesTest/MobTest.java b/src/test/java/org/bitbiome/entitiesTest/MobTest.java index b11f4ce..9777dc3 100644 --- a/src/test/java/org/bitbiome/entitiesTest/MobTest.java +++ b/src/test/java/org/bitbiome/entitiesTest/MobTest.java @@ -13,6 +13,7 @@ public class MobTest { public static void setMob() { mob = new Mob(); mob.setFriendly(true); + mob.setDamage(0F); } @@ -20,5 +21,10 @@ public class MobTest { public void testFriendly() { assertEquals(mob.isFriendly(), true); } + + @Test + public void testDamage() { + assertEquals(mob.getDamage(), 0F); + } } From 06c77bf83768ad7503e89ee9e176e6370d2503a8 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 20:11:44 +0100 Subject: [PATCH 15/80] unittest: updated mob test This additionally tests how much life the mob has --- src/test/java/org/bitbiome/entitiesTest/MobTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/org/bitbiome/entitiesTest/MobTest.java b/src/test/java/org/bitbiome/entitiesTest/MobTest.java index 9777dc3..00afaf8 100644 --- a/src/test/java/org/bitbiome/entitiesTest/MobTest.java +++ b/src/test/java/org/bitbiome/entitiesTest/MobTest.java @@ -14,6 +14,7 @@ public class MobTest { mob = new Mob(); mob.setFriendly(true); mob.setDamage(0F); + mob.setHp(561.45F); } @@ -27,4 +28,8 @@ public class MobTest { assertEquals(mob.getDamage(), 0F); } + @Test + public void testHp() { + assertEquals(mob.getHp(), 561.45F); + } } From 8dda2401d12e18fa0846eb44c00fdc7bfa8f6f34 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 20:12:44 +0100 Subject: [PATCH 16/80] unittest: created test for location class This tests the name of the location --- .../bitbiome/entitiesTest/LocationTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/test/java/org/bitbiome/entitiesTest/LocationTest.java diff --git a/src/test/java/org/bitbiome/entitiesTest/LocationTest.java b/src/test/java/org/bitbiome/entitiesTest/LocationTest.java new file mode 100644 index 0000000..1ae0575 --- /dev/null +++ b/src/test/java/org/bitbiome/entitiesTest/LocationTest.java @@ -0,0 +1,24 @@ +package org.bitbiome.entitiesTest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.bitbiome.entities.Location; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class LocationTest { + + private static Location location; + + + @BeforeAll + public static void setLocation() { + location = new Location(); + location.setName("NewUnitWorld"); + } + + @Test + public void testLocationName() { + assertEquals("NewUnitWorld", location.getName()); + } +} From 9acf819d35f8df525d081dcd1d8992c28b79c0c0 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 20:13:52 +0100 Subject: [PATCH 17/80] unittest: created test for player class This tests the name of the player --- .../org/bitbiome/entitiesTest/PlayerTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/test/java/org/bitbiome/entitiesTest/PlayerTest.java diff --git a/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java b/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java new file mode 100644 index 0000000..4f9d885 --- /dev/null +++ b/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java @@ -0,0 +1,26 @@ +package org.bitbiome.entitiesTest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.bitbiome.entities.Player; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class PlayerTest { + + private static Player player; + + @BeforeAll + public static void setPlayer() { + player = new Player(); + + player.setName("UnitPlayer"); + + } + + @Test + public void testPlayerName() { + assertEquals("UnitPlayer", player.getName()); + } + +} From df4b960fa7532add54a912710539b03c2c86ae9d Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 20:14:52 +0100 Subject: [PATCH 18/80] unittest: updated player test This additionally tests how many lives the player has --- src/test/java/org/bitbiome/entitiesTest/PlayerTest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java b/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java index 4f9d885..df03dfd 100644 --- a/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java +++ b/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java @@ -15,7 +15,7 @@ public class PlayerTest { player = new Player(); player.setName("UnitPlayer"); - + player.setHp(100F); } @Test @@ -23,4 +23,10 @@ public class PlayerTest { assertEquals("UnitPlayer", player.getName()); } + + @Test + public void testPlayerHp() { + assertEquals(100F, player.getHp()); + } + } From 1ded07cd00b5145388665ee00836fa152cb14598 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 1 Feb 2023 20:17:10 +0100 Subject: [PATCH 19/80] unittest: updated player test This additionally tests if the player has a location by testing the name of the location related to the player --- .../java/org/bitbiome/entitiesTest/PlayerTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java b/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java index df03dfd..2205e95 100644 --- a/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java +++ b/src/test/java/org/bitbiome/entitiesTest/PlayerTest.java @@ -2,6 +2,7 @@ package org.bitbiome.entitiesTest; import static org.junit.jupiter.api.Assertions.assertEquals; +import org.bitbiome.entities.Location; import org.bitbiome.entities.Player; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -9,12 +10,17 @@ import org.junit.jupiter.api.Test; public class PlayerTest { private static Player player; + private static Location location; @BeforeAll public static void setPlayer() { player = new Player(); + location = new Location(); + + location.setName("NewUnitWorld"); player.setName("UnitPlayer"); + player.setLocation(location); player.setHp(100F); } @@ -29,4 +35,9 @@ public class PlayerTest { assertEquals(100F, player.getHp()); } + @Test + public void testLocationNameFromPlayer() { + assertEquals("NewUnitWorld", player.getLocation().getName()); + } + } From 083e9ae00e2908d86160df5a222a9d47ab26ed77 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Wed, 1 Feb 2023 23:52:35 +0100 Subject: [PATCH 20/80] refactoring: Updated JsonParser Edited method name Now working with relative paths --- .../java/org/bitbiome/classes/JsonParser.java | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/bitbiome/classes/JsonParser.java b/src/main/java/org/bitbiome/classes/JsonParser.java index 56a9535..66dc79c 100644 --- a/src/main/java/org/bitbiome/classes/JsonParser.java +++ b/src/main/java/org/bitbiome/classes/JsonParser.java @@ -2,13 +2,11 @@ package org.bitbiome.classes; import org.json.JSONObject; import org.json.JSONTokener; -import org.json.JSONWriter; +import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.StringWriter; - public class JsonParser { @@ -28,20 +26,30 @@ public class JsonParser { https://github.com/stleary/JSON-java */ - 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); + public static JSONObject readJSONFile(String filePath) { + StringBuilder sb = null; + try { + FileReader reader = new FileReader(filePath); + char[] buffer = new char[1024]; + int length; + sb = new StringBuilder(); + while ((length = reader.read(buffer)) != -1) { + sb.append(buffer, 0, length); + } + + reader.close(); + + } catch (IOException e) { + System.out.println(e); } - JSONTokener tokener = new JSONTokener(is); - return new JSONObject(tokener); + return new JSONObject(sb.toString()); } public static void writeObject(String fileName, JSONObject object) { - String resourceName = System.getProperty("user.dir") + "/src/main/resources/" + fileName; + String resourceName = fileName; + try { FileWriter fw = new FileWriter(resourceName, false); fw.write(object.toString(1)); @@ -49,7 +57,5 @@ public class JsonParser { } catch (IOException e) { throw new RuntimeException(e); } - - } } From ed0debe9fa1b642d4f8ba2088336e6e0442f6297 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Wed, 1 Feb 2023 23:54:17 +0100 Subject: [PATCH 21/80] Created new Command: Quiz --- .../org/bitbiome/commands/CommandListener.java | 1 + .../java/org/bitbiome/commands/QuizCommand.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 src/main/java/org/bitbiome/commands/QuizCommand.java diff --git a/src/main/java/org/bitbiome/commands/CommandListener.java b/src/main/java/org/bitbiome/commands/CommandListener.java index 2e62105..6465891 100644 --- a/src/main/java/org/bitbiome/commands/CommandListener.java +++ b/src/main/java/org/bitbiome/commands/CommandListener.java @@ -14,6 +14,7 @@ public class CommandListener { commands.put("exit", new QuitCommand()); commands.put("quit", new QuitCommand()); + commands.put("quiz", new QuizCommand()); } public HashMap returnCommands() { diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java new file mode 100644 index 0000000..cdcf4e1 --- /dev/null +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -0,0 +1,14 @@ +package org.bitbiome.commands; + + +import java.util.Scanner; + +public class QuizCommand implements CommandAPI { + + @Override + public void performCommand(Scanner scanner, boolean isRunning, String message) { + + } +} + + From cb3c5b402e391321c49b50b7224d5d42eebf772f Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Wed, 1 Feb 2023 23:56:49 +0100 Subject: [PATCH 22/80] refactoring: Updated Json File Added lastPlayed keyword --- src/main/resources/quiz.json | 405 ++++++++++++++++++----------------- 1 file changed, 203 insertions(+), 202 deletions(-) diff --git a/src/main/resources/quiz.json b/src/main/resources/quiz.json index cc58a3f..a36366a 100644 --- a/src/main/resources/quiz.json +++ b/src/main/resources/quiz.json @@ -1,204 +1,205 @@ { - "Quiz": [ - { - "frage": "Wie lang ist der Äquator der Erde?", - "antworten": [ - "20.000m", - "30km", - "60.000km", - "40.000km" - ], - "korrekteAntwort": "40.000km" - }, - { - "frage": "Was ist der längste Fluss der Welt?", - "antworten": [ - "Amazonas", - "Nil", - "Rhein", - "Niger" - ], - "korrekteAntwort": "Nil" - }, - { - "frage": "Wie viele Tasten hat ein Klavier?", - "antworten": [ - "74", - "86", - "82", - "88" - ], - "korrekteAntwort": "88" - }, - { - "frage": "Von wem wird der Bundespräsident gewählt?", - "antworten": [ - "Vom Europäischen Parlament", - "Vom Bundeskanzler", - "Vom Bundestag", - "Von der Bundesversammlung" - ], - "korrekteAntwort": "von der Bundesversammlung" - }, - { - "frage": "Welches Land produziert jährlich die meisten Filme?", - "antworten": [ - "USA", - "Indien", - "Japan", - "Nigeria" - ], - "korrekteAntwort": "Indien" - }, - { - "frage": "Wie heißt der am schnellsten schwimmende Fisch auf Erden?", - "antworten": [ - "Flugfisch", - "Tigerhai", - "Segelfisch", - "Windfisch" - ], - "korrekteAntwort": "Segelfisch" - }, - { - "frage": "Was ist KEIN Gewächs?", - "antworten": [ - "Geranie", - "Moosfarn", - "Incolornis", - "Strandflieder" - ], - "korrekteAntwort": "Incolornis" - }, - { - "frage": "Was nutzt eine Fledermaus zur Orientierung in der Luft?", - "antworten": [ - "Infrarot", - "Röntgenstrahlen", - "Speichel", - "Ultraschall" - ], - "korrekteAntwort": "Ultraschall" - }, - { - "frage": "Von wem stammt der berühmte Satz: 'Ich denke, also bin ich'?", - "antworten": [ - "John Fitzgerald Kennedy", - "George Walker Bush", - "René Descartes", - "Julius Caesar" - ], - "korrekteAntwort": "René Descartes" - }, - { - "frage": "Welches Lebensmittel enthält das meiste Wasser?", - "antworten": [ - "Gurke", - "Wassermelone", - "Zitrone", - "Paprika" - ], - "korrekteAntwort": "Gurke" - }, - { - "frage": "Welches Lebensmittel gehört im botanischen Sinne zu den Früchten?", - "antworten": [ - "Möhre", - "Kartoffel", - "Tomate", - "Weißkohl" - ], - "korrekteAntwort": "Tomate" - }, - { - "frage": "Haptische Wahrnehmung beruht auf dem...?", - "antworten": [ - "Greifreflex", - "Gleichgewichtssinn", - "Hörsinn", - "Tastsinn" - ], - "korrekteAntwort": "Tastsinn" - }, - { - "frage": "Wie nennt man den letzten Tanz einer Tanzveranstaltung?", - "antworten": [ - "Voraus", - "Garaus", - "Kehraus", - "Durchaus" - ], - "korrekteAntwort": "Kehraus" - }, - { - "frage": "Wie nennt man ein tiefes, enges Tal, durch das ein Gebirgsbach fließt?", - "antworten": [ - "Klamm", - "Feucht", - "Nass", - "Schwamm" - ], - "korrekteAntwort": "Klamm" - }, - { - "frage": "Wer oder was ist Gerbera?", - "antworten": [ - "eine europäische Landschaft", - "eine Pflanze", - "die erste Präsidentin von Südafrika", - "eine Stadt in Lichtenstein" - ], - "korrekteAntwort": "eine Pflanze" - }, - { - "frage": "Nach wem wurde ein Gesellschaftsanzug benannt?", - "antworten": [ - "Richard von Weizsäcker", - "Gustav Heinemann", - "Jürgen Klinsmann", - "Gustav Stresemann" - ], - "korrekteAntwort": "Gustav Stresemann" - }, - { - "frage": "Was ist Speckstein?", - "antworten": [ - "eine Fischart, die sich als Stein tarnt", - "ein Gericht aus dem Mittelalter", - "eine Skulptur im Römischen Reich unter Nero", - "ein besonders weicher Stein" - ], - "korrekteAntwort": "ein besonders weicher Stein" - }, - { - "frage": "In welcher Religion gibt es Gurus?", - "antworten": [ - "im Christentum", - "im Hinduismus", - "im Islam", - "im Judentum" - ], - "korrekteAntwort": "im Hinduismus" - }, - { - "frage": "Was versteht man unter Brunsbüttel?", - "antworten": [ - "eine Industriestadt an der Unterelbe", - "einen Plakatkleber", - "eine 630 Mark-Kraft", - "ein Staatssekretär" - ], - "korrekteAntwort": "eine Industriestadt an der Unterelbe" - }, - { - "frage": "Welcher im 11. Jahrhundert gegründeter Orden rettet und pfelgt auch noch heute Verletzte und Kranke?", - "antworten": [ - "die Dominikaner", - "die Augustiner", - "die Zisterzienser", - "die Johanniter" - ], - "korrekteAntwort": "die Johanniter" - } - ] + "Quiz": [ + { + "frage": "Wie lang ist der Äquator der Erde?", + "korrekteAntwort": "40.000km", + "antworten": [ + "20.000m", + "30km", + "60.000km", + "40.000km" + ] + }, + { + "frage": "Was ist der längste Fluss der Welt?", + "korrekteAntwort": "Nil", + "antworten": [ + "Amazonas", + "Nil", + "Rhein", + "Niger" + ] + }, + { + "frage": "Wie viele Tasten hat ein Klavier?", + "korrekteAntwort": "88", + "antworten": [ + "74", + "86", + "82", + "88" + ] + }, + { + "frage": "Von wem wird der Bundespräsident gewählt?", + "korrekteAntwort": "von der Bundesversammlung", + "antworten": [ + "Vom Europäischen Parlament", + "Vom Bundeskanzler", + "Vom Bundestag", + "Von der Bundesversammlung" + ] + }, + { + "frage": "Welches Land produziert jährlich die meisten Filme?", + "korrekteAntwort": "Indien", + "antworten": [ + "USA", + "Indien", + "Japan", + "Nigeria" + ] + }, + { + "frage": "Wie heißt der am schnellsten schwimmende Fisch auf Erden?", + "korrekteAntwort": "Segelfisch", + "antworten": [ + "Flugfisch", + "Tigerhai", + "Segelfisch", + "Windfisch" + ] + }, + { + "frage": "Was ist KEIN Gewächs?", + "korrekteAntwort": "Incolornis", + "antworten": [ + "Geranie", + "Moosfarn", + "Incolornis", + "Strandflieder" + ] + }, + { + "frage": "Was nutzt eine Fledermaus zur Orientierung in der Luft?", + "korrekteAntwort": "Ultraschall", + "antworten": [ + "Infrarot", + "Röntgenstrahlen", + "Speichel", + "Ultraschall" + ] + }, + { + "frage": "Von wem stammt der berühmte Satz: 'Ich denke, also bin ich'?", + "korrekteAntwort": "René Descartes", + "antworten": [ + "John Fitzgerald Kennedy", + "George Walker Bush", + "René Descartes", + "Julius Caesar" + ] + }, + { + "frage": "Welches Lebensmittel enthält das meiste Wasser?", + "korrekteAntwort": "Gurke", + "antworten": [ + "Gurke", + "Wassermelone", + "Zitrone", + "Paprika" + ] + }, + { + "frage": "Welches Lebensmittel gehört im botanischen Sinne zu den Früchten?", + "korrekteAntwort": "Tomate", + "antworten": [ + "Möhre", + "Kartoffel", + "Tomate", + "Weißkohl" + ] + }, + { + "frage": "Haptische Wahrnehmung beruht auf dem...?", + "korrekteAntwort": "Tastsinn", + "antworten": [ + "Greifreflex", + "Gleichgewichtssinn", + "Hörsinn", + "Tastsinn" + ] + }, + { + "frage": "Wie nennt man den letzten Tanz einer Tanzveranstaltung?", + "korrekteAntwort": "Kehraus", + "antworten": [ + "Voraus", + "Garaus", + "Kehraus", + "Durchaus" + ] + }, + { + "frage": "Wie nennt man ein tiefes, enges Tal, durch das ein Gebirgsbach fließt?", + "korrekteAntwort": "Klamm", + "antworten": [ + "Klamm", + "Feucht", + "Nass", + "Schwamm" + ] + }, + { + "frage": "Wer oder was ist Gerbera?", + "korrekteAntwort": "eine Pflanze", + "antworten": [ + "eine europäische Landschaft", + "eine Pflanze", + "die erste Präsidentin von Südafrika", + "eine Stadt in Lichtenstein" + ] + }, + { + "frage": "Nach wem wurde ein Gesellschaftsanzug benannt?", + "korrekteAntwort": "Gustav Stresemann", + "antworten": [ + "Richard von Weizsäcker", + "Gustav Heinemann", + "Jürgen Klinsmann", + "Gustav Stresemann" + ] + }, + { + "frage": "Was ist Speckstein?", + "korrekteAntwort": "ein besonders weicher Stein", + "antworten": [ + "eine Fischart, die sich als Stein tarnt", + "ein Gericht aus dem Mittelalter", + "eine Skulptur im Römischen Reich unter Nero", + "ein besonders weicher Stein" + ] + }, + { + "frage": "In welcher Religion gibt es Gurus?", + "korrekteAntwort": "im Hinduismus", + "antworten": [ + "im Christentum", + "im Hinduismus", + "im Islam", + "im Judentum" + ] + }, + { + "frage": "Was versteht man unter Brunsbüttel?", + "korrekteAntwort": "eine Industriestadt an der Unterelbe", + "antworten": [ + "eine Industriestadt an der Unterelbe", + "einen Plakatkleber", + "eine 630 Mark-Kraft", + "ein Staatssekretär" + ] + }, + { + "frage": "Welcher im 11. Jahrhundert gegründeter Orden rettet und pfelgt auch noch heute Verletzte und Kranke?", + "korrekteAntwort": "die Johanniter", + "antworten": [ + "die Dominikaner", + "die Augustiner", + "die Zisterzienser", + "die Johanniter" + ] + } + ], + "lastPlayed": 0 } \ No newline at end of file From 803ed2f6fc67f6c4d90cefd6e91f6f6ab10667e3 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 00:19:13 +0100 Subject: [PATCH 23/80] Update QuizCommand.java Random question picked from json file and printed out with answeres --- .../org/bitbiome/commands/QuizCommand.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index cdcf4e1..3d10aea 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -1,12 +1,35 @@ package org.bitbiome.commands; +import org.bitbiome.classes.JsonParser; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.Random; import java.util.Scanner; public class QuizCommand implements CommandAPI { @Override public void performCommand(Scanner scanner, boolean isRunning, String message) { + String path = "src\\main\\resources\\quiz.json"; + JSONObject quiz = JsonParser.readJSONFile(path); + + JSONArray fragen = quiz.getJSONArray("Quiz"); + int index = new Random().nextInt(fragen.length()); + JSONObject frage = fragen.getJSONObject(index); + JSONArray antworten = frage.getJSONArray("antworten"); + + System.out.println("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"); + + String frageString = frage.getString("frage"); + + System.out.println(frageString); + + for (int i = 0; i < antworten.length(); i++) { + String antwort = antworten.getString(i); + System.out.println(i + 1 + ". " + antwort); + } } } From 50784178b371185317fd58e3691e978acafcade0 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 00:26:42 +0100 Subject: [PATCH 24/80] Update QuizCommand.java Getting userinput and checking whether answer is correct --- src/main/java/org/bitbiome/commands/QuizCommand.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 3d10aea..241917d 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -30,7 +30,17 @@ public class QuizCommand implements CommandAPI { String antwort = antworten.getString(i); System.out.println(i + 1 + ". " + antwort); } + Scanner quizScanner = new Scanner(System.in); + int eingabe = quizScanner.nextInt(); + String korrekteAntwort = frage.getString("korrekteAntwort"); + + if (antworten.getString(eingabe - 1).equalsIgnoreCase(korrekteAntwort)) { + System.out.println("Richtige Antwort!\n"); + } else { + System.out.println("Leider falsch... Richtig ist: " + korrekteAntwort + "\n"); + } + System.out.println("Das Quiz ist vorbei."); } } From 34c135d54c5477e250abbd58d46ea82cfc9c275e Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 00:49:19 +0100 Subject: [PATCH 25/80] refactoring: Outsourced code Whether the answer is correct or not is outsourced now --- src/main/java/org/bitbiome/commands/QuizCommand.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 241917d..0088a6b 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -34,7 +34,7 @@ public class QuizCommand implements CommandAPI { int eingabe = quizScanner.nextInt(); String korrekteAntwort = frage.getString("korrekteAntwort"); - if (antworten.getString(eingabe - 1).equalsIgnoreCase(korrekteAntwort)) { + if (answerIsCorrect(eingabe, korrekteAntwort, antworten)) { System.out.println("Richtige Antwort!\n"); } else { System.out.println("Leider falsch... Richtig ist: " + korrekteAntwort + "\n"); @@ -42,6 +42,10 @@ public class QuizCommand implements CommandAPI { System.out.println("Das Quiz ist vorbei."); } + + public static boolean answerIsCorrect(int picked, String answer, JSONArray answers) { + return answers.getString(picked - 1).equalsIgnoreCase(answer); + } } From 37ad1c9b1ea6635916e983a2c37f062c57e6fb58 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 00:52:54 +0100 Subject: [PATCH 26/80] refactoring: Created print method This method is printing a messag to the console and returning the same message --- .../org/bitbiome/commands/QuizCommand.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 0088a6b..05eb2e8 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -20,32 +20,38 @@ public class QuizCommand implements CommandAPI { JSONObject frage = fragen.getJSONObject(index); JSONArray antworten = frage.getJSONArray("antworten"); - System.out.println("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"); + print("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"); String frageString = frage.getString("frage"); - System.out.println(frageString); + print(frageString); for (int i = 0; i < antworten.length(); i++) { String antwort = antworten.getString(i); - System.out.println(i + 1 + ". " + antwort); + print(i + 1 + ". " + antwort); } Scanner quizScanner = new Scanner(System.in); int eingabe = quizScanner.nextInt(); String korrekteAntwort = frage.getString("korrekteAntwort"); if (answerIsCorrect(eingabe, korrekteAntwort, antworten)) { - System.out.println("Richtige Antwort!\n"); + print("Richtige Antwort!\n"); } else { - System.out.println("Leider falsch... Richtig ist: " + korrekteAntwort + "\n"); + print("Leider falsch... Richtig ist: " + korrekteAntwort + "\n"); } - System.out.println("Das Quiz ist vorbei."); + print("Das Quiz ist vorbei."); } public static boolean answerIsCorrect(int picked, String answer, JSONArray answers) { return answers.getString(picked - 1).equalsIgnoreCase(answer); } + + public static String print(String message) { + System.out.println(message); + return message; + } + } From 96715d3f4372085d4ef555aff67aba163b17de2a Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 00:57:24 +0100 Subject: [PATCH 27/80] refactoring: Edited code structure Outsourced the generating random number method --- .../org/bitbiome/commands/QuizCommand.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 05eb2e8..d4c37dd 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -9,20 +9,24 @@ import java.util.Random; import java.util.Scanner; public class QuizCommand implements CommandAPI { - + private Scanner quizScanner; @Override public void performCommand(Scanner scanner, boolean isRunning, String message) { + quizScanner = new Scanner(System.in); + String path = "src\\main\\resources\\quiz.json"; JSONObject quiz = JsonParser.readJSONFile(path); JSONArray fragen = quiz.getJSONArray("Quiz"); - int index = new Random().nextInt(fragen.length()); - JSONObject frage = fragen.getJSONObject(index); - JSONArray antworten = frage.getJSONArray("antworten"); + JSONObject frage = fragen.getJSONObject(random(fragen.length())); - print("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"); + JSONArray antworten = frage.getJSONArray("antworten"); String frageString = frage.getString("frage"); + String korrekteAntwort = frage.getString("korrekteAntwort"); + + + print("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"); print(frageString); @@ -30,9 +34,9 @@ public class QuizCommand implements CommandAPI { String antwort = antworten.getString(i); print(i + 1 + ". " + antwort); } - Scanner quizScanner = new Scanner(System.in); + int eingabe = quizScanner.nextInt(); - String korrekteAntwort = frage.getString("korrekteAntwort"); + if (answerIsCorrect(eingabe, korrekteAntwort, antworten)) { print("Richtige Antwort!\n"); @@ -52,6 +56,10 @@ public class QuizCommand implements CommandAPI { return message; } + public static int random(int length) { + return new Random().nextInt(length); + } + } From c720c4836fe3b03c0f6fcd3db6e706c00f709271 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 01:03:43 +0100 Subject: [PATCH 28/80] refactoring: Outsourced Code Generating the question with possible answers now in outsourced method --- .../org/bitbiome/commands/QuizCommand.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index d4c37dd..5748d7b 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -22,22 +22,15 @@ public class QuizCommand implements CommandAPI { JSONArray antworten = frage.getJSONArray("antworten"); - String frageString = frage.getString("frage"); - String korrekteAntwort = frage.getString("korrekteAntwort"); + String korrekteAntwort = frage.getString("korrekteAntwort"); print("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"); - print(frageString); - - for (int i = 0; i < antworten.length(); i++) { - String antwort = antworten.getString(i); - print(i + 1 + ". " + antwort); - } + print(generateQuestion(frage, antworten)); int eingabe = quizScanner.nextInt(); - if (answerIsCorrect(eingabe, korrekteAntwort, antworten)) { print("Richtige Antwort!\n"); } else { @@ -60,6 +53,15 @@ public class QuizCommand implements CommandAPI { return new Random().nextInt(length); } + public static String generateQuestion(JSONObject frage, JSONArray answers) { + StringBuilder sb = new StringBuilder(); + sb.append(frage.getString("frage")).append("\n"); + for (int i = 0; i < answers.length(); i++) { + sb.append(i+1).append(". ").append(answers.getString(i)).append("\n"); + } + return sb.toString(); + } + } From 949d479d9ce1ed42a347ab9efc1f72f78489c4e9 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 01:07:10 +0100 Subject: [PATCH 29/80] Earn gold with quiz You can earn some gold with your quiz now --- src/main/java/org/bitbiome/commands/QuizCommand.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 5748d7b..2c1007d 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -32,7 +32,13 @@ public class QuizCommand implements CommandAPI { int eingabe = quizScanner.nextInt(); if (answerIsCorrect(eingabe, korrekteAntwort, antworten)) { - print("Richtige Antwort!\n"); + String playerpath = "src\\main\\resources\\playerconfig.json"; + JSONObject playerconfig = JsonParser.readJSONFile(playerpath); + int gold = playerconfig.getInt("gold"); + gold = gold + 5; + playerconfig.put("gold", gold); + JsonParser.writeObject(playerpath, playerconfig); + print("Richtig! Du hast 5 Münzen verdient.\nDein Münzstand beträgt: " + gold); } else { print("Leider falsch... Richtig ist: " + korrekteAntwort + "\n"); } From eadd9316e157fd30ef6027b7ab0112a753bebaa0 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 01:09:33 +0100 Subject: [PATCH 30/80] refactoring: Outsourced gold method The method to add gold after your answer was right is now in a seperate method --- .../org/bitbiome/commands/QuizCommand.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 2c1007d..658a898 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -32,13 +32,8 @@ public class QuizCommand implements CommandAPI { int eingabe = quizScanner.nextInt(); if (answerIsCorrect(eingabe, korrekteAntwort, antworten)) { - String playerpath = "src\\main\\resources\\playerconfig.json"; - JSONObject playerconfig = JsonParser.readJSONFile(playerpath); - int gold = playerconfig.getInt("gold"); - gold = gold + 5; - playerconfig.put("gold", gold); - JsonParser.writeObject(playerpath, playerconfig); - print("Richtig! Du hast 5 Münzen verdient.\nDein Münzstand beträgt: " + gold); + int neuerStand = addGold(); + print("Richtig! Du hast 5 Münzen verdient.\nDein Münzstand beträgt: " + neuerStand); } else { print("Leider falsch... Richtig ist: " + korrekteAntwort + "\n"); } @@ -68,6 +63,16 @@ public class QuizCommand implements CommandAPI { return sb.toString(); } + public static int addGold() { + String playerpath = "src\\main\\resources\\playerconfig.json"; + JSONObject playerconfig = JsonParser.readJSONFile(playerpath); + int gold = playerconfig.getInt("gold"); + gold = gold + 5; + playerconfig.put("gold", gold); + JsonParser.writeObject(playerpath, playerconfig); + return gold; + } + } From b4cc87b7ca622d978321c1e4a2b9ae03fdd8370f Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 01:12:43 +0100 Subject: [PATCH 31/80] Added time feature for quiz You have to wait a min time after you can play the quiz again. Currently 5 minutes --- .../java/org/bitbiome/commands/QuizCommand.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 658a898..e261831 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -5,6 +5,7 @@ import org.bitbiome.classes.JsonParser; import org.json.JSONArray; import org.json.JSONObject; +import java.util.Date; import java.util.Random; import java.util.Scanner; @@ -17,6 +18,14 @@ public class QuizCommand implements CommandAPI { String path = "src\\main\\resources\\quiz.json"; JSONObject quiz = JsonParser.readJSONFile(path); + long currentTime = System.currentTimeMillis(); + long minTime = Long.parseLong(quiz.get("lastPlayed").toString()) + (60 * 5 * 1000); + if (minTime >= currentTime) { + long diff = minTime - currentTime; + System.out.println("Du darfst erst in " + diff / 1000 / 60 + " minuten spielen."); + return; + } + JSONArray fragen = quiz.getJSONArray("Quiz"); JSONObject frage = fragen.getJSONObject(random(fragen.length())); @@ -39,6 +48,11 @@ public class QuizCommand implements CommandAPI { } print("Das Quiz ist vorbei."); + + Date d = new Date(); + long lastPlayed = d.getTime(); + quiz.put("lastPlayed", lastPlayed); + JsonParser.writeObject(path, quiz); } public static boolean answerIsCorrect(int picked, String answer, JSONArray answers) { From d08bdb85e0d886dbd75685f9096539833407c757 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 01:19:28 +0100 Subject: [PATCH 32/80] refactoring: Outsourced time feature The time feature for the quiz has a seperate method now which returns the difference of the current time and the time you played the last time. If the difference is greater than 0 you can't play. --- .../java/org/bitbiome/commands/QuizCommand.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index e261831..81e7b3d 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -18,11 +18,9 @@ public class QuizCommand implements CommandAPI { String path = "src\\main\\resources\\quiz.json"; JSONObject quiz = JsonParser.readJSONFile(path); - long currentTime = System.currentTimeMillis(); - long minTime = Long.parseLong(quiz.get("lastPlayed").toString()) + (60 * 5 * 1000); - if (minTime >= currentTime) { - long diff = minTime - currentTime; - System.out.println("Du darfst erst in " + diff / 1000 / 60 + " minuten spielen."); + long diffTime = canPlayAgain(quiz.getLong("lastPlayed")); + if (diffTime > 0) { + print("Du darfst erst in " + diffTime / 1000 / 60 + " minuten spielen."); return; } @@ -87,6 +85,12 @@ public class QuizCommand implements CommandAPI { return gold; } + public static long canPlayAgain(long lastPlayedTime) { + long currentTime = System.currentTimeMillis(); + long minTime = lastPlayedTime + (60 * 5 * 1000); + return minTime - currentTime; + } + } From 1749b5f66351b5f14f30888347bcf06380487a47 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 10:07:56 +0100 Subject: [PATCH 33/80] refactoring: Outsourced System.out.println --- .../java/org/bitbiome/classes/InteractionLoop.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/bitbiome/classes/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java index a0b5017..bbf513d 100644 --- a/src/main/java/org/bitbiome/classes/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -12,7 +12,7 @@ public class InteractionLoop { public void run(TravelEngine travelEngine) { boolean isRunning = true; if (travelEngine.getPlayer().getName().equals("null")) { - System.out.println(Colors.ANSI_BLUE + "Oh, ein Fremder!\nBist du bereit für dein womöglich größtes Abenteuer?\nDann sag mir doch zunächst wie du heißt: " + Colors.ANSI_RESET); + print(Colors.ANSI_BLUE + "Oh, ein Fremder!\nBist du bereit für dein womöglich größtes Abenteuer?\nDann sag mir doch zunächst wie du heißt: " + Colors.ANSI_RESET); String name = input.nextLine(); JsonParser jp = new JsonParser(); JSONObject playerconf = jp.getJSONObject("playerconfig.json"); @@ -20,15 +20,20 @@ public class InteractionLoop { travelEngine.getPlayer().setName(name); jp.writeObject("playerconfig.json", playerconf); } - System.out.println(Colors.ANSI_BG_CYAN + Colors.ANSI_BLACK + "Willkommen zu BitBiome " + travelEngine.getPlayer().getName() + "!" + Colors.ANSI_RESET + "\n\n"); + print(Colors.ANSI_BG_CYAN + Colors.ANSI_BLACK + "Willkommen zu BitBiome " + travelEngine.getPlayer().getName() + "!" + Colors.ANSI_RESET + "\n\n"); while (isRunning) { String line = input.nextLine().toLowerCase(); if (!Boot.instance.getCmdListener().perform(line.toLowerCase().split(" ")[0], input, isRunning, line, travelEngine)) { - System.out.println(Colors.ANSI_RED + "Unbekannter Befehl - Siehe " + Colors.ANSI_PURPLE + "help\n" + Colors.ANSI_RESET); + print(Colors.ANSI_RED + "Unbekannter Befehl - Siehe " + Colors.ANSI_PURPLE + "help\n" + Colors.ANSI_RESET); } } } + public boolean print(String message) { + System.out.println(message); + return true; + } + } From 4e990e2d04321291f620cfeb23b416874abb1419 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 10:17:22 +0100 Subject: [PATCH 34/80] refactoring: Outsourced code in InteractionLoop Whether the player is new or not will be checked in new outsourced function --- src/main/java/org/bitbiome/classes/InteractionLoop.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/classes/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java index bbf513d..5cd9a59 100644 --- a/src/main/java/org/bitbiome/classes/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -11,7 +11,7 @@ public class InteractionLoop { public void run(TravelEngine travelEngine) { boolean isRunning = true; - if (travelEngine.getPlayer().getName().equals("null")) { + if (playerIsNew(travelEngine.getPlayer().getName())) { print(Colors.ANSI_BLUE + "Oh, ein Fremder!\nBist du bereit für dein womöglich größtes Abenteuer?\nDann sag mir doch zunächst wie du heißt: " + Colors.ANSI_RESET); String name = input.nextLine(); JsonParser jp = new JsonParser(); @@ -34,6 +34,10 @@ public class InteractionLoop { return true; } + public boolean playerIsNew(String name) { + return name.equalsIgnoreCase("null"); + } + } From d6ece832e7768909c1e77e1dc34fc800fcd3f5bb Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 10:39:23 +0100 Subject: [PATCH 35/80] refactoring: Outsourced code When the player is new a outsourced method is called --- .../org/bitbiome/classes/InteractionLoop.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/bitbiome/classes/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java index 5cd9a59..f8a6c84 100644 --- a/src/main/java/org/bitbiome/classes/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -4,7 +4,6 @@ import org.bitbiome.Boot; import org.json.JSONObject; import java.util.Scanner; - public class InteractionLoop { Scanner input = new Scanner(System.in); @@ -12,19 +11,16 @@ public class InteractionLoop { public void run(TravelEngine travelEngine) { boolean isRunning = true; if (playerIsNew(travelEngine.getPlayer().getName())) { - print(Colors.ANSI_BLUE + "Oh, ein Fremder!\nBist du bereit für dein womöglich größtes Abenteuer?\nDann sag mir doch zunächst wie du heißt: " + Colors.ANSI_RESET); - String name = input.nextLine(); - JsonParser jp = new JsonParser(); - JSONObject playerconf = jp.getJSONObject("playerconfig.json"); - playerconf.put("name", name); - travelEngine.getPlayer().setName(name); - jp.writeObject("playerconfig.json", playerconf); + newPlayerWelcome(travelEngine); } - print(Colors.ANSI_BG_CYAN + Colors.ANSI_BLACK + "Willkommen zu BitBiome " + travelEngine.getPlayer().getName() + "!" + Colors.ANSI_RESET + "\n\n"); + print(Colors.ANSI_BG_CYAN + Colors.ANSI_BLACK + "Willkommen zu BitBiome " + travelEngine.getPlayer().getName() + + "!" + Colors.ANSI_RESET + "\n\n"); while (isRunning) { String line = input.nextLine().toLowerCase(); - if (!Boot.instance.getCmdListener().perform(line.toLowerCase().split(" ")[0], input, isRunning, line, travelEngine)) { - print(Colors.ANSI_RED + "Unbekannter Befehl - Siehe " + Colors.ANSI_PURPLE + "help\n" + Colors.ANSI_RESET); + if (!Boot.instance.getCmdListener().perform(line.toLowerCase().split(" ")[0], input, isRunning, line, + travelEngine)) { + print(Colors.ANSI_RED + "Unbekannter Befehl - Siehe " + Colors.ANSI_PURPLE + "help\n" + + Colors.ANSI_RESET); } } } @@ -38,6 +34,16 @@ public class InteractionLoop { return name.equalsIgnoreCase("null"); } - + public void newPlayerWelcome(TravelEngine travelEngine) { + print(Colors.ANSI_BLUE + + "Oh, ein Fremder!\nBist du bereit für dein womöglich größtes Abenteuer?\nDann sag mir doch zunächst wie du heißt: " + + Colors.ANSI_RESET); + String name = input.nextLine(); + JsonParser jp = new JsonParser(); + JSONObject playerconf = jp.getJSONObject("playerconfig.json"); + playerconf.put("name", name); + travelEngine.getPlayer().setName(name); + jp.writeObject("playerconfig.json", playerconf); + } } From fda96a5acb65c3b651dee5235a7f70e049f82bd5 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 10:41:41 +0100 Subject: [PATCH 36/80] refactoring: Deleted CreateLocations --- .../org/bitbiome/classes/CreateLocations.java | 24 ------------------- 1 file changed, 24 deletions(-) delete mode 100644 src/main/java/org/bitbiome/classes/CreateLocations.java diff --git a/src/main/java/org/bitbiome/classes/CreateLocations.java b/src/main/java/org/bitbiome/classes/CreateLocations.java deleted file mode 100644 index ed237a1..0000000 --- a/src/main/java/org/bitbiome/classes/CreateLocations.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.bitbiome.classes; - -import org.bitbiome.entities.Item; -import org.bitbiome.entities.Location; -import org.bitbiome.entities.Mob; - -import java.util.ArrayList; - -public class CreateLocations { - - public static Location createForest() { - ArrayList items = new ArrayList<>(); - ArrayList mobs = new ArrayList<>(); - String name = "Wald"; - return new Location(name, mobs, items); - } - - public static Location createBeach() { - ArrayList items = new ArrayList<>(); - ArrayList mobs = new ArrayList<>(); - String name = "Strand"; - return new Location(name, mobs, items); - } -} From 9ebe7ea258f84b34dd3cfdc207d07759796a770b Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 10:48:37 +0100 Subject: [PATCH 37/80] refactoring: Outsourced System.out.println --- .../java/org/bitbiome/commands/TravelCommand.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/TravelCommand.java b/src/main/java/org/bitbiome/commands/TravelCommand.java index da5cbe7..810333e 100644 --- a/src/main/java/org/bitbiome/commands/TravelCommand.java +++ b/src/main/java/org/bitbiome/commands/TravelCommand.java @@ -16,18 +16,23 @@ public class TravelCommand implements CommandAPI { @Override public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) { - System.out.println(Colors.ANSI_BLUE + "Du hast dein Travel-Pad gezückt. Wohin möchtest du reisen?" + Colors.ANSI_RESET); + print(Colors.ANSI_BLUE + "Du hast dein Travel-Pad gezückt. Wohin möchtest du reisen?" + Colors.ANSI_RESET); JSONArray locations = travelEngine.getLocationList(); for (int i = 0; i < locations.length(); i++) { - System.out.println("- " + locations.getJSONObject(i).getString("name")); + print("- " + locations.getJSONObject(i).getString("name")); } String locationName = scanner.nextLine(); if (travelEngine.locationExists(locationName)) { travelEngine.travelTo(new Location(locationName, new ArrayList(), new ArrayList())); - System.out.println(Colors.ANSI_BLUE + "Du bist nun hierhin gereist: " + locationName + "\n" + Colors.ANSI_RESET); + print(Colors.ANSI_BLUE + "Du bist nun hierhin gereist: " + locationName + "\n" + Colors.ANSI_RESET); } else { - System.out.println(Colors.ANSI_BLUE + "Du hast dein Travel-Pad weggesteckt." + Colors.ANSI_RESET); + print(Colors.ANSI_BLUE + "Du hast dein Travel-Pad weggesteckt." + Colors.ANSI_RESET); } } + + public String print(String message) { + System.out.println(message); + return message; + } } From 460fc7157038129ad248030f981f9e5d677d43f7 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 11:17:56 +0100 Subject: [PATCH 38/80] refactoring: Added comment to Color --- .../java/org/bitbiome/classes/Colors.java | 106 +++++++++--------- 1 file changed, 56 insertions(+), 50 deletions(-) diff --git a/src/main/java/org/bitbiome/classes/Colors.java b/src/main/java/org/bitbiome/classes/Colors.java index d08f59e..99e45e1 100644 --- a/src/main/java/org/bitbiome/classes/Colors.java +++ b/src/main/java/org/bitbiome/classes/Colors.java @@ -2,55 +2,61 @@ package org.bitbiome.classes; public class Colors { - public static final String ANSI_RESET = "\u001B[0m"; - - public static final String ANSI_BLACK = "\u001B[30m"; - public static final String ANSI_RED = "\u001B[31m"; - public static final String ANSI_GREEN = "\u001B[32m"; - public static final String ANSI_YELLOW = "\u001B[33m"; - public static final String ANSI_BLUE = "\u001B[34m"; - public static final String ANSI_PURPLE = "\u001B[35m"; - public static final String ANSI_CYAN = "\u001B[36m"; - public static final String ANSI_WHITE = "\u001B[37m"; - - public static final String ANSI_BRIGHT_BLACK = "\u001B[90m"; - public static final String ANSI_BRIGHT_RED = "\u001B[91m"; - public static final String ANSI_BRIGHT_GREEN = "\u001B[92m"; - public static final String ANSI_BRIGHT_YELLOW = "\u001B[93m"; - public static final String ANSI_BRIGHT_BLUE = "\u001B[94m"; - public static final String ANSI_BRIGHT_PURPLE = "\u001B[95m"; - public static final String ANSI_BRIGHT_CYAN = "\u001B[96m"; - public static final String ANSI_BRIGHT_WHITE = "\u001B[97m"; - - public static final String[] FOREGROUNDS = { - ANSI_BLACK, ANSI_RED, ANSI_GREEN, ANSI_YELLOW, - ANSI_BLUE, ANSI_PURPLE, ANSI_CYAN, ANSI_WHITE, - ANSI_BRIGHT_BLACK, ANSI_BRIGHT_RED, ANSI_BRIGHT_GREEN, ANSI_BRIGHT_YELLOW, - ANSI_BRIGHT_BLUE, ANSI_BRIGHT_PURPLE, ANSI_BRIGHT_CYAN, ANSI_BRIGHT_WHITE - }; - - public static final String ANSI_BG_BLACK = "\u001B[40m"; - public static final String ANSI_BG_RED = "\u001B[41m"; - public static final String ANSI_BG_GREEN = "\u001B[42m"; - public static final String ANSI_BG_YELLOW = "\u001B[43m"; - public static final String ANSI_BG_BLUE = "\u001B[44m"; - public static final String ANSI_BG_PURPLE = "\u001B[45m"; - public static final String ANSI_BG_CYAN = "\u001B[46m"; - public static final String ANSI_BG_WHITE = "\u001B[47m"; - - public static final String ANSI_BRIGHT_BG_BLACK = "\u001B[100m"; - public static final String ANSI_BRIGHT_BG_RED = "\u001B[101m"; - public static final String ANSI_BRIGHT_BG_GREEN = "\u001B[102m"; - public static final String ANSI_BRIGHT_BG_YELLOW = "\u001B[103m"; - public static final String ANSI_BRIGHT_BG_BLUE = "\u001B[104m"; - public static final String ANSI_BRIGHT_BG_PURPLE = "\u001B[105m"; - public static final String ANSI_BRIGHT_BG_CYAN = "\u001B[106m"; - public static final String ANSI_BRIGHT_BG_WHITE = "\u001B[107m"; - - public static final String[] BACKGROUNDS = { - ANSI_BG_BLACK, ANSI_BG_RED, ANSI_BG_GREEN, ANSI_BG_YELLOW, - ANSI_BG_BLUE, ANSI_BG_PURPLE, ANSI_BG_CYAN, ANSI_BG_WHITE, - ANSI_BRIGHT_BG_BLACK, ANSI_BRIGHT_BG_RED, ANSI_BRIGHT_BG_GREEN, ANSI_BRIGHT_BG_YELLOW, - ANSI_BRIGHT_BG_BLUE, ANSI_BRIGHT_BG_PURPLE, ANSI_BRIGHT_BG_CYAN, ANSI_BRIGHT_BG_WHITE }; + /* + * This class has only public static mehtods + * Just add a String to your String and finalize it with the ANSI_RESET String + * The Color Codes with BG in the variable name are for the background colors + */ + + public static final String ANSI_RESET = "\u001B[0m"; + + public static final String ANSI_BLACK = "\u001B[30m"; + public static final String ANSI_RED = "\u001B[31m"; + public static final String ANSI_GREEN = "\u001B[32m"; + public static final String ANSI_YELLOW = "\u001B[33m"; + public static final String ANSI_BLUE = "\u001B[34m"; + public static final String ANSI_PURPLE = "\u001B[35m"; + public static final String ANSI_CYAN = "\u001B[36m"; + public static final String ANSI_WHITE = "\u001B[37m"; + + public static final String ANSI_BRIGHT_BLACK = "\u001B[90m"; + public static final String ANSI_BRIGHT_RED = "\u001B[91m"; + public static final String ANSI_BRIGHT_GREEN = "\u001B[92m"; + public static final String ANSI_BRIGHT_YELLOW = "\u001B[93m"; + public static final String ANSI_BRIGHT_BLUE = "\u001B[94m"; + public static final String ANSI_BRIGHT_PURPLE = "\u001B[95m"; + public static final String ANSI_BRIGHT_CYAN = "\u001B[96m"; + public static final String ANSI_BRIGHT_WHITE = "\u001B[97m"; + + public static final String[] FOREGROUNDS = { + ANSI_BLACK, ANSI_RED, ANSI_GREEN, ANSI_YELLOW, + ANSI_BLUE, ANSI_PURPLE, ANSI_CYAN, ANSI_WHITE, + ANSI_BRIGHT_BLACK, ANSI_BRIGHT_RED, ANSI_BRIGHT_GREEN, ANSI_BRIGHT_YELLOW, + ANSI_BRIGHT_BLUE, ANSI_BRIGHT_PURPLE, ANSI_BRIGHT_CYAN, ANSI_BRIGHT_WHITE + }; + + public static final String ANSI_BG_BLACK = "\u001B[40m"; + public static final String ANSI_BG_RED = "\u001B[41m"; + public static final String ANSI_BG_GREEN = "\u001B[42m"; + public static final String ANSI_BG_YELLOW = "\u001B[43m"; + public static final String ANSI_BG_BLUE = "\u001B[44m"; + public static final String ANSI_BG_PURPLE = "\u001B[45m"; + public static final String ANSI_BG_CYAN = "\u001B[46m"; + public static final String ANSI_BG_WHITE = "\u001B[47m"; + + public static final String ANSI_BRIGHT_BG_BLACK = "\u001B[100m"; + public static final String ANSI_BRIGHT_BG_RED = "\u001B[101m"; + public static final String ANSI_BRIGHT_BG_GREEN = "\u001B[102m"; + public static final String ANSI_BRIGHT_BG_YELLOW = "\u001B[103m"; + public static final String ANSI_BRIGHT_BG_BLUE = "\u001B[104m"; + public static final String ANSI_BRIGHT_BG_PURPLE = "\u001B[105m"; + public static final String ANSI_BRIGHT_BG_CYAN = "\u001B[106m"; + public static final String ANSI_BRIGHT_BG_WHITE = "\u001B[107m"; + + public static final String[] BACKGROUNDS = { + ANSI_BG_BLACK, ANSI_BG_RED, ANSI_BG_GREEN, ANSI_BG_YELLOW, + ANSI_BG_BLUE, ANSI_BG_PURPLE, ANSI_BG_CYAN, ANSI_BG_WHITE, + ANSI_BRIGHT_BG_BLACK, ANSI_BRIGHT_BG_RED, ANSI_BRIGHT_BG_GREEN, ANSI_BRIGHT_BG_YELLOW, + ANSI_BRIGHT_BG_BLUE, ANSI_BRIGHT_BG_PURPLE, ANSI_BRIGHT_BG_CYAN, ANSI_BRIGHT_BG_WHITE }; } From ad5f87a4b0332170961a112eb3493006573e1a1c Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 11:21:36 +0100 Subject: [PATCH 39/80] refactoring: Added comments to CommandInterface --- src/main/java/org/bitbiome/commands/CommandAPI.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/bitbiome/commands/CommandAPI.java b/src/main/java/org/bitbiome/commands/CommandAPI.java index a5c48be..9e5ade8 100644 --- a/src/main/java/org/bitbiome/commands/CommandAPI.java +++ b/src/main/java/org/bitbiome/commands/CommandAPI.java @@ -5,6 +5,10 @@ import org.bitbiome.classes.TravelEngine; import java.util.Scanner; public interface CommandAPI { + + // This is the command interface. Every command implements it's run method from here + // This is the API between the commands and the interaction loop/game + public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine); } From b26a43dfcbb824f587895f2ea341542ae8e3eaa7 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 11:24:45 +0100 Subject: [PATCH 40/80] refactoring: Added comments to CommandListener --- src/main/java/org/bitbiome/commands/CommandListener.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/commands/CommandListener.java b/src/main/java/org/bitbiome/commands/CommandListener.java index d59813c..d30ef1f 100644 --- a/src/main/java/org/bitbiome/commands/CommandListener.java +++ b/src/main/java/org/bitbiome/commands/CommandListener.java @@ -6,7 +6,9 @@ import java.util.HashMap; import java.util.Scanner; public class CommandListener { - + // This class is the API between the command interface and the user input + // This class is used to handle all the commands + // The commands are stored in a hashmap private HashMap commands; public CommandListener() { From 3e4259170cebe5dfb1fa2d16073a96d7e7f67c51 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 11:28:09 +0100 Subject: [PATCH 41/80] refactoring: Added comments to QuitCommand --- src/main/java/org/bitbiome/commands/QuitCommand.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/bitbiome/commands/QuitCommand.java b/src/main/java/org/bitbiome/commands/QuitCommand.java index 25dbb00..258ab1a 100644 --- a/src/main/java/org/bitbiome/commands/QuitCommand.java +++ b/src/main/java/org/bitbiome/commands/QuitCommand.java @@ -6,6 +6,10 @@ import java.util.Scanner; public class QuitCommand implements CommandAPI { + // This command is used to end the game via command + // When the player quits the game, the game is stopped through System.exit(0) + // That means it is exited with no error status + @Override public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) { System.out.println(getQuitMessage()); From 23c162d5246115b5603b771065252f9870cfcaf5 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 11:36:56 +0100 Subject: [PATCH 42/80] unittest: Created test for important color codes --- src/test/java/org/bitbiome/classes/ColorsTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/test/java/org/bitbiome/classes/ColorsTest.java diff --git a/src/test/java/org/bitbiome/classes/ColorsTest.java b/src/test/java/org/bitbiome/classes/ColorsTest.java new file mode 100644 index 0000000..21069bb --- /dev/null +++ b/src/test/java/org/bitbiome/classes/ColorsTest.java @@ -0,0 +1,13 @@ +package org.bitbiome.classes; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class ColorsTest { + + @Test + public void testResetCode() { + assertEquals("\u001B[0m", Colors.ANSI_RESET); + } +} From 76aeb8fc0fc9529176aa45444fa5d85400ec38bd Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 11:38:13 +0100 Subject: [PATCH 43/80] unittest: Created test for blue color --- src/test/java/org/bitbiome/classes/ColorsTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/org/bitbiome/classes/ColorsTest.java b/src/test/java/org/bitbiome/classes/ColorsTest.java index 21069bb..d6cc36b 100644 --- a/src/test/java/org/bitbiome/classes/ColorsTest.java +++ b/src/test/java/org/bitbiome/classes/ColorsTest.java @@ -10,4 +10,9 @@ public class ColorsTest { public void testResetCode() { assertEquals("\u001B[0m", Colors.ANSI_RESET); } + + @Test + public void testBlueCode() { + assertEquals("\u001B[34m", Colors.ANSI_BLUE); + } } From 7ec03825398edf2eedf9947932b8578b380422b4 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Mon, 6 Feb 2023 11:42:03 +0100 Subject: [PATCH 44/80] unittest: Created test for cyan code --- src/test/java/org/bitbiome/classes/ColorsTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/org/bitbiome/classes/ColorsTest.java b/src/test/java/org/bitbiome/classes/ColorsTest.java index d6cc36b..541ebdf 100644 --- a/src/test/java/org/bitbiome/classes/ColorsTest.java +++ b/src/test/java/org/bitbiome/classes/ColorsTest.java @@ -15,4 +15,9 @@ public class ColorsTest { public void testBlueCode() { assertEquals("\u001B[34m", Colors.ANSI_BLUE); } + + @Test + public void testCyanCode() { + assertEquals("\u001B[36m", Colors.ANSI_CYAN); + } } From b4ff7a5d2a8732cd62879e35268b4a84f6fa92f4 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:18:51 +0100 Subject: [PATCH 45/80] refactoring: Outsourced code Outsourced starter quiz message --- src/main/java/org/bitbiome/commands/QuizCommand.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 81e7b3d..d1ab06d 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -32,7 +32,7 @@ public class QuizCommand implements CommandAPI { String korrekteAntwort = frage.getString("korrekteAntwort"); - print("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"); + print(anfangsnachricht()); print(generateQuestion(frage, antworten)); @@ -91,6 +91,10 @@ public class QuizCommand implements CommandAPI { return minTime - currentTime; } + public static String anfangsnachricht(){ + return "Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"; + + } } From 7ccb7ee30400f2fc6ddf40ce6114bb69a7f46709 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:19:19 +0100 Subject: [PATCH 46/80] refactoring: Outsourced code Outsourced ending quiz message --- src/main/java/org/bitbiome/commands/QuizCommand.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index d1ab06d..526a0da 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -45,7 +45,7 @@ public class QuizCommand implements CommandAPI { print("Leider falsch... Richtig ist: " + korrekteAntwort + "\n"); } - print("Das Quiz ist vorbei."); + print(endmessage()); Date d = new Date(); long lastPlayed = d.getTime(); @@ -95,6 +95,10 @@ public class QuizCommand implements CommandAPI { return "Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"; } + + public static String endmessage(){ + return "Das Quiz ist vorbei!"; + } } From f92d8f58470252a8a55818e5d2dac89045a921c2 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:20:33 +0100 Subject: [PATCH 47/80] Updated quiz message How long user has to wait to play toward --- src/main/java/org/bitbiome/commands/QuizCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 526a0da..307ec3d 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -20,7 +20,7 @@ public class QuizCommand implements CommandAPI { long diffTime = canPlayAgain(quiz.getLong("lastPlayed")); if (diffTime > 0) { - print("Du darfst erst in " + diffTime / 1000 / 60 + " minuten spielen."); + print("Du darfst erst in " + diffTime / 1000 / 60 + " Minuten spielen."); return; } From 05693206bd677d5923417b1e2c13d6895a40ddbf Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:26:19 +0100 Subject: [PATCH 48/80] refactoring: Edited method name in QuizCommand anfangsnachricht -> starterMessage --- src/main/java/org/bitbiome/commands/QuizCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index 307ec3d..a4210ac 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -32,7 +32,7 @@ public class QuizCommand implements CommandAPI { String korrekteAntwort = frage.getString("korrekteAntwort"); - print(anfangsnachricht()); + print(starterMessage()); print(generateQuestion(frage, antworten)); @@ -91,7 +91,7 @@ public class QuizCommand implements CommandAPI { return minTime - currentTime; } - public static String anfangsnachricht(){ + public static String starterMessage(){ return "Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"; } From 915d3829dc0bf219187c42fe6588616a862f871e Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:26:43 +0100 Subject: [PATCH 49/80] refactoring: Edited method name in QuizCommand endmessage -> endMessage *Camel Case Convention --- src/main/java/org/bitbiome/commands/QuizCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index a4210ac..db0316e 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -45,7 +45,7 @@ public class QuizCommand implements CommandAPI { print("Leider falsch... Richtig ist: " + korrekteAntwort + "\n"); } - print(endmessage()); + print(endMessage()); Date d = new Date(); long lastPlayed = d.getTime(); @@ -96,7 +96,7 @@ public class QuizCommand implements CommandAPI { } - public static String endmessage(){ + public static String endMessage(){ return "Das Quiz ist vorbei!"; } } From ada95c7e03706602cc70455e3300633b29dfa7bf Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:34:20 +0100 Subject: [PATCH 50/80] unittest: Created test for QuizCommand This test tests the start/welcome message to the quiz --- .../java/org/bitbiome/commands/QuizCommandTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/test/java/org/bitbiome/commands/QuizCommandTest.java diff --git a/src/test/java/org/bitbiome/commands/QuizCommandTest.java b/src/test/java/org/bitbiome/commands/QuizCommandTest.java new file mode 100644 index 0000000..14b73a7 --- /dev/null +++ b/src/test/java/org/bitbiome/commands/QuizCommandTest.java @@ -0,0 +1,13 @@ +package org.bitbiome.commands; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class QuizCommandTest { + + @Test + public void testStartMessage() { + assertEquals("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n", QuizCommand.starterMessage()); + } +} From c8c68946b0d77614fea7e4e48cb2ded620e551a3 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:35:52 +0100 Subject: [PATCH 51/80] unittest: Edited test for QuizCommand This test tests the ending message for the quiz --- src/test/java/org/bitbiome/commands/QuizCommandTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/org/bitbiome/commands/QuizCommandTest.java b/src/test/java/org/bitbiome/commands/QuizCommandTest.java index 14b73a7..1668be2 100644 --- a/src/test/java/org/bitbiome/commands/QuizCommandTest.java +++ b/src/test/java/org/bitbiome/commands/QuizCommandTest.java @@ -10,4 +10,9 @@ public class QuizCommandTest { public void testStartMessage() { assertEquals("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n", QuizCommand.starterMessage()); } + + @Test + public void testEndMessage() { + assertEquals("Das Quiz ist vorbei!", QuizCommand.endMessage()); + } } From e6b712ca09847b466cf91eafa2fad129ec63de54 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:39:14 +0100 Subject: [PATCH 52/80] unittest: Edited test for QuizCommand This test tests whether the canPlayAgain function works or not --- src/test/java/org/bitbiome/commands/QuizCommandTest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/bitbiome/commands/QuizCommandTest.java b/src/test/java/org/bitbiome/commands/QuizCommandTest.java index 1668be2..93afc23 100644 --- a/src/test/java/org/bitbiome/commands/QuizCommandTest.java +++ b/src/test/java/org/bitbiome/commands/QuizCommandTest.java @@ -2,7 +2,7 @@ package org.bitbiome.commands; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; public class QuizCommandTest { @@ -15,4 +15,10 @@ public class QuizCommandTest { public void testEndMessage() { assertEquals("Das Quiz ist vorbei!", QuizCommand.endMessage()); } + + @Test + public void testLastTimePlayed() { + long lastTimePlayed = System.currentTimeMillis(); + assertTrue(QuizCommand.canPlayAgain(lastTimePlayed) < lastTimePlayed); + } } From 56794efd3be7db992e2059d5ee61e373ddf2ed29 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:46:42 +0100 Subject: [PATCH 53/80] unittest: Edited test for QuizCommand This test tests the random function which gives a random number between 0 and a length back --- src/test/java/org/bitbiome/commands/QuizCommandTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/org/bitbiome/commands/QuizCommandTest.java b/src/test/java/org/bitbiome/commands/QuizCommandTest.java index 93afc23..520ae87 100644 --- a/src/test/java/org/bitbiome/commands/QuizCommandTest.java +++ b/src/test/java/org/bitbiome/commands/QuizCommandTest.java @@ -2,6 +2,8 @@ package org.bitbiome.commands; import org.junit.jupiter.api.Test; +import java.util.Random; + import static org.junit.jupiter.api.Assertions.*; public class QuizCommandTest { @@ -21,4 +23,10 @@ public class QuizCommandTest { long lastTimePlayed = System.currentTimeMillis(); assertTrue(QuizCommand.canPlayAgain(lastTimePlayed) < lastTimePlayed); } + + @Test + public void testRandomNumberGenerator() { + int getRandom = QuizCommand.random(3); + assertTrue(getRandom >= 0 && getRandom <= 3); + } } From 3641292f5e552a0c510c424999dcbb0ce4233f06 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:47:13 +0100 Subject: [PATCH 54/80] refactoring: Deleted unused imports --- src/test/java/org/bitbiome/commands/QuizCommandTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/bitbiome/commands/QuizCommandTest.java b/src/test/java/org/bitbiome/commands/QuizCommandTest.java index 520ae87..cb16ee6 100644 --- a/src/test/java/org/bitbiome/commands/QuizCommandTest.java +++ b/src/test/java/org/bitbiome/commands/QuizCommandTest.java @@ -2,7 +2,6 @@ package org.bitbiome.commands; import org.junit.jupiter.api.Test; -import java.util.Random; import static org.junit.jupiter.api.Assertions.*; From 46422e4d5cb8fdda1987285d3277f355967ebc4f Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:52:01 +0100 Subject: [PATCH 55/80] unittest: Edited test for QuizCommand This test tests the print function --- src/test/java/org/bitbiome/commands/QuizCommandTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/org/bitbiome/commands/QuizCommandTest.java b/src/test/java/org/bitbiome/commands/QuizCommandTest.java index cb16ee6..937b97d 100644 --- a/src/test/java/org/bitbiome/commands/QuizCommandTest.java +++ b/src/test/java/org/bitbiome/commands/QuizCommandTest.java @@ -28,4 +28,9 @@ public class QuizCommandTest { int getRandom = QuizCommand.random(3); assertTrue(getRandom >= 0 && getRandom <= 3); } + + @Test + public void testPrintFunction() { + assertEquals("I am a unit test!", QuizCommand.print("I am a unit test!")); + } } From bf45a594055377a5987cc495ffe0824259a4303f Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:54:47 +0100 Subject: [PATCH 56/80] Created BlackJackCommand and added to the CommandListener --- .../java/org/bitbiome/commands/BlackJackCommand.java | 12 ++++++++++++ .../java/org/bitbiome/commands/CommandListener.java | 1 + 2 files changed, 13 insertions(+) create mode 100644 src/main/java/org/bitbiome/commands/BlackJackCommand.java diff --git a/src/main/java/org/bitbiome/commands/BlackJackCommand.java b/src/main/java/org/bitbiome/commands/BlackJackCommand.java new file mode 100644 index 0000000..734f6b6 --- /dev/null +++ b/src/main/java/org/bitbiome/commands/BlackJackCommand.java @@ -0,0 +1,12 @@ +package org.bitbiome.commands; + + +import java.util.Scanner; + +public class BlackJackCommand implements CommandAPI { + + @Override + public void performCommand(Scanner scanner, boolean isRunning, String message) { + System.out.println("Du hast das Spiel BlackJack gestartet. Die Spielregeln lauten wie folgt: Du und dein Gegner bekommen jede Runde Zahlen von 4 - 11. \nDerjenige, der zuerst 21 Punkte hat gewinnt. Derjenige, der über 21 Punkte hat verliert. Möchte keiner mehr Karten ziehen, gewinnt der mit dem höchsten Blatt!\n"); + } +} \ No newline at end of file diff --git a/src/main/java/org/bitbiome/commands/CommandListener.java b/src/main/java/org/bitbiome/commands/CommandListener.java index 6465891..ebc7dbc 100644 --- a/src/main/java/org/bitbiome/commands/CommandListener.java +++ b/src/main/java/org/bitbiome/commands/CommandListener.java @@ -15,6 +15,7 @@ public class CommandListener { commands.put("exit", new QuitCommand()); commands.put("quit", new QuitCommand()); commands.put("quiz", new QuizCommand()); + commands.put("blackjack", new BlackJackCommand()); } public HashMap returnCommands() { From 66d9d812c79f5fb45b944bce2f4c517723b1070a Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:57:27 +0100 Subject: [PATCH 57/80] refactoring: The HashMap commands is final now --- src/main/java/org/bitbiome/commands/CommandListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/commands/CommandListener.java b/src/main/java/org/bitbiome/commands/CommandListener.java index ebc7dbc..5a9bcfe 100644 --- a/src/main/java/org/bitbiome/commands/CommandListener.java +++ b/src/main/java/org/bitbiome/commands/CommandListener.java @@ -5,7 +5,7 @@ import java.util.Scanner; public class CommandListener { - private HashMap commands; + private final HashMap commands; public CommandListener() { commands = new HashMap<>(); From d0142a9c269b5be83e5c55606409e0cab389284e Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 13:58:33 +0100 Subject: [PATCH 58/80] refactoring: Deleted unused method returnCommands() --- src/main/java/org/bitbiome/commands/CommandListener.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/CommandListener.java b/src/main/java/org/bitbiome/commands/CommandListener.java index 5a9bcfe..fdb5ce4 100644 --- a/src/main/java/org/bitbiome/commands/CommandListener.java +++ b/src/main/java/org/bitbiome/commands/CommandListener.java @@ -18,10 +18,6 @@ public class CommandListener { commands.put("blackjack", new BlackJackCommand()); } - public HashMap returnCommands() { - return commands; - } - public boolean perform(String command, Scanner scanner, boolean isRunning, String message) { From eab3f7bda10dc68b1628846408362abe7ec514ab Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:03:39 +0100 Subject: [PATCH 59/80] Created BlackJack class --- src/main/java/org/bitbiome/classes/BlackJack.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/org/bitbiome/classes/BlackJack.java diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java new file mode 100644 index 0000000..3af2dc4 --- /dev/null +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -0,0 +1,4 @@ +package org.bitbiome.classes; + +public class BlackJack { +} From 37570a200cb69ddd638a157539efe37758ec8a69 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:04:35 +0100 Subject: [PATCH 60/80] Created enum in BlackJack This is for the PLAYER and BOT --- .../java/org/bitbiome/classes/BlackJack.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index 3af2dc4..dbc3386 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -1,4 +1,35 @@ package org.bitbiome.classes; public class BlackJack { + + public enum Entity { + PLAYER(1), BOT(2); + private int value; + + private Entity(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + //Just for testing from some SO answers, but no use + public void setValue(int value) { + this.value = value; + } + + public static Entity getEventStatusById(int id) { + + Entity entity = null; + + switch (id) { + case 1 -> entity = PLAYER; + case 2 -> entity = BOT; + default -> { + } + } + return entity; + } + } + } From 1e8a16c14d86933d305b5db172a7d3716c2db61c Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:05:35 +0100 Subject: [PATCH 61/80] Created BlackJack constructor Set playername --- src/main/java/org/bitbiome/classes/BlackJack.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index dbc3386..c0bc92b 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -32,4 +32,10 @@ public class BlackJack { } } + private String playerName; + + public BlackJack(String playerName) { + this.playerName = playerName; + } + } From f5aa351958c2543ced8deb60b0210af0421dec4a Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:06:07 +0100 Subject: [PATCH 62/80] Edited BlackJack constructor Set playerPoints --- src/main/java/org/bitbiome/classes/BlackJack.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index c0bc92b..e69c7f5 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -33,9 +33,11 @@ public class BlackJack { } private String playerName; + private int playerPoints; public BlackJack(String playerName) { this.playerName = playerName; + this.playerPoints = 0; } } From b17b3bc18a237c11b7610df36174b77f34e1ab6b Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:06:35 +0100 Subject: [PATCH 63/80] Edited BlackJack constructor Set botPoints --- src/main/java/org/bitbiome/classes/BlackJack.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index e69c7f5..a3ac081 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -34,10 +34,12 @@ public class BlackJack { private String playerName; private int playerPoints; + private int botPoints; public BlackJack(String playerName) { this.playerName = playerName; this.playerPoints = 0; + this.botPoints = 0; } } From 673372adcabeb8460bae2dc2b71db1e872f076fd Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:07:16 +0100 Subject: [PATCH 64/80] Edited BlackJack constructor Set playerIn = true --- src/main/java/org/bitbiome/classes/BlackJack.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index a3ac081..bc17c37 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -35,11 +35,13 @@ public class BlackJack { private String playerName; private int playerPoints; private int botPoints; + private boolean playerIn; public BlackJack(String playerName) { this.playerName = playerName; this.playerPoints = 0; this.botPoints = 0; + this.playerIn = true; } } From 04d9439db68d8e3c3ee29ea4a96513b3387933e9 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:07:46 +0100 Subject: [PATCH 65/80] Edited BlackJack constructor Set botIn = true --- src/main/java/org/bitbiome/classes/BlackJack.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index bc17c37..e004bde 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -36,12 +36,14 @@ public class BlackJack { private int playerPoints; private int botPoints; private boolean playerIn; + private boolean botIn; public BlackJack(String playerName) { this.playerName = playerName; this.playerPoints = 0; this.botPoints = 0; this.playerIn = true; + this.botIn = true; } } From b16aab62c5ff80abdf19d17041aa625305282999 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:09:35 +0100 Subject: [PATCH 66/80] Added getPlayerName() to BlackJack --- src/main/java/org/bitbiome/classes/BlackJack.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index e004bde..5a0a341 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -46,4 +46,7 @@ public class BlackJack { this.botIn = true; } + public String getPlayerName(Entity entity) { + return entity == Entity.PLAYER ? playerName : "BitBiome"; + } } From 905e5975abc31b54e5e66e32ce51eff15b4cfbe5 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:10:08 +0100 Subject: [PATCH 67/80] Added getPoints() to BlackJack This method returns the points from the bot or the player --- src/main/java/org/bitbiome/classes/BlackJack.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index 5a0a341..b4ed1f9 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -49,4 +49,8 @@ public class BlackJack { public String getPlayerName(Entity entity) { return entity == Entity.PLAYER ? playerName : "BitBiome"; } + + public int getPoints(Entity entity) { + return entity == Entity.PLAYER ? playerPoints : botPoints; + } } From 09e3cdce30185b88c731d60e887b25fd8ce19936 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:10:40 +0100 Subject: [PATCH 68/80] Added isIn() to BlackJack This method returns whether the bot or the player is in the game --- src/main/java/org/bitbiome/classes/BlackJack.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index b4ed1f9..dfb791c 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -53,4 +53,8 @@ public class BlackJack { public int getPoints(Entity entity) { return entity == Entity.PLAYER ? playerPoints : botPoints; } + + public boolean isIn(Entity entity) { + return entity == Entity.PLAYER ? playerIn : botIn; + } } From 07921299906b9e679caa093a5559aa2adee04e51 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:11:20 +0100 Subject: [PATCH 69/80] Added getEntity() to BlackJack This method returns the Entity by the enum --- src/main/java/org/bitbiome/classes/BlackJack.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index dfb791c..b1bfce8 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -57,4 +57,8 @@ public class BlackJack { public boolean isIn(Entity entity) { return entity == Entity.PLAYER ? playerIn : botIn; } + + public Entity getEntity(int ID) { + return Entity.getEventStatusById(ID); + } } From 5239962efeaed496428b6517c12a425f048489f0 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:11:53 +0100 Subject: [PATCH 70/80] Added addPoints() to BlackJack This method adds points to the player or the bot --- src/main/java/org/bitbiome/classes/BlackJack.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index b1bfce8..60c33ec 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -61,4 +61,9 @@ public class BlackJack { public Entity getEntity(int ID) { return Entity.getEventStatusById(ID); } + + public void addPoints(Entity entity, int points) { + if (entity == Entity.BOT) botPoints += points; + if (entity == Entity.PLAYER) playerPoints += points; + } } From 0b3ef70def0b30900e646f7d7ddbaf2e8b408171 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:12:48 +0100 Subject: [PATCH 71/80] Added botWantsToPlay() to BlackJack This method returns a boolean whether the bot wants to play or not specified by the points he has currently --- .../java/org/bitbiome/classes/BlackJack.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index 60c33ec..9df3478 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -1,5 +1,7 @@ package org.bitbiome.classes; +import java.util.Random; + public class BlackJack { public enum Entity { @@ -66,4 +68,25 @@ public class BlackJack { if (entity == Entity.BOT) botPoints += points; if (entity == Entity.PLAYER) playerPoints += points; } + + public boolean botWantsToPlay() { + if (botIn) { + if (botPoints <= 10) { + return true; + } else if (botPoints <= 17) { + int r = new Random().nextInt(1, 9); + if (r <= 3) { + botIn = false; + return false; + } else { + return true; + } + } else { + botIn = false; + return false; + } + } else { + return false; + } + } } From 4897a2321cf504b1bfab3747972f2522323791dc Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:13:19 +0100 Subject: [PATCH 72/80] Added playerOut() to BlackJack This method sets the current playerIn state to false --- src/main/java/org/bitbiome/classes/BlackJack.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/bitbiome/classes/BlackJack.java b/src/main/java/org/bitbiome/classes/BlackJack.java index 9df3478..19d210e 100644 --- a/src/main/java/org/bitbiome/classes/BlackJack.java +++ b/src/main/java/org/bitbiome/classes/BlackJack.java @@ -89,4 +89,8 @@ public class BlackJack { return false; } } + + public void playerOut() { + this.playerIn = false; + } } From b1c0059a7494004896fad6f082291ead77ea3b0b Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:32:13 +0100 Subject: [PATCH 73/80] Added methods and stuff to BlackJackCommand The blackjack game works now --- .../bitbiome/commands/BlackJackCommand.java | 100 +++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/BlackJackCommand.java b/src/main/java/org/bitbiome/commands/BlackJackCommand.java index 734f6b6..52e854a 100644 --- a/src/main/java/org/bitbiome/commands/BlackJackCommand.java +++ b/src/main/java/org/bitbiome/commands/BlackJackCommand.java @@ -1,12 +1,108 @@ package org.bitbiome.commands; +import org.bitbiome.classes.BlackJack; + +import java.util.Random; import java.util.Scanner; public class BlackJackCommand implements CommandAPI { - + private boolean over; @Override public void performCommand(Scanner scanner, boolean isRunning, String message) { - System.out.println("Du hast das Spiel BlackJack gestartet. Die Spielregeln lauten wie folgt: Du und dein Gegner bekommen jede Runde Zahlen von 4 - 11. \nDerjenige, der zuerst 21 Punkte hat gewinnt. Derjenige, der über 21 Punkte hat verliert. Möchte keiner mehr Karten ziehen, gewinnt der mit dem höchsten Blatt!\n"); + + System.out.println("Du hast das Spiel BlackJack gestartet. Die Spielregeln lauten wie folgt: Du und dein Gegner bekommen jede Runde Zahlen von 4 - 11. \nDerjenige, der zuerst 21 Punkte hat gewinnt. Derjenige, der über 21 Punkte hat verliert. Möchte keiner mehr Karten ziehen, gewinnt der mit dem höchsten Blatt!\nViel Spaß!"); + + over = false; + spielen(); + } + + public void spielen() { + BlackJack bj = new BlackJack("Dave"); + Scanner sc = new Scanner(System.in); + BlackJack.Entity player = bj.getEntity(1); + while (!over) { + int r = new Random().nextInt(4, 11); + bj.addPoints(player, r); + System.out.println(bj.getPlayerName(player) + " hat " + r + " bekommen. Er hat insgesamt " + bj.getPoints(player) + "."); + + if (bj.getPoints(player) >= 21) { + over21(player, bj); + has21(player, bj); + over = true; + break; + } + System.out.print("Weiter?"); + + if (player == BlackJack.Entity.BOT) { + if (bj.botWantsToPlay()) { + System.out.println("Na klar!"); + } else { + System.out.println("Nope, ich bin fertig."); + } + } else { + String eingabe = sc.nextLine(); + if (!eingabe.toLowerCase().startsWith("j")) { + bj.playerOut(); + } + } + + + player = switchPlayer(player, bj); + + } + } + + public void over21(BlackJack.Entity player, BlackJack bj) { + if (bj.getPoints(player) > 21) { + over = true; + System.out.println(bj.getPlayerName(player) + " hat über 21 Punkte und damit verloren."); + } + + } + + + public void has21(BlackJack.Entity player, BlackJack bj) { + if (bj.getPoints(player) == 21) { + System.out.println(bj.getPlayerName(player) + " hat gewonnen! Du hast 21 Punkte!"); + over = true; + } + } + + public BlackJack.Entity switchPlayer(BlackJack.Entity player, BlackJack bj) { + BlackJack.Entity BOT = BlackJack.Entity.BOT; + BlackJack.Entity PLAYER = BlackJack.Entity.PLAYER; + if (bj.isIn(BOT) || bj.isIn(PLAYER)) { + if (player == PLAYER) { + if (bj.isIn(BOT)) { + return BOT; + } + return PLAYER; + } else { + if (bj.isIn(PLAYER)) { + return PLAYER; + } + return BOT; + } + } else { + over = true; + getWinner(bj); + return null; + } + } + + + public void getWinner(BlackJack bj) { + BlackJack.Entity entity; + if (bj.getPoints(BlackJack.Entity.BOT) < bj.getPoints(BlackJack.Entity.PLAYER)) { + entity = BlackJack.Entity.PLAYER; + System.out.println(bj.getPlayerName(entity) + " hat gewonnen, da er mehr Punkte hat!"); + } else if (bj.getPoints(BlackJack.Entity.BOT) == bj.getPoints(BlackJack.Entity.PLAYER)){ + System.out.println("Es ist Gleichstand!"); + } else { + entity = BlackJack.Entity.BOT; + System.out.println(bj.getPlayerName(entity) + " hat gewonnen, da er mehr Punkte hat!"); + } + } } \ No newline at end of file From c558b1287cd5a47f01ebfe6d851eb62ad53df080 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:51:06 +0100 Subject: [PATCH 74/80] unittest: Created BlackJackTest This test tests the Entity ID from the Player --- .../org/bitbiome/classes/BlackJackTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/test/java/org/bitbiome/classes/BlackJackTest.java diff --git a/src/test/java/org/bitbiome/classes/BlackJackTest.java b/src/test/java/org/bitbiome/classes/BlackJackTest.java new file mode 100644 index 0000000..35247ff --- /dev/null +++ b/src/test/java/org/bitbiome/classes/BlackJackTest.java @@ -0,0 +1,20 @@ +package org.bitbiome.classes; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BlackJackTest { + + private static BlackJack bj; + @BeforeAll + public static void setUpTest() { + bj = new BlackJack("UnitTest"); + } + @Test + public void testGetEntity() { + assertEquals(BlackJack.Entity.PLAYER, bj.getEntity(1)); + } + +} From 9edfb370d379aec6b06298d8a3910f751a9b0161 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:52:01 +0100 Subject: [PATCH 75/80] unittest: Added test to BlackJackTest This test tests the entity ID of the bot --- src/test/java/org/bitbiome/classes/BlackJackTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/org/bitbiome/classes/BlackJackTest.java b/src/test/java/org/bitbiome/classes/BlackJackTest.java index 35247ff..be098bc 100644 --- a/src/test/java/org/bitbiome/classes/BlackJackTest.java +++ b/src/test/java/org/bitbiome/classes/BlackJackTest.java @@ -17,4 +17,9 @@ public class BlackJackTest { assertEquals(BlackJack.Entity.PLAYER, bj.getEntity(1)); } + @Test + public void testGetEntityBot() { + assertEquals(BlackJack.Entity.BOT, bj.getEntity(2)); + } + } From c6d9d282a1be2561615a820804898c5ec6a3b77a Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:53:21 +0100 Subject: [PATCH 76/80] unittest: Added test to BlackJackTest This tests the playername --- src/test/java/org/bitbiome/classes/BlackJackTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/org/bitbiome/classes/BlackJackTest.java b/src/test/java/org/bitbiome/classes/BlackJackTest.java index be098bc..bc4a408 100644 --- a/src/test/java/org/bitbiome/classes/BlackJackTest.java +++ b/src/test/java/org/bitbiome/classes/BlackJackTest.java @@ -22,4 +22,9 @@ public class BlackJackTest { assertEquals(BlackJack.Entity.BOT, bj.getEntity(2)); } + @Test + public void testPlayerName() { + assertEquals("UnitTest", bj.getPlayerName(BlackJack.Entity.PLAYER)); + } + } From 44b4ac4d9b11174a2509c105196ce0229ce537a5 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:53:54 +0100 Subject: [PATCH 77/80] unittest: Added test to BlackJackTest This tests the bot name of BlackJack --- src/test/java/org/bitbiome/classes/BlackJackTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/org/bitbiome/classes/BlackJackTest.java b/src/test/java/org/bitbiome/classes/BlackJackTest.java index bc4a408..d6da1c2 100644 --- a/src/test/java/org/bitbiome/classes/BlackJackTest.java +++ b/src/test/java/org/bitbiome/classes/BlackJackTest.java @@ -27,4 +27,9 @@ public class BlackJackTest { assertEquals("UnitTest", bj.getPlayerName(BlackJack.Entity.PLAYER)); } + @Test + public void testBotName() { + assertEquals("BitBiome", bj.getPlayerName(BlackJack.Entity.BOT)); + } + } From bfe11ff7d9b92ba2085170e4554835b80c1a6270 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:56:25 +0100 Subject: [PATCH 78/80] unittest: Added test to BlackJackTest This tests whether the player is in the game or not --- src/test/java/org/bitbiome/classes/BlackJackTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/org/bitbiome/classes/BlackJackTest.java b/src/test/java/org/bitbiome/classes/BlackJackTest.java index d6da1c2..0534913 100644 --- a/src/test/java/org/bitbiome/classes/BlackJackTest.java +++ b/src/test/java/org/bitbiome/classes/BlackJackTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class BlackJackTest { @@ -32,4 +33,9 @@ public class BlackJackTest { assertEquals("BitBiome", bj.getPlayerName(BlackJack.Entity.BOT)); } + @Test + public void testPlayerIsIn() { + assertTrue(bj.isIn(BlackJack.Entity.PLAYER)); + } + } From c08df3bc8f4ca92263ffeced2ed0796712f139b2 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Mon, 6 Feb 2023 14:59:11 +0100 Subject: [PATCH 79/80] unittest: Added test to BlackJackTest This tests whether the bot is in the game or not --- src/test/java/org/bitbiome/classes/BlackJackTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/bitbiome/classes/BlackJackTest.java b/src/test/java/org/bitbiome/classes/BlackJackTest.java index 0534913..30752e0 100644 --- a/src/test/java/org/bitbiome/classes/BlackJackTest.java +++ b/src/test/java/org/bitbiome/classes/BlackJackTest.java @@ -3,8 +3,7 @@ package org.bitbiome.classes; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public class BlackJackTest { @@ -38,4 +37,8 @@ public class BlackJackTest { assertTrue(bj.isIn(BlackJack.Entity.PLAYER)); } + @Test + public void testBotIsIn() { + assertTrue(bj.isIn(BlackJack.Entity.BOT)); + } } From d9d97cd7c294abe9ec2da37d4cf554e3be00593c Mon Sep 17 00:00:00 2001 From: David Hermann Date: Tue, 7 Feb 2023 12:16:09 +0100 Subject: [PATCH 80/80] Hotfix for merge --- src/main/java/org/bitbiome/Boot.java | 3 +- .../org/bitbiome/classes/InteractionLoop.java | 4 +-- .../java/org/bitbiome/classes/JsonParser.java | 19 +----------- .../org/bitbiome/classes/TravelEngine.java | 11 +++---- .../bitbiome/commands/BlackJackCommand.java | 3 +- .../org/bitbiome/commands/QuizCommand.java | 12 ++++--- .../java/org/bitbiome/entities/Player.java | 5 ++- src/main/resources/playerconfig.json | 31 ++++++++++--------- src/main/resources/quiz.json | 2 +- 9 files changed, 36 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/bitbiome/Boot.java b/src/main/java/org/bitbiome/Boot.java index 0f5ebdd..4852196 100644 --- a/src/main/java/org/bitbiome/Boot.java +++ b/src/main/java/org/bitbiome/Boot.java @@ -26,8 +26,7 @@ public class Boot { private Player getPlayerSave() { String name; - JsonParser jp = new JsonParser(); - JSONObject playerconfig = jp.getJSONObject("playerconfig.json"); + JSONObject playerconfig = JsonParser.getJSONObject("src/main/resources/playerconfig.json"); name = playerconfig.getString("name"); return new Player(name); } diff --git a/src/main/java/org/bitbiome/classes/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java index f8a6c84..4948f5c 100644 --- a/src/main/java/org/bitbiome/classes/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -40,10 +40,10 @@ public class InteractionLoop { + Colors.ANSI_RESET); String name = input.nextLine(); JsonParser jp = new JsonParser(); - JSONObject playerconf = jp.getJSONObject("playerconfig.json"); + JSONObject playerconf = JsonParser.getJSONObject("src/main/resources/playerconfig.json"); playerconf.put("name", name); travelEngine.getPlayer().setName(name); - jp.writeObject("playerconfig.json", playerconf); + JsonParser.writeObject("src/main/resources/playerconfig.json", playerconf); } } diff --git a/src/main/java/org/bitbiome/classes/JsonParser.java b/src/main/java/org/bitbiome/classes/JsonParser.java index e7ad5b7..d9657e1 100644 --- a/src/main/java/org/bitbiome/classes/JsonParser.java +++ b/src/main/java/org/bitbiome/classes/JsonParser.java @@ -1,13 +1,10 @@ package org.bitbiome.classes; import org.json.JSONObject; -import org.json.JSONTokener; import java.io.FileWriter; import java.io.IOException; import java.io.FileReader; -import java.io.InputStream; - public class JsonParser { @@ -26,7 +23,7 @@ public class JsonParser { https://github.com/stleary/JSON-java */ - public static JSONObject readJSONFile(String filePath) { + public static JSONObject getJSONObject(String filePath) { StringBuilder sb = null; try { FileReader reader = new FileReader(filePath); @@ -58,18 +55,4 @@ public class JsonParser { throw new RuntimeException(e); } } - - public void writeObject(String fileName, JSONObject object) { - - String resourceName = System.getProperty("user.dir") + "/src/main/resources/" + fileName; - try { - FileWriter fw = new FileWriter(resourceName, false); - fw.write(object.toString(1)); - fw.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - - - } } diff --git a/src/main/java/org/bitbiome/classes/TravelEngine.java b/src/main/java/org/bitbiome/classes/TravelEngine.java index a8d3213..0573cd9 100644 --- a/src/main/java/org/bitbiome/classes/TravelEngine.java +++ b/src/main/java/org/bitbiome/classes/TravelEngine.java @@ -10,21 +10,19 @@ import java.util.ArrayList; public class TravelEngine { private JSONArray locations; - private JsonParser jp; private Player player; public TravelEngine(Player player) { - jp = new JsonParser(); - locations = jp.getJSONObject("gameconfig.json").getJSONArray("locations"); + locations = JsonParser.getJSONObject("src/main/resources/gameconfig.json").getJSONArray("locations"); this.player = player; } public void travelTo(Location location) { player.setLocation(location); - JSONObject jObj = jp.getJSONObject("playerconfig.json"); + JSONObject jObj = JsonParser.getJSONObject("src/main/resources/playerconfig.json"); jObj.put("currentLocation", location.getName()); - jp.writeObject("playerconfig.json", jObj); + JsonParser.writeObject("src/main/resources/playerconfig.json", jObj); } public Player getPlayer() { @@ -45,8 +43,7 @@ public class TravelEngine { } public Location getLocationByName(String name) { - JsonParser jp = new JsonParser(); - JSONObject gameconfig = jp.getJSONObject("gameconfig.json"); + JSONObject gameconfig = JsonParser.getJSONObject("src/main/resources/gameconfig.json"); JSONArray locations = gameconfig.getJSONArray("locations"); JSONObject location = null; if (locationExists(name)) { diff --git a/src/main/java/org/bitbiome/commands/BlackJackCommand.java b/src/main/java/org/bitbiome/commands/BlackJackCommand.java index 52e854a..9794e95 100644 --- a/src/main/java/org/bitbiome/commands/BlackJackCommand.java +++ b/src/main/java/org/bitbiome/commands/BlackJackCommand.java @@ -2,6 +2,7 @@ package org.bitbiome.commands; import org.bitbiome.classes.BlackJack; +import org.bitbiome.classes.TravelEngine; import java.util.Random; import java.util.Scanner; @@ -9,7 +10,7 @@ import java.util.Scanner; public class BlackJackCommand implements CommandAPI { private boolean over; @Override - public void performCommand(Scanner scanner, boolean isRunning, String message) { + public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) { System.out.println("Du hast das Spiel BlackJack gestartet. Die Spielregeln lauten wie folgt: Du und dein Gegner bekommen jede Runde Zahlen von 4 - 11. \nDerjenige, der zuerst 21 Punkte hat gewinnt. Derjenige, der über 21 Punkte hat verliert. Möchte keiner mehr Karten ziehen, gewinnt der mit dem höchsten Blatt!\nViel Spaß!"); diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index db0316e..59d215f 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -4,6 +4,8 @@ package org.bitbiome.commands; import org.bitbiome.classes.JsonParser; import org.json.JSONArray; import org.json.JSONObject; +import org.bitbiome.classes.TravelEngine; + import java.util.Date; import java.util.Random; @@ -12,11 +14,11 @@ import java.util.Scanner; public class QuizCommand implements CommandAPI { private Scanner quizScanner; @Override - public void performCommand(Scanner scanner, boolean isRunning, String message) { + public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) { quizScanner = new Scanner(System.in); - String path = "src\\main\\resources\\quiz.json"; - JSONObject quiz = JsonParser.readJSONFile(path); + String path = "src/main/resources/quiz.json"; + JSONObject quiz = JsonParser.getJSONObject(path); long diffTime = canPlayAgain(quiz.getLong("lastPlayed")); if (diffTime > 0) { @@ -76,8 +78,8 @@ public class QuizCommand implements CommandAPI { } public static int addGold() { - String playerpath = "src\\main\\resources\\playerconfig.json"; - JSONObject playerconfig = JsonParser.readJSONFile(playerpath); + String playerpath = "src/main/resources/ssplayerconfig.json"; + JSONObject playerconfig = JsonParser.getJSONObject(playerpath); int gold = playerconfig.getInt("gold"); gold = gold + 5; playerconfig.put("gold", gold); diff --git a/src/main/java/org/bitbiome/entities/Player.java b/src/main/java/org/bitbiome/entities/Player.java index 3d46e17..72a903c 100644 --- a/src/main/java/org/bitbiome/entities/Player.java +++ b/src/main/java/org/bitbiome/entities/Player.java @@ -12,13 +12,12 @@ public class Player { private ArrayList inventory; - private JsonParser jp; + public Player(String name) { - jp = new JsonParser(); this.name = name; hp = 100.0F; - location = new Location(jp.getJSONObject("playerconfig.json").getString("currentLocation"), new ArrayList<>(), new ArrayList<>()); + location = new Location(JsonParser.getJSONObject("src/main/resources/playerconfig.json").getString("currentLocation"), new ArrayList<>(), new ArrayList<>()); inventory = new ArrayList<>(); } diff --git a/src/main/resources/playerconfig.json b/src/main/resources/playerconfig.json index 1a2c55e..9da94a1 100644 --- a/src/main/resources/playerconfig.json +++ b/src/main/resources/playerconfig.json @@ -1,17 +1,18 @@ { - "name": "null", - "gold": 0, - "hp": 10, - "currentLocation": "Wald", - "inventory": [ - { - "name": "Holz", - "amount": "5", - "durability": 1000 - },{ - "name": "Stein", - "amount": "5", - "durability": 1000 - } - ] + "gold": 0, + "name": "Dave", + "hp": 10, + "inventory": [ + { + "amount": "5", + "durability": 1000, + "name": "Holz" + }, + { + "amount": "5", + "durability": 1000, + "name": "Stein" + } + ], + "currentLocation": "Wald" } \ No newline at end of file diff --git a/src/main/resources/quiz.json b/src/main/resources/quiz.json index a36366a..ae61307 100644 --- a/src/main/resources/quiz.json +++ b/src/main/resources/quiz.json @@ -201,5 +201,5 @@ ] } ], - "lastPlayed": 0 + "lastPlayed": 1675768444160 } \ No newline at end of file