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
26 changes: 20 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,31 @@
<maven.compiler.source>18</maven.compiler.source>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<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>
<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>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
</dependency>
</dependencies>

Expand Down
80 changes: 80 additions & 0 deletions src/main/java/com/tictactoe/LogicServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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("/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);
//Empty cell checking
if (Sign.EMPTY != currentSign) {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
dispatcher.forward(req, resp);
return;
}
//Put "cross" and win checking
field.getField().put(index, Sign.CROSS);
if (checkWin(resp, currentSession, field)) {
return;
}
//Put "nought" in a first empty cell, check win or draw
int emptyFieldIndex = field.getEmptyFieldIndex();
if (emptyFieldIndex >= 0) {
field.getField().put(emptyFieldIndex, Sign.NOUGHT);
if (checkWin(resp, currentSession, field)) {
return;
}
} else { //draw
currentSession.setAttribute("draw", true);
List<Sign> data = field.getFieldData();
currentSession.setAttribute("data", data);
resp.sendRedirect("/index.jsp");
return;
}
//updated field is got and saved
List<Sign> data = field.getFieldData();
currentSession.setAttribute("data", data);
currentSession.setAttribute("field", field);
resp.sendRedirect("/index.jsp");
}

private int getSelectedIndex(HttpServletRequest request) {
String click = request.getParameter("click");
boolean isNumeric = click.chars().allMatch(Character::isDigit);
return isNumeric ? Integer.parseInt(click) : 0;
}

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

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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/tictactoe/RestartServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tictactoe;

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("/restart")
public class RestartServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
req.getSession().invalidate();
resp.sendRedirect("/start");
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/tictactoe/initServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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;

@WebServlet("/start")
public class initServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession currentSession = req.getSession(true);
Field field = new Field();
List<Sign> data = field.getFieldData();
currentSession.setAttribute("field", field);
currentSession.setAttribute("data", data);
getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);

}
}
52 changes: 50 additions & 2 deletions src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -1,16 +1,64 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.tictactoe.Sign" %>
<%@ page contentType="text/html;charset=UTF-8" %>

<!DOCTYPE html>
<html>
<head>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<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'">${sessionScope.data.get(0).getSign()}</td>
<td onclick="window.location='/logic?click=1'">${sessionScope.data.get(1).getSign()}</td>
<td onclick="window.location='/logic?click=2'">${sessionScope.data.get(2).getSign()}</td>
</tr>
<tr>
<td onclick="window.location='/logic?click=3'">${sessionScope.data.get(3).getSign()}</td>
<td onclick="window.location='/logic?click=4'">${sessionScope.data.get(4).getSign()}</td>
<td onclick="window.location='/logic?click=5'">${sessionScope.data.get(5).getSign()}</td>
</tr>
<tr>
<td onclick="window.location='/logic?click=6'">${sessionScope.data.get(6).getSign()}</td>
<td onclick="window.location='/logic?click=7'">${sessionScope.data.get(7).getSign()}</td>
<td onclick="window.location='/logic?click=8'">${sessionScope.data.get(8).getSign()}</td>
</tr>
</table>

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

<script>
<c:if test="${sessionScope.winner == CROSSES}">
<h1>CROSSES WIN!</h1>
<button onclick="restart()">Start again</button>
</c:if>
<c:if test="${sessionScope.winner == NOUGHTS}">
<h1>NOUGHTS WIN!</h1>
<button onclick="restart()">Start again</button>
</c:if>
<c:if test="${sessionScope.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>
Expand Down
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;
}