Skip to content
Open

Dev #23

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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ build/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store
/.idea/
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Tic-Tac-Toe

## Description
This project is an implementation of the classic game "Tic-Tac-Toe" in Java using Servlets, JSP, and JSTL. The game operates through a web interface.

## Installation
To run this project, you will need Maven and an application server that supports Servlets, such as Apache Tomcat.

### Installation Steps:
1. Clone the repository:
```bash
git clone https://github.com/AriiSib/project-servlet.git
```
2. Navigate to the project directory:
```bash
cd project-servlet
```
3. Compile and build the project using Maven:
```bash
mvn clean install
```
4. Deploy the generated WAR file on your application server.
The application was developed on Tomcat 10.1.24.
Application context:`/`

## Usage
After deployment, open the URL of your application server in a browser (e.g., `http://localhost:8080/start`) to start the game.
20 changes: 14 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,24 @@

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
</dependency>

</dependencies>

<build>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/tictactoe/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public Sign checkWin() {

for (List<Integer> winPossibility : winPossibilities) {
if (field.get(winPossibility.get(0)) == field.get(winPossibility.get(1))
&& field.get(winPossibility.get(0)) == field.get(winPossibility.get(2))) {
&& field.get(winPossibility.get(0)) == field.get(winPossibility.get(2))
&& field.get(winPossibility.get(0)) != Sign.EMPTY) {
return field.get(winPossibility.get(0));
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/tictactoe/InitServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.tictactoe;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "InitServlet", value = "/start")
public class InitServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession currentSession = req.getSession();

Field field = new Field();
Map<Integer, Sign> fieldData = field.getField();
List<Sign> data = field.getFieldData();

currentSession.setAttribute("field", field);
currentSession.setAttribute("data", data);

getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
83 changes: 83 additions & 0 deletions src/main/java/com/tictactoe/LogicServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.tictactoe;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet(name = "LogicServlet", value = "/logic")
public class LogicServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession currentSession = req.getSession();

Field field = extractField(currentSession);

int index = getSelectedIndex(req);
Sign currentSign = field.getField().get(index);
if (Sign.EMPTY != currentSign) {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
dispatcher.forward(req, resp);
}

field.getField().put(index, Sign.CROSS);
if (checkWin(resp, currentSession, field)) {
return;
}

int emptyFieldIndex = field.getEmptyFieldIndex();

if (emptyFieldIndex >= 0) {
field.getField().put(emptyFieldIndex, Sign.NOUGHT);
if (checkWin(resp, currentSession, field)) {
return;
}
} else {
currentSession.setAttribute("draw", true);
List<Sign> data = field.getFieldData();
currentSession.setAttribute("data", data);
resp.sendRedirect("/index.jsp");
return;
}

List<Sign> data = field.getFieldData();
currentSession.setAttribute("data", data);
currentSession.setAttribute("field", field);

resp.sendRedirect("/index.jsp");
}

private boolean checkWin(HttpServletResponse response, HttpSession currentSession, Field field) throws IOException {
Sign winner = field.checkWin();
if (Sign.CROSS == winner || Sign.NOUGHT == winner) {
currentSession.setAttribute("winner", winner);
List<Sign> data = field.getFieldData();
currentSession.setAttribute("data", data);

response.sendRedirect("/index.jsp");
return true;
}
return false;
}

private Field extractField(HttpSession currentSession) {
Object fieldAttribute = currentSession.getAttribute("field");
if (Field.class != fieldAttribute.getClass()) {
currentSession.invalidate();
throw new RuntimeException("Session is broken, try one more time");
}
return (Field) fieldAttribute;
}

private int getSelectedIndex(HttpServletRequest request) {
String click = request.getParameter("click");
boolean isNumeric = click.chars().allMatch(Character::isDigit);
return isNumeric ? Integer.parseInt(click) : 0;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/tictactoe/RestartServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.tictactoe;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(name = "RestartServlet", value = "/restart")
public class RestartServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().invalidate();
resp.sendRedirect("/start");
}
}
54 changes: 51 additions & 3 deletions src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -1,17 +1,65 @@
<%@ page import="com.tictactoe.Sign" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<script src="<c:url value="/static/jquery-3.6.0.min.js"/>"></script>
<link href="static/main.css" rel="stylesheet">
<title>Tic-Tac-Toe</title>
</head>
<body>
<h1>Tic-Tac-Toe</h1>

<table>
<tr>
<td onclick="window.location='/logic?click=0'">${data.get(0).getSign()}</td>
<td onclick="window.location='/logic?click=1'">${data.get(1).getSign()}</td>
<td onclick="window.location='/logic?click=2'">${data.get(2).getSign()}</td>
</tr>
<tr>
<td onclick="window.location='/logic?click=3'">${data.get(3).getSign()}</td>
<td onclick="window.location='/logic?click=4'">${data.get(4).getSign()}</td>
<td onclick="window.location='/logic?click=5'">${data.get(5).getSign()}</td>
</tr>
<tr>
<td onclick="window.location='/logic?click=6'">${data.get(6).getSign()}</td>
<td onclick="window.location='/logic?click=7'">${data.get(7).getSign()}</td>
<td onclick="window.location='/logic?click=8'">${data.get(8).getSign()}</td>
</tr>
</table>

<script>
<hr>
<c:set var="CROSSES" value="<%=Sign.CROSS%>"/>
<c:set var="NOUGHTS" value="<%=Sign.NOUGHT%>"/>

</script>
<c:if test="${winner == CROSSES}">
<h1>CROSSES WIN!</h1>
<button onclick="restart()">Start again</button>
</c:if>
<c:if test="${winner == NOUGHTS}">
<h1>NOUGHTS WIN!</h1>
<button onclick="restart()">Start again</button>
</c:if>

<c:if test="${draw}">
<h1>IT'S A DRAW</h1>
<br>
<button onclick="restart()">Start again</button>
</c:if>

<script>
function restart() {
$.ajax({
url: '/restart',
type: 'POST',
contentType: 'application/json;charset=UTF-8',
async: false,
success: function () {
location.reload();
}
});
}
</script>
</body>
</html>
11 changes: 11 additions & 0 deletions src/main/webapp/static/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
td {
border: 3px solid black;
padding: 10px;
border-collapse: separate;
margin: 10px;
width: 100px;
height: 100px;
font-size: 50px;
text-align: center;
empty-cells: show;
}