From 1f9f63bccdf1b63d6038bc8b1cc46d9b8d9debc0 Mon Sep 17 00:00:00 2001 From: nkuzmischev Date: Sat, 2 Mar 2024 09:52:52 +0300 Subject: [PATCH 1/9] from the first to the seventh point --- src/main/java/com/tictactoe/Field.java | 4 ++++ src/main/webapp/index.jsp | 20 ++++++++++++++++---- src/main/webapp/static/main.css | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/tictactoe/Field.java b/src/main/java/com/tictactoe/Field.java index c52d2a0d..ae733f2b 100644 --- a/src/main/java/com/tictactoe/Field.java +++ b/src/main/java/com/tictactoe/Field.java @@ -25,6 +25,7 @@ public Map getField() { return field; } + //getEmptyFieldIndex ищет первую незанятую ячейку public int getEmptyFieldIndex() { return field.entrySet().stream() .filter(e -> e.getValue() == Sign.EMPTY) @@ -32,6 +33,8 @@ public int getEmptyFieldIndex() { .findFirst().orElse(-1); } + //getFieldData возвращает значения мапы “field” + //в виде списка,отсортированного в порядке возрастания индексов. public List getFieldData() { return field.entrySet().stream() .sorted(Map.Entry.comparingByKey()) @@ -39,6 +42,7 @@ public List getFieldData() { .collect(Collectors.toList()); } + //checkWin проверяет не завершена ли игра public Sign checkWin() { List> winPossibilities = List.of( List.of(0, 1, 2), diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 964cc071..fa99e99a 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -4,14 +4,26 @@ Tic-Tac-Toe +

Tic-Tac-Toe

- - + + + + + + + + + + + + + + +
012
345
678
- \ No newline at end of file diff --git a/src/main/webapp/static/main.css b/src/main/webapp/static/main.css index e69de29b..0a8ac786 100644 --- a/src/main/webapp/static/main.css +++ b/src/main/webapp/static/main.css @@ -0,0 +1,22 @@ +td { + border-top-width: 3px; + border-top-style: solid; + border-top-color: black; + border-right-width: 3px; + border-right-style: solid; + border-right-color: black; + border-left-width: 3px; + border-left-style: solid; + border-left-color: black; + border-bottom-width: 3px; + border-bottom-style: solid; + border-bottom-color: black; + padding: 10px; + border-collapse: separate; + margin: 10px; + width: 100px; + height: 100px; + font-size: 50px; + text-align: center; + empty-cells: show; +} \ No newline at end of file From c03421c6c6f89e7d313650ab43c51dee7c787cdd Mon Sep 17 00:00:00 2001 From: nkuzmischev Date: Sat, 2 Mar 2024 09:53:15 +0300 Subject: [PATCH 2/9] from the first to the seventh point --- .idea/.gitignore | 8 ++++++++ .idea/encodings.xml | 7 +++++++ .idea/inspectionProfiles/Project_Default.xml | 21 ++++++++++++++++++++ .idea/misc.xml | 12 +++++++++++ .idea/vcs.xml | 6 ++++++ 5 files changed, 54 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..3af5b525 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,21 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..aacacf55 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From b705bb82fd445001f405d47291b53529520c6131 Mon Sep 17 00:00:00 2001 From: nkuzmischev Date: Sat, 2 Mar 2024 10:13:02 +0300 Subject: [PATCH 3/9] p.8-9 --- src/main/java/com/tictactoe/LogicServlet.java | 24 +++++++++++++++++++ src/main/webapp/index.jsp | 20 +++++++++------- 2 files changed, 35 insertions(+), 9 deletions(-) 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..e2f29c34 --- /dev/null +++ b/src/main/java/com/tictactoe/LogicServlet.java @@ -0,0 +1,24 @@ +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"); + } + + 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 fa99e99a..50f5377d 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -9,18 +9,20 @@

Tic-Tac-Toe

- - - - - - + + + - - - + + + + + + + +
012
345012
678345
678

Tic-Tac-Toe

@@ -25,8 +27,37 @@ ${data.get(7).getSign()} ${data.get(8).getSign()} +
+ + + + +

CROSSES WIN!

+
+ +

NOUGHTS WIN!

+ +

CROSSES WIN!

+ +
+ +

NOUGHTS WIN!

+ +
+
\ No newline at end of file From 03a10c55a44be31254833be3be891cbd3715118b Mon Sep 17 00:00:00 2001 From: nkuzmischev Date: Sun, 3 Mar 2024 16:47:20 +0300 Subject: [PATCH 9/9] finisch commit --- src/main/java/com/tictactoe/InitServlet.java | 10 +--- src/main/java/com/tictactoe/LogicServlet.java | 51 +++++++++++-------- src/main/webapp/index.jsp | 42 +++++++-------- 3 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/tictactoe/InitServlet.java b/src/main/java/com/tictactoe/InitServlet.java index 003ed9e9..1088e6ac 100644 --- a/src/main/java/com/tictactoe/InitServlet.java +++ b/src/main/java/com/tictactoe/InitServlet.java @@ -12,26 +12,18 @@ @WebServlet(name = "InitServlet", value = "/start") public class InitServlet extends HttpServlet { - @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - // Creating a new session HttpSession currentSession = req.getSession(true); - // Creating the game field Field field = new Field(); Map fieldData = field.getField(); - // Getting the list of field values List data = field.getFieldData(); - // Adding field parameters to the session currentSession.setAttribute("field", field); - - // Adding sorted field values by index to the session currentSession.setAttribute("data", data); - // Redirecting the request to the index.jsp page getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp); } -} +} \ No newline at end of file diff --git a/src/main/java/com/tictactoe/LogicServlet.java b/src/main/java/com/tictactoe/LogicServlet.java index 68f57c9c..3eec3466 100644 --- a/src/main/java/com/tictactoe/LogicServlet.java +++ b/src/main/java/com/tictactoe/LogicServlet.java @@ -14,13 +14,10 @@ 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); @@ -31,26 +28,32 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se return; } - 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 data = field.getFieldData(); - List data = field.getFieldData(); + currentSession.setAttribute("data", data); + + resp.sendRedirect("/index.jsp"); + return; + } + List data = field.getFieldData(); currentSession.setAttribute("data", data); currentSession.setAttribute("field", field); @@ -58,21 +61,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se 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 fieldAttribute = currentSession.getAttribute("field"); - if (Field.class != fieldAttribute.getClass()) { - currentSession.invalidate(); - throw new RuntimeException("Session is broken, try one more time"); - } - return (Field) fieldAttribute; - } - + /** + * Метод проверяет, нет ли трех крестиков/ноликов в ряд. + * Возвращает true/false + */ private boolean checkWin(HttpServletResponse response, HttpSession currentSession, Field field) throws IOException { Sign winner = field.checkWin(); if (Sign.CROSS == winner || Sign.NOUGHT == winner) { @@ -87,4 +79,19 @@ private boolean checkWin(HttpServletResponse response, HttpSession currentSessio } return false; } + + 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 fieldAttribute = currentSession.getAttribute("field"); + if (Field.class != fieldAttribute.getClass()) { + currentSession.invalidate(); + throw new RuntimeException("Session is broken, try one more time"); + } + return (Field) fieldAttribute; + } } \ No newline at end of file diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 73f3d1b1..13d1dfa6 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -4,13 +4,14 @@ - Tic-Tac-Toe <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + Tic-Tac-Toe

Tic-Tac-Toe

+ @@ -27,27 +28,27 @@ -
- - - - -

CROSSES WIN!

-
- -

NOUGHTS WIN!

- -

CROSSES WIN!

- -
- -

NOUGHTS WIN!

- -
-
${data.get(0).getSign()}${data.get(7).getSign()} ${data.get(8).getSign()}
+ +
+ + + + +

CROSSES WIN!

+ +
+ +

NOUGHTS WIN!

+ +
+ +

IT'S A DRAW

+ +
+ + \ No newline at end of file