-
Notifications
You must be signed in to change notification settings - Fork 671
jsp proj #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
jsp proj #17
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| 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; | ||
|
|
||
| @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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add Logs |
||
| Field field = new Field(); | ||
| List<Sign> data = field.getFieldData(); | ||
| currentSession.setAttribute("field", field); | ||
| currentSession.setAttribute("data", data); | ||
| getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| 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; | ||
|
|
||
| import static com.tictactoe.Sign.*; | ||
|
|
||
| @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 (EMPTY != currentSign) { | ||
| RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add logs |
||
| dispatcher.forward(req, resp); | ||
| return; | ||
| } | ||
|
|
||
| field.getField().put(index, CROSS); | ||
| int emptyFieldIndex = field.getEmptyFieldIndex(); | ||
| if (checkWinner(field, currentSession, resp)) { | ||
| return; | ||
| } | ||
| if (emptyFieldIndex != -1) { | ||
| field.getField().put(emptyFieldIndex, NOUGHT); | ||
| if (checkWinner(field, currentSession, resp)) { | ||
| 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 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; | ||
| } | ||
|
|
||
| private boolean checkWinner(Field field, HttpSession currentSession, HttpServletResponse resp) throws IOException { | ||
| var winner = field.checkWin(); | ||
| if (winner != EMPTY) { | ||
| currentSession.setAttribute("winner", winner); | ||
| List<Sign> data = field.getFieldData(); | ||
| currentSession.setAttribute("data", data); | ||
| resp.sendRedirect("/index.jsp"); | ||
| return true; | ||
|
|
||
| } | ||
| return false; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,59 @@ | ||
| <%@ page import="com.tictactoe.Sign" %> | ||
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> | ||
|
|
||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Tic-Tac-Toe</title> | ||
| <link href="static/main.css" rel="stylesheet"> | ||
| <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> | ||
| <script src="<c:url value="/static/jquery-3.6.0.min.js"/>"></script> | ||
| </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> | ||
| <c:choose> | ||
| <c:when test="${sessionScope.winner eq Sign.CROSS}"> | ||
| <h1>You win</h1> | ||
| </c:when> | ||
| <c:when test="${sessionScope.winner eq Sign.NOUGHT}"> | ||
| <h1>You lose</h1> | ||
| </c:when> | ||
| <c:when test="${sessionScope.draw eq true}"> | ||
| <h1>Draw</h1> | ||
| <button onclick="restart()">Start again</button> | ||
| </c:when> | ||
| <c:otherwise><h1>Playing</h1></c:otherwise> | ||
| </c:choose> | ||
| <script> | ||
|
|
||
| function restart() { | ||
| $.ajax({ | ||
| url: "/restart", | ||
| type: 'POST', | ||
| contentType:'application/json; charset=UTF-8', | ||
| async:false, | ||
| success:function () { | ||
| location.reload() | ||
| } | ||
| }) | ||
| } | ||
| </script> | ||
|
|
||
| </body> | ||
| </html> | ||
| </body> |
Large diffs are not rendered by default.
| 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; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use 17