-
Notifications
You must be signed in to change notification settings - Fork 671
complete project #24
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
Open
5arav031k
wants to merge
17
commits into
vasylmalik:master
Choose a base branch
from
5arav031k:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
complete project #24
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
955c44c
add class LogicServlet, modified main.css and index.jsp (added table …
8563ac1
add class InitServlet
01946f9
modify LogicServlet & index.jsp (draw cross on click)
2098dd4
add AI move and win message
b4f7c88
add RestartServlet logic and restart button
4dcc3b2
add draw logic
19cde4b
add Field tests & add dependency
602692a
modify tests
82def6a
add tests for LogicServlet
aa2a426
delete unused variable
c79164f
add conf file and dependencies for logs
d88c1cc
fix win checking
0b21f95
add logs for InitServlet
b7b1568
add logs for LogicServlet
c76e457
add logs for RestartServlet
8b08516
Create README.md
5arav031k b9cd8eb
Delete README.md
5arav031k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.tictactoe; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| 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 { | ||
| private static final Logger LOGGER = LogManager.getLogger(InitServlet.class); | ||
|
|
||
| @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); | ||
|
|
||
| LOGGER.info("Game started, field initialized"); | ||
| getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package com.tictactoe; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| 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") | ||
|
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. not informative url |
||
| public class LogicServlet extends HttpServlet { | ||
| private static final Logger LOGGER = LogManager.getLogger(LogicServlet.class); | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
|
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. buisness logic inside servlet, move it into separate class(serive) |
||
| HttpSession currentSession = req.getSession(true); | ||
| Field field = extractField(currentSession); | ||
|
|
||
| int index = getSelectedIndex(req); | ||
| Sign currentSign = field.getField().get(index); | ||
|
|
||
| if (Sign.EMPTY != currentSign) { | ||
| LOGGER.debug("Clicked on not EMPTY sign"); | ||
| RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp"); | ||
| dispatcher.forward(req, resp); | ||
| return; | ||
| } | ||
|
|
||
| field.getField().put(index, Sign.CROSS); | ||
| if (checkWin(resp, currentSession, field)) { | ||
| LOGGER.info("Game ended"); | ||
| LOGGER.debug("Player won"); | ||
| return; | ||
| } | ||
|
|
||
| int emptyFieldIndex = field.getEmptyFieldIndex(); | ||
| if (emptyFieldIndex >= 0) { | ||
| field.getField().put(emptyFieldIndex, Sign.NOUGHT); | ||
| if (checkWin(resp, currentSession, field)) { | ||
| LOGGER.info("Game ended"); | ||
| LOGGER.debug("Computer won"); | ||
| 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 Field extractField(HttpSession currentSession) { | ||
| Object fieldAttribute = currentSession.getAttribute("field"); | ||
| if (fieldAttribute.getClass() != Field.class) { | ||
| currentSession.invalidate(); | ||
| LOGGER.error("Session is broken"); | ||
| throw new RuntimeException("Session is broken, try one more time"); | ||
| } | ||
| return (Field) fieldAttribute; | ||
| } | ||
|
|
||
| private int getSelectedIndex(HttpServletRequest request) { | ||
| String click = request.getParameter("click"); | ||
| boolean isNumeric = click.chars().allMatch(Character::isDigit); | ||
| return isNumeric ? Integer.parseInt(click) : 0; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.tictactoe; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| 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 { | ||
| private static final Logger LOGGER = LogManager.getLogger(RestartServlet.class); | ||
|
|
||
| @Override | ||
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { | ||
| req.getSession().invalidate(); | ||
| resp.sendRedirect("/start"); | ||
| LOGGER.info("Game restarted"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <Configuration status="WARN"> | ||
| <Properties> | ||
| <Property name="LOG_PATTERN">%date %level %logger{10} [%file:%line] %msg%n</Property> | ||
| <Property name="APP_LOG_ROOT">C:/Main/project-servlet</Property> | ||
| </Properties> | ||
|
|
||
| <Appenders> | ||
| <RollingFile name="FILE" fileName="${APP_LOG_ROOT}/tic-tac-toe.log" | ||
| filePattern="${APP_LOG_ROOT}/tic-tac-toe-%d{yyyy-MM-dd}-%i.log"> | ||
| <PatternLayout pattern="${LOG_PATTERN}"/> | ||
| <Policies> | ||
| <SizeBasedTriggeringPolicy size="19500KB" /> | ||
| </Policies> | ||
| <DefaultRolloverStrategy max="10"/> | ||
| </RollingFile> | ||
| </Appenders> | ||
|
|
||
| <Loggers> | ||
| <Root level="ALL"> | ||
| <AppenderRef ref="FILE"/> | ||
| </Root> | ||
| </Loggers> | ||
| </Configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,61 @@ | ||
| <%@ page import="com.tictactoe.Sign" %> | ||
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> | ||
|
|
||
| <!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> | ||
| <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:set var="CROSSES" value="<%=Sign.CROSS%>"/> | ||
| <c:set var="NOUGHTS" value="<%=Sign.NOUGHT%>"/> | ||
|
|
||
|
|
||
| <script> | ||
|
|
||
| </script> | ||
| <c:if test="${winner == CROSSES}"> | ||
| <h1>CROSSES WIN!</h1> | ||
| <button onclick="restart()">Start again</button> | ||
| </c:if> | ||
| <c:if test="${winner == NOUGHTS}"> | ||
| <h1>NOUGHTS WIN!</h1> | ||
| <button onclick="restart()">Start again</button> | ||
| </c:if> | ||
| <c:if test="${draw}"> | ||
| <h1>IT'S A DRAW</h1> | ||
| <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> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| 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; | ||
| } | ||
|
|
||
|
|
||
| body { | ||
| text-align: center; | ||
| } | ||
|
|
||
| table { | ||
| position: relative; | ||
| left: 50%; | ||
| transform: translateX(-50%); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
move all jsp's into constants