Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Yaroslav/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ua.com.javarush.gnew</groupId>
<artifactId>Yaroslav</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Yaroslav Maven Webapp</name>
<url>http://maven.apache.org</url>


<dependencies>


<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.0</version>
</dependency>


<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>


</dependencies>
<build>
<finalName>Yaroslav</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
54 changes: 54 additions & 0 deletions Yaroslav/src/main/java/com/game/controller/GameServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.game.controller;

import com.game.model.GameState;
import com.game.model.Player;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/game")
public class GameServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();

Player player = (Player) session.getAttribute("player");
if (player == null) {
String playerName = request.getParameter("playerName");
if (playerName == null || playerName.trim().isEmpty()) {
playerName = "Player";
}
player = new Player(playerName);
session.setAttribute("player", player);
}

GameState gameState = (GameState) session.getAttribute("gameState");
if (gameState == null) {
gameState = new GameState();
session.setAttribute("gameState", gameState);
}

if (gameState.getCurrentText().equals("You stand at a fork in the road. Where do you go? (left/right)")) {
player.incrementGamesPlayed();
}

String userChoice = request.getParameter("choice");
if (userChoice != null) {
gameState.processChoice(userChoice);
}

if (gameState.isGameOver()) {
if (gameState.getCurrentText().contains("treasure") || gameState.getCurrentText().contains("escaped")) {
player.incrementGamesWon();
}
request.getRequestDispatcher("result.jsp").forward(request, response);
} else {
request.getRequestDispatcher("game.jsp").forward(request, response);
}
}
}
50 changes: 50 additions & 0 deletions Yaroslav/src/main/java/com/game/model/GameState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.game.model;

public class GameState {
private final Story story;
private String currentScene;
private boolean gameOver;

public GameState() {
this.story = new Story();
this.currentScene = "start";
this.gameOver = false;
}

public void processChoice(String choice) {
Story.Scene scene = story.getScene(currentScene);

if (scene != null) {
System.out.println("=== PROCESSING CHOICE ===");
System.out.println("Current scene: " + currentScene);
System.out.println("Scene text: " + scene.getText());
System.out.println("Available choices: " + scene.getChoices().keySet());
System.out.println("Player input: '" + choice + "' (Length: " + choice.length() + ")");

String normalizedChoice = choice.trim().toLowerCase();
System.out.println("After processing: '" + normalizedChoice + "' (Length: " + normalizedChoice.length() + ")");

if (scene.getChoices().containsKey(normalizedChoice)) {
currentScene = scene.getChoices().get(normalizedChoice);
gameOver = story.isEndScene(currentScene);

System.out.println("Choice accepted! New scene: " + currentScene);
System.out.println("New scene text: " + story.getScene(currentScene).getText());
System.out.println("=== END OF PROCESSING ===");
} else {
System.out.println("Invalid choice! Try again.");
}
} else {
System.out.println("Error: Scene not found.");
}
}

public String getCurrentText() {
Story.Scene scene = story.getScene(currentScene);
return (scene != null) ? scene.getText() : "Error loading scene.";
}

public boolean isGameOver() {
return gameOver;
}
}
35 changes: 35 additions & 0 deletions Yaroslav/src/main/java/com/game/model/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.game.model;

import java.io.Serializable;

public class Player implements Serializable {
private String name;
private int gamesPlayed;
private int gamesWon;

public Player(String name) {
this.name = name;
this.gamesPlayed = 0;
this.gamesWon = 0;
}

public String getName() {
return name;
}

public int getGamesPlayed() {
return gamesPlayed;
}

public int getGamesWon() {
return gamesWon;
}

public void incrementGamesPlayed() {
gamesPlayed++;
}

public void incrementGamesWon() {
gamesWon++;
}
}
54 changes: 54 additions & 0 deletions Yaroslav/src/main/java/com/game/model/Story.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.game.model;

import java.util.HashMap;
import java.util.Map;

public class Story {
private final Map<String, Scene> scenes = new HashMap<>();

public Story() {
scenes.put("start", new Scene("You stand at a fork in the road. Where do you go? (left/right)",
Map.of("left", "left_path", "right", "right_path")));

scenes.put("left_path", new Scene("You found a chest. Open it? (yes/no)",
Map.of("yes", "open", "no", "ignore")));

scenes.put("right_path", new Scene("You met a wolf. Run away? (yes/no)",
Map.of("yes", "run", "no", "fight")));

scenes.put("open", new Scene("You found a treasure! (end)", Map.of()));
scenes.put("ignore", new Scene("You walked away empty-handed. (end)", Map.of()));
scenes.put("run", new Scene("You escaped safely. (end)", Map.of()));
scenes.put("fight", new Scene("The wolf was stronger... (end)", Map.of()));
}

public Scene getScene(String sceneKey) {
return scenes.getOrDefault(sceneKey, new Scene("Error: Unknown scene", Map.of()));
}

public boolean isEndScene(String sceneKey) {
return !scenes.getOrDefault(sceneKey, new Scene("", Map.of())).hasChoices();
}

public static class Scene {
private final String text;
private final Map<String, String> choices;

public Scene(String text, Map<String, String> choices) {
this.text = text;
this.choices = choices;
}

public String getText() {
return text;
}

public Map<String, String> getChoices() {
return choices;
}

public boolean hasChoices() {
return !choices.isEmpty();
}
}
}
20 changes: 20 additions & 0 deletions Yaroslav/src/main/webapp/game.jsp
Original file line number Diff line number Diff line change
@@ -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");
%>
<html>
<head>
<title>Text Adventure</title>
<meta charset="UTF-8">
</head>
<body>
<h1>Text Adventure</h1>
<p><%= gameState.getCurrentText() %></p>

<form action="game" method="post">
<input type="text" name="choice" placeholder="Enter your choice" required>
<button type="submit">Choose</button>
</form>
</body>
</html>
15 changes: 15 additions & 0 deletions Yaroslav/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Start Game</title>
<meta charset="UTF-8">
</head>
<body>
<h1>Welcome to the Text Adventure!</h1>
<form action="game" method="post">
<label>Enter your name:</label>
<input type="text" name="playerName" required>
<button type="submit">Start</button>
</form>
</body>
</html>
21 changes: 21 additions & 0 deletions Yaroslav/src/main/webapp/result.jsp
Original file line number Diff line number Diff line change
@@ -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");
%>
<html>
<head>
<title>Game Over</title>
<meta charset="UTF-8">
</head>
<body>
<h1>Game Over!</h1>
<p>Player Name: <%= player.getName() %></p>
<p>Games Played: <%= player.getGamesPlayed() %></p>
<p>Games Won: <%= player.getGamesWon() %></p>

<form action="index.jsp">
<button type="submit">Play Again</button>
</form>
</body>
</html>
57 changes: 57 additions & 0 deletions Yaroslav/src/test/java/com/game/model/GameStateTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
Loading