getChoices() {
+ return choices;
+ }
+
+ public boolean hasChoices() {
+ return !choices.isEmpty();
+ }
+ }
+}
\ No newline at end of file
diff --git a/Yaroslav/src/main/webapp/game.jsp b/Yaroslav/src/main/webapp/game.jsp
new file mode 100644
index 0000000..da30a52
--- /dev/null
+++ b/Yaroslav/src/main/webapp/game.jsp
@@ -0,0 +1,20 @@
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<%@ page import="com.game.model.GameState" %>
+<%
+ GameState gameState = (GameState) session.getAttribute("gameState");
+%>
+
+
+ Text Adventure
+
+
+
+Text Adventure
+<%= gameState.getCurrentText() %>
+
+
+
+
\ No newline at end of file
diff --git a/Yaroslav/src/main/webapp/index.jsp b/Yaroslav/src/main/webapp/index.jsp
new file mode 100644
index 0000000..dfe5d39
--- /dev/null
+++ b/Yaroslav/src/main/webapp/index.jsp
@@ -0,0 +1,15 @@
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+
+
+ Start Game
+
+
+
+Welcome to the Text Adventure!
+
+
+
\ No newline at end of file
diff --git a/Yaroslav/src/main/webapp/result.jsp b/Yaroslav/src/main/webapp/result.jsp
new file mode 100644
index 0000000..fea0cad
--- /dev/null
+++ b/Yaroslav/src/main/webapp/result.jsp
@@ -0,0 +1,21 @@
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<%@ page import="com.game.model.Player" %>
+<%
+ Player player = (Player) session.getAttribute("player");
+%>
+
+
+ Game Over
+
+
+
+Game Over!
+Player Name: <%= player.getName() %>
+Games Played: <%= player.getGamesPlayed() %>
+Games Won: <%= player.getGamesWon() %>
+
+
+
+
\ No newline at end of file
From 751d51578c8580682976976b85a78e4aadd0dd34 Mon Sep 17 00:00:00 2001
From: Yaroslav <6i4bbb@gmail.com>
Date: Thu, 27 Feb 2025 13:38:07 +0200
Subject: [PATCH 2/2] JUnit tests added
---
Yaroslav/pom.xml | 24 ++++++--
.../java/com/game/model/GameStateTest.java | 57 +++++++++++++++++++
.../test/java/com/game/model/StoryTest.java | 57 +++++++++++++++++++
3 files changed, 132 insertions(+), 6 deletions(-)
create mode 100644 Yaroslav/src/test/java/com/game/model/GameStateTest.java
create mode 100644 Yaroslav/src/test/java/com/game/model/StoryTest.java
diff --git a/Yaroslav/pom.xml b/Yaroslav/pom.xml
index c12dd3d..4d91ca8 100644
--- a/Yaroslav/pom.xml
+++ b/Yaroslav/pom.xml
@@ -12,12 +12,18 @@
-
- junit
- junit
- 3.8.1
- test
-
+
+ org.junit.jupiter
+ junit-jupiter-api
+ 5.9.0
+ test
+
+
+
+ org.junit.jupiter
+ junit-jupiter-engine
+ 5.9.0
+
@@ -26,6 +32,12 @@
4.0.1
provided
+
+ org.junit.jupiter
+ junit-jupiter
+ RELEASE
+ test
+
diff --git a/Yaroslav/src/test/java/com/game/model/GameStateTest.java b/Yaroslav/src/test/java/com/game/model/GameStateTest.java
new file mode 100644
index 0000000..5d97349
--- /dev/null
+++ b/Yaroslav/src/test/java/com/game/model/GameStateTest.java
@@ -0,0 +1,57 @@
+package com.game.model;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+
+class GameStateTest {
+ private GameState gameState;
+
+ @BeforeEach
+ void setUp() {
+ gameState = new GameState();
+ }
+
+ @Test
+ void testInitialState() {
+ assertEquals("You stand at a fork in the road. Where do you go? (left/right)", gameState.getCurrentText());
+ assertFalse(gameState.isGameOver(), "Game should not be over at the start");
+ }
+
+ @Test
+ void testProcessChoice_ValidPath_Left() {
+ gameState.processChoice("left");
+ assertEquals("You found a chest. Open it? (yes/no)", gameState.getCurrentText());
+ assertFalse(gameState.isGameOver());
+ }
+
+ @Test
+ void testProcessChoice_ValidPath_Right() {
+ gameState.processChoice("right");
+ assertEquals("You met a wolf. Run away? (yes/no)", gameState.getCurrentText());
+ assertFalse(gameState.isGameOver());
+ }
+
+ @Test
+ void testProcessChoice_Ending_Open() {
+ gameState.processChoice("left");
+ gameState.processChoice("yes"); // Opens the chest
+ assertEquals("You found a treasure! (end)", gameState.getCurrentText());
+ assertTrue(gameState.isGameOver(), "Game should be over after finding the treasure");
+ }
+
+ @Test
+ void testProcessChoice_Ending_Run() {
+ gameState.processChoice("right");
+ gameState.processChoice("yes"); // Runs away from wolf
+ assertEquals("You escaped safely. (end)", gameState.getCurrentText());
+ assertTrue(gameState.isGameOver(), "Game should be over after escaping");
+ }
+
+ @Test
+ void testProcessChoice_InvalidInput() {
+ gameState.processChoice("unknown");
+ assertEquals("You stand at a fork in the road. Where do you go? (left/right)", gameState.getCurrentText());
+ assertFalse(gameState.isGameOver(), "Game should not be over after an invalid input");
+ }
+}
\ No newline at end of file
diff --git a/Yaroslav/src/test/java/com/game/model/StoryTest.java b/Yaroslav/src/test/java/com/game/model/StoryTest.java
new file mode 100644
index 0000000..4513f66
--- /dev/null
+++ b/Yaroslav/src/test/java/com/game/model/StoryTest.java
@@ -0,0 +1,57 @@
+package com.game.model;
+
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.Map;
+
+class StoryTest {
+ private Story story;
+
+ @BeforeEach
+ void setUp() {
+ story = new Story();
+ }
+
+ @Test
+ void testGetScene_ValidScene() {
+ Story.Scene scene = story.getScene("start");
+ assertNotNull(scene, "Scene should not be null");
+ assertEquals("You stand at a fork in the road. Where do you go? (left/right)", scene.getText());
+ assertEquals(Map.of("left", "left_path", "right", "right_path"), scene.getChoices());
+ }
+
+ @Test
+ void testGetScene_InvalidScene() {
+ Story.Scene scene = story.getScene("invalid_key");
+ assertNotNull(scene, "Scene should not be null");
+ assertEquals("Error: Unknown scene", scene.getText());
+ assertTrue(scene.getChoices().isEmpty(), "Unknown scene should have no choices");
+ }
+
+ @Test
+ void testIsEndScene_True() {
+ assertTrue(story.isEndScene("open"), "Scene 'open' should be an end scene");
+ assertTrue(story.isEndScene("ignore"), "Scene 'ignore' should be an end scene");
+ assertTrue(story.isEndScene("run"), "Scene 'run' should be an end scene");
+ assertTrue(story.isEndScene("fight"), "Scene 'fight' should be an end scene");
+ }
+
+ @Test
+ void testIsEndScene_False() {
+ assertFalse(story.isEndScene("start"), "Scene 'start' should not be an end scene");
+ assertFalse(story.isEndScene("left_path"), "Scene 'left_path' should not be an end scene");
+ assertFalse(story.isEndScene("right_path"), "Scene 'right_path' should not be an end scene");
+ }
+
+ @Test
+ void testSceneChoices() {
+ Story.Scene scene = story.getScene("left_path");
+ assertEquals(Map.of("yes", "open", "no", "ignore"), scene.getChoices());
+
+ scene = story.getScene("right_path");
+ assertEquals(Map.of("yes", "run", "no", "fight"), scene.getChoices());
+ }
+}
\ No newline at end of file