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
12 changes: 12 additions & 0 deletions Anna/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM maven:3.8.5-openjdk-17-slim AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests


FROM tomcat:9.0-jdk17

COPY --from=build /app/target/Anna.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
CMD ["catalina.sh", "run"]
44 changes: 44 additions & 0 deletions Anna/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Gods' Trials - Text-Based Adventure Game

This is a text-based adventure game where the player must pass the trials of the gods to obtain their power and prevent
a catastrophe. The game is built using Java (Servlets, JSP, JSTL, Maven, JUnit) and runs on Tomcat 9. It uses Bootstrap
5 for styling.

This project is deployed on **Render** https://m3-quest-new-2ziq.onrender.com

---

## 📌 Features

- Interactive Story: Answer questions from the gods and make the right choices.
- Session-Based Progress: The game tracks your progress using session attributes.
- Dynamic UI: Each question displays a corresponding god’s image.
- Game Over & Restart: If you fail a challenge, you can restart the game.

---

## 🚀 Getting Started

### Prerequisites

- Java 11+ (Recommended: OpenJDK 17)
- Maven (For dependency management)
- Tomcat 9 (For running the application)

### Installation

Clone the repository:

git clone https://github.com/Anna-Yavorska/M3-Quest-NEW

## 🛠️ Technologies Used

**Backend**: Java, Servlets, JSP, JSTL

**Frontend**: Bootstrap 5, CSS

**Build Tool**: Maven

**Testing**: JUnit 5

**Server**: Tomcat 9
5 changes: 5 additions & 0 deletions Anna/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
web:
build: .
ports:
- "8080:8080"
105 changes: 105 additions & 0 deletions Anna/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<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>org.example</groupId>
<artifactId>Anna</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Anna Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.version>17</java.version>

<lombok.version>1.18.36</lombok.version>

<junit-jupiter-engine.version>5.12.0</junit-jupiter-engine.version>

<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<jakarta.servlet.jsp.jstl.version>3.0.1</jakarta.servlet.jsp.jstl.version>
<jstl.version>1.2</jstl.version>

<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>

</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>${jakarta.servlet.jsp.jstl.version}</version>
</dependency>

<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>${jakarta.servlet.jsp.jstl.version}</version>
</dependency>

</dependencies>
<build>
<finalName>Anna</finalName>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
</plugin>

</plugins>
</build>
</project>
75 changes: 75 additions & 0 deletions Anna/src/main/java/controller/QuizServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package controller;

import model.Question;
import repository.QuestionRepository;

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;
import java.util.ArrayList;

@WebServlet(name = "QuizServlet", value = "/quiz")
public class QuizServlet extends HttpServlet {
private static final QuestionRepository questionRepository = QuestionRepository.getInstance();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();

if (session.getAttribute("gameOver") != null && (boolean) session.getAttribute("gameOver")) {
session.invalidate();
session = req.getSession(true);
}

ArrayList<Question> questions = questionRepository.getQuestions();
session.setAttribute("questions", questions);

Integer currentIndex = (Integer) session.getAttribute("currentIndex");
if (currentIndex == null) {
currentIndex = 0;
session.setAttribute("currentIndex", currentIndex);
}

Question currentQuestion = questions.get(currentIndex);
session.setAttribute("currentQuestionText", currentQuestion.getText());
session.setAttribute("currentQuestionAnswers", currentQuestion.getAnswers());

getServletContext().getRequestDispatcher("/quiz.jsp").forward(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
ArrayList<Question> questions = (ArrayList<Question>) session.getAttribute("questions");
Integer currentIndex = (Integer) session.getAttribute("currentIndex");

if (questions == null || currentIndex == null) {
resp.sendRedirect("index.jsp");
return;
}

Question currentQuestion = questions.get(currentIndex);
int correctAnswer = currentQuestion.getCorrectAnswer();
int userAnswer = Integer.parseInt(req.getParameter("answer"));

if (userAnswer == correctAnswer) {
currentIndex++;
if (currentIndex < questions.size()) {
session.setAttribute("currentIndex", currentIndex);
resp.sendRedirect("quiz");
} else {
session.setAttribute("gameOver", true);
session.setAttribute("win", true);
resp.sendRedirect("result.jsp");
}
} else {
session.setAttribute("gameOver", true);
session.setAttribute("win", false);
resp.sendRedirect("result.jsp");
}
}
}
15 changes: 15 additions & 0 deletions Anna/src/main/java/model/Question.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model;

import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;

@Data
@Builder
public class Question {
private String text;
private ArrayList<String> answers;
private int correctAnswer;

}
56 changes: 56 additions & 0 deletions Anna/src/main/java/repository/QuestionRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package repository;

import model.Question;

import java.util.ArrayList;
import java.util.List;

public class QuestionRepository {
private static QuestionRepository INSTANCE;
private ArrayList<Question> questions;

private QuestionRepository() {
questions = new ArrayList<>();

questions.add(Question.builder()
.text("Грім роздирає небо, і ти стоїш перед Зевсом. Він каже: “Смертний, щоб отримати силу блискавки, ти маєш знати, що найважливіше для правителя. Що ти обереш?”")
.answers(new ArrayList<>(List.of("Справедливість", "Сила", "Хитрість", "Помста")))
.correctAnswer(0)
.build());

questions.add(Question.builder()
.text("Хвилі розступаються, і перед тобою постає Посейдон. “Лише той, хто розуміє природу океану, може керувати його силою. Що є головною сутністю моря?”")
.answers(new ArrayList<>(List.of("Гнів", "Спокій", "Непередбачуваність", "Глибина")))
.correctAnswer(2)
.build());

questions.add(Question.builder()
.text("Афіна спостерігає за тобою пильним поглядом. “Перемогу здобувають не лише мечем, а й розумом. Що важливіше у битві?”")
.answers(new ArrayList<>(List.of("Сміливість", "Стратегія", "Швидкість", "Честь")))
.correctAnswer(1)
.build());

questions.add(Question.builder()
.text("Ти спускаєшся в Підземний світ, і перед тобою Аїд. “Смертний, чи ти розумієш істину життя? Що найбільше лякає людей?”")
.answers(new ArrayList<>(List.of("Біль", "Забуття", "Темрява", "Самотність")))
.correctAnswer(1)
.build());

questions.add(Question.builder()
.text("Арес грізно дивиться на тебе. “Лише найсильніший воїн заслуговує силу богів! Що є справжньою сутністю війни?”")
.answers(new ArrayList<>(List.of("Руйнування", "Слава", "Виживання", "Влада")))
.correctAnswer(2)
.build());
}

public static QuestionRepository getInstance() {
if (INSTANCE == null) {
INSTANCE = new QuestionRepository();
}
return INSTANCE;
}

public ArrayList<Question> getQuestions() {
return questions;
}
}
7 changes: 7 additions & 0 deletions Anna/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
48 changes: 48 additions & 0 deletions Anna/src/main/webapp/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
body {
background-color: #3e2723;
color: #ffcc80;
text-align: center;
padding: 20px;
font-family: 'Comic Sans MS', sans-serif;
}

.container {
max-width: 800px;
}

.text-shadow {
text-shadow: 3px 3px 5px rgba(0, 0, 0, 0.7);
}

.btn-custom {
background-color: gold;
color: black;
font-size: 1.2em;
padding: 10px 20px;
border-radius: 10px;
border: none;
}

.btn-custom:hover {
background-color: darkgoldenrod;
}

.game-image {
border-radius: 10px;
margin-bottom: 20px;
}

.card-title {
color: #000000;
}

.card {
background-color: #baac8d;
color: #5E4B3C;
}

.btn-primary {
color: #000000;
background-color: rgb(237, 202, 15);
border-color: black;
}
Loading