From 2a694079b332bc058306535fe0d7dff659ca4cea Mon Sep 17 00:00:00 2001 From: Anastasiia Dosyn Date: Sun, 16 Jun 2024 18:29:25 +0200 Subject: [PATCH 01/29] added and edited doGet method in LogicServlet class --- src/main/java/com/tictactoe/LogicServlet.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/main/java/com/tictactoe/LogicServlet.java diff --git a/src/main/java/com/tictactoe/LogicServlet.java b/src/main/java/com/tictactoe/LogicServlet.java new file mode 100644 index 00000000..355cc4b6 --- /dev/null +++ b/src/main/java/com/tictactoe/LogicServlet.java @@ -0,0 +1,61 @@ +package com.tictactoe; + +import javax.servlet.RequestDispatcher; +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; + +@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 currentField = extractField(currentSession); + + int index = getSelectedIndex(req); + Sign currentSign = currentField.getField().get(index); + + if (Sign.EMPTY != currentSign) { + RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp"); + dispatcher.forward(req, resp); + return; + } + currentField.getField().put(index, Sign.CROSS); + + int emptyFieldIndex = currentField.getEmptyFieldIndex(); + + if (emptyFieldIndex >= 0) { + currentField.getField().put(emptyFieldIndex, Sign.NOUGHT); + } + + List data = currentField.getFieldData(); + + currentSession.setAttribute("data", data); + currentSession.setAttribute("field", currentField); + + resp.sendRedirect("/index.jsp"); + } + + private int getSelectedIndex(HttpServletRequest request) { + String click = request.getParameter("click"); + if (click != null && click.matches("\\d+")) { + return Integer.parseInt(click); + } else { + return 0; + } + } + + private Field extractField(HttpSession session) { + Object field = session.getAttribute("field"); + if(Field.class != field.getClass()) { + session.invalidate(); + throw new RuntimeException("Session is broken, try one more time"); + } + return (Field) field; + } +} From 0d48d79e2c0fa77af4bd353668462146b41f0304 Mon Sep 17 00:00:00 2001 From: Anastasiia Dosyn Date: Sun, 16 Jun 2024 18:30:12 +0200 Subject: [PATCH 02/29] added InitServlet class and doGet method in this class --- src/main/java/com/tictactoe/InitServlet.java | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/com/tictactoe/InitServlet.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..6b5217f5 --- /dev/null +++ b/src/main/java/com/tictactoe/InitServlet.java @@ -0,0 +1,30 @@ +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); + + getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp); + //запит і відповідь будуть надсилатися на index.jsp + } +} \ No newline at end of file From fcb8ec1a3e128e9536198fdb2b3e6297a6257229 Mon Sep 17 00:00:00 2001 From: Anastasiia Dosyn Date: Sun, 16 Jun 2024 18:32:14 +0200 Subject: [PATCH 03/29] created a game display, added basic styles --- src/main/webapp/index.jsp | 19 +++++++++++++++++++ src/main/webapp/static/main.css | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 964cc071..80754c43 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -4,10 +4,29 @@ Tic-Tac-Toe + + <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Tic-Tac-Toe

+ + + + + + + + + + + + + + + + +
${data.get(0).getSign()}${data.get(1).getSign()}${data.get(2).getSign()}
${data.get(3).getSign()}${data.get(4).getSign()}${data.get(5).getSign()}
${data.get(6).getSign()}${data.get(7).getSign()}${data.get(8).getSign()}
From 5a068e107a5b528d9b9bf1f3a789025c9c12235b Mon Sep 17 00:00:00 2001 From: Anastasiia Dosyn Date: Sun, 16 Jun 2024 19:45:04 +0200 Subject: [PATCH 07/29] restart button added --- src/main/webapp/index.jsp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index b7e70642..e6dd2c19 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -7,6 +7,7 @@ Tic-Tac-Toe <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +

Tic-Tac-Toe

@@ -35,13 +36,25 @@

CROSSES WIN!

+

NOUGHTS WIN!

+
From e565dcd5e7736ee5fe52f068266891f37b45ea33 Mon Sep 17 00:00:00 2001 From: Anastasiia Dosyn Date: Sun, 16 Jun 2024 19:49:08 +0200 Subject: [PATCH 08/29] RestartServlet added --- src/main/java/com/tictactoe/RestartServlet.java | 17 +++++++++++++++++ 1 file changed, 17 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..fdd03d00 --- /dev/null +++ b/src/main/java/com/tictactoe/RestartServlet.java @@ -0,0 +1,17 @@ +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 = "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"); + } +} From 7a2ff43988a7687dcd63c556cfe3d21906149b59 Mon Sep 17 00:00:00 2001 From: Anastasiia Dosyn Date: Sun, 16 Jun 2024 19:49:25 +0200 Subject: [PATCH 09/29] RestartServlet added --- src/main/java/com/tictactoe/RestartServlet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/tictactoe/RestartServlet.java b/src/main/java/com/tictactoe/RestartServlet.java index fdd03d00..909ee7d0 100644 --- a/src/main/java/com/tictactoe/RestartServlet.java +++ b/src/main/java/com/tictactoe/RestartServlet.java @@ -10,7 +10,7 @@ @WebServlet(name = "RestartServlet", value = "/restart") public class RestartServlet extends HttpServlet { @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { req.getSession().invalidate(); resp.sendRedirect("/start"); } From 1c6c05795bb44ccc4068813c4f8578cbc0f0ebb2 Mon Sep 17 00:00:00 2001 From: Anastasiia Dosyn Date: Sun, 16 Jun 2024 19:49:36 +0200 Subject: [PATCH 10/29] RestartServlet added --- src/main/java/com/tictactoe/RestartServlet.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/tictactoe/RestartServlet.java b/src/main/java/com/tictactoe/RestartServlet.java index 909ee7d0..019c3ce8 100644 --- a/src/main/java/com/tictactoe/RestartServlet.java +++ b/src/main/java/com/tictactoe/RestartServlet.java @@ -1,6 +1,5 @@ package com.tictactoe; -import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; From f948e63a9fadc47836a5bae697be2dde8d0357fb Mon Sep 17 00:00:00 2001 From: Anastasiia Dosyn Date: Sun, 16 Jun 2024 19:52:13 +0200 Subject: [PATCH 11/29] added logic for draw --- src/main/java/com/tictactoe/LogicServlet.java | 8 ++++++++ src/main/webapp/index.jsp | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/com/tictactoe/LogicServlet.java b/src/main/java/com/tictactoe/LogicServlet.java index b35eb16e..68ece36d 100644 --- a/src/main/java/com/tictactoe/LogicServlet.java +++ b/src/main/java/com/tictactoe/LogicServlet.java @@ -36,6 +36,14 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se if (checkWin(resp, currentSession, currentField)) { return; } + }else{ + currentSession.setAttribute("draw", true); + + List data = currentField.getFieldData(); + currentSession.setAttribute("data", data); + + resp.sendRedirect("/index.jsp"); + return; } List data = currentField.getFieldData(); diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index e6dd2c19..44468684 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -42,6 +42,11 @@

NOUGHTS WIN!

+ +

IT'S A DRAW

+
+ +