From 689053cb3b377a1a6a3e7ba5409d4fe3205025e4 Mon Sep 17 00:00:00 2001 From: JavaDen44 Date: Wed, 17 Aug 2022 17:09:22 +0300 Subject: [PATCH 01/11] =?UTF-8?q?Added=20a=20field=20with=20cells.=20Added?= =?UTF-8?q?=20the=20functionality=20of=20clicking=20on=20a=20cell,=20at=20?= =?UTF-8?q?which=20a=20request=20will=20be=20sent=20to=20the=20server.=20A?= =?UTF-8?q?dded=20to=20each=20cell=20the=20address=20of=20the=20servlet=20?= =?UTF-8?q?that=20is=20responsible=20for=20the=20logic.=20In=20the=20packa?= =?UTF-8?q?ge=20=E2=80=9Ccom.tictactoe=E2=80=9C,=20we=20will=20create=20a?= =?UTF-8?q?=20class=20=E2=80=9DLogicServlet"=20and=20add=20a=20method=20to?= =?UTF-8?q?=20it=20that=20will=20get=20the=20index=20of=20the=20cell=20on?= =?UTF-8?q?=20which=20the=20click=20occurred.=20Created=20the=20=E2=80=9DI?= =?UTF-8?q?nitServlet=E2=80=9C=20class,=20redefined=20the=20=E2=80=9DdoGet?= =?UTF-8?q?"=20method=20in=20it,=20in=20which=20he=20created=20a=20new=20s?= =?UTF-8?q?ession,=20a=20playing=20field,=20put=20this=20playing=20field?= =?UTF-8?q?=20and=20a=20Sign=20list=20in=20the=20session=20attributes,=20a?= =?UTF-8?q?nd=20sent=20=E2=80=9Cforward"=20to=20the=20index.jsp=20page.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/tictactoe/InitServlet.java | 47 + src/main/java/com/tictactoe/LogicServlet.java | 28 + src/main/webapp/index.jsp | 19 +- src/main/webapp/static/jquery-3.6.0.min.js | 2595 ++++++++++++++++- src/main/webapp/static/main.css | 11 + 5 files changed, 2698 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/tictactoe/InitServlet.java create mode 100644 src/main/java/com/tictactoe/LogicServlet.java diff --git a/src/main/java/com/tictactoe/InitServlet.java b/src/main/java/com/tictactoe/InitServlet.java new file mode 100644 index 00000000..58372afc --- /dev/null +++ b/src/main/java/com/tictactoe/InitServlet.java @@ -0,0 +1,47 @@ +package com.tictactoe; + +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.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(true); + + /** + * Создание игрового поля + */ + Field field = new Field(); + Map fieldData = field.getField(); + + /** + * Получение списка значений поля + */ + List data = field.getFieldData(); + + /** + * Добавление в сессию параметров поля (необходим для хранения состояния между запросами) + */ + currentSession.setAttribute("field", field); + /** + * Добавление значений поля, отсортированных по индексу (необходим для отрисовки крестиков и ноликов) + */ + currentSession.setAttribute("data", data); + + /** + * Перенаправление запроса на страницу index.jsp через сервер + */ + getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp); + } +} diff --git a/src/main/java/com/tictactoe/LogicServlet.java b/src/main/java/com/tictactoe/LogicServlet.java new file mode 100644 index 00000000..e672d86a --- /dev/null +++ b/src/main/java/com/tictactoe/LogicServlet.java @@ -0,0 +1,28 @@ +package com.tictactoe; + +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 java.io.IOException; + +@WebServlet(name = "LogicServlet", value = "/logic") +public class LogicServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + int index = getSelectedIndex(req); + resp.sendRedirect("/index.jsp"); + super.doGet(req, resp); + } + + /** + * Метод, который получает индекс ячейки, по которой произошел клик. + */ + private int getSelectedIndex(HttpServletRequest request){ + String click = request.getParameter("click"); + boolean isNumeric = click.chars().allMatch(Character::isDigit); + return isNumeric ? Integer.parseInt(click) : 0; + } +} diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 964cc071..c8738830 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -4,10 +4,27 @@ Tic-Tac-Toe +

Tic-Tac-Toe

- + + + + + + + + + + + + + + + + +
012
345
678

Tic-Tac-Toe

+ @@ -26,20 +28,33 @@ -
- - - - -

CROSSES WIN!

-
- -

NOUGHTS WIN!

-
${data.get(0).getSign()}${data.get(7).getSign()} ${data.get(8).getSign()}
- From 89f34c90a33ba40fd581e34198b4c57c75b9e7ed Mon Sep 17 00:00:00 2001 From: JavaDen44 Date: Thu, 18 Aug 2022 16:40:34 +0300 Subject: [PATCH 09/11] Created a servlet that will serve the URL "/restart". --- .../java/com/tictactoe/RestartServlet.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/java/com/tictactoe/RestartServlet.java diff --git a/src/main/java/com/tictactoe/RestartServlet.java b/src/main/java/com/tictactoe/RestartServlet.java new file mode 100644 index 00000000..cc4644d0 --- /dev/null +++ b/src/main/java/com/tictactoe/RestartServlet.java @@ -0,0 +1,19 @@ +package com.tictactoe; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Сервлет, который обслуживает URL "/restart" + */ +@WebServlet(name = "RestartServlet", value = "/restart") +public class RestartServlet extends HttpServlet { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + req.getSession().invalidate(); + resp.sendRedirect("/start"); + } +} From d59e95f73da4ae351018a5255e1430122bb79064 Mon Sep 17 00:00:00 2001 From: JavaDen44 Date: Thu, 18 Aug 2022 16:59:18 +0300 Subject: [PATCH 10/11] =?UTF-8?q?In=20"LogicServlet",=20I=20added=20anothe?= =?UTF-8?q?r=20"draw"=20parameter=20to=20the=20session,=20updated=20the=20?= =?UTF-8?q?=E2=80=9Cdata=E2=80=9D=20field=20and=20sent=20a=20redirect=20to?= =?UTF-8?q?=20"index.jsp".?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/tictactoe/LogicServlet.java | 28 +++++++++++++++++++ src/main/webapp/index.jsp | 7 ++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/tictactoe/LogicServlet.java b/src/main/java/com/tictactoe/LogicServlet.java index fd579eea..7bfa7d82 100644 --- a/src/main/java/com/tictactoe/LogicServlet.java +++ b/src/main/java/com/tictactoe/LogicServlet.java @@ -59,6 +59,9 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se */ int emptyFieldIndex = field.getEmptyFieldIndex(); + /** + * Если такая ячейка присутствует + */ if (emptyFieldIndex >= 0){ field.getField().put(emptyFieldIndex, Sign.NOUGHT); @@ -69,6 +72,31 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se return; } } + /** + * Если пустой ячейки нет и никто не победил - значит это ничья + */ + else { + /** + * Добавляем в сессию флаг, который сигнализирует что произошла ничья + */ + currentSession.setAttribute("draw", true); + + /** + * Считаем список значков + */ + List data = field.getFieldData(); + + /** + * Обновляем этот список в сессии + */ + currentSession.setAttribute("data", data); + + /** + * Шлем редирект + */ + resp.sendRedirect("/index.jsp"); + return; + } /** * Считаем список значков diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 3ca77aaf..96865e5e 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -4,10 +4,10 @@ - Tic-Tac-Toe <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + Tic-Tac-Toe

Tic-Tac-Toe

@@ -42,6 +42,11 @@

NOUGHTS WIN!

+ +

IT'S A DRAW

+
+ +