Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/com/tictactoe/InitServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.tictactoe;

import java.io.IOException;
import java.util.List;
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;

@WebServlet(name = "InitServlet", value = "/start")
public class InitServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
HttpSession session = req.getSession(true);
Field field = new Field();
List<Sign> data = field.getFieldData();
session.setAttribute("field", field);
session.setAttribute("data", data);
getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
81 changes: 81 additions & 0 deletions src/main/java/com/tictactoe/LogicServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.tictactoe;

import java.util.List;
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 java.io.IOException;
import javax.servlet.http.HttpSession;

@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 (Sign.EMPTY != currentSign) {
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(
"/index.jsp");
requestDispatcher.forward(req, resp);
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<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 req) {
String click = req.getParameter("click");
boolean isNumeric = click.chars().allMatch(Character::isDigit);
return isNumeric ? Integer.parseInt(click) : 0;
}

private Field extractField(HttpSession session) {
Object field = session.getAttribute("field");
if (Field.class != field.getClass()) {
session.invalidate();
throw new RuntimeException("Field is not an instance of Field.class");
}
return (Field) field;
}

private boolean checkWin(HttpServletResponse resp, HttpSession currentSession, Field field)
throws IOException {
Sign winSign = field.checkWin();
if (Sign.CROSS == winSign || Sign.NOUGHT == winSign) {
currentSession.setAttribute("winner", winSign);
List<Sign> data = field.getFieldData();
currentSession.setAttribute("data", data);
resp.sendRedirect("/index.jsp");
return true;
}
return false;
}

}
17 changes: 17 additions & 0 deletions src/main/java/com/tictactoe/RestartServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tictactoe;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@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");
}
}
52 changes: 51 additions & 1 deletion src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -1,16 +1,66 @@
<%@ 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" %>
<title>Tic-Tac-Toe</title>
<link href="static/main.css" rel="stylesheet">
<script src="<c:url value="static/jquery-3.6.0.min.js"/>"></script>
</head>
<body>
<h1>Tic-Tac-Toe</h1>

<hr>
<c:set var="CROSSES" value="<%=Sign.CROSS%>"/>
<c:set var="NOUGHTS" value="<%=Sign.NOUGHT%>"/>

<script>
<c:if test="${winner == CROSSES}">
<h1>CROSSES WON!</h1>
<button onclick="restart()">Play again</button>
</c:if>

<c:if test="${winner == NOUGHTS}">
<h1>NOUGHTS WON!</h1>
<button onclick="restart()">Play again</button>
</c:if>

<c:if test="${draw}">
<h1>IT'S A DRAW!</h1>
<button onclick="restart()">Play again</button>
</c:if>

<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>

<script>
function restart() {
$.ajax({
url: '/restart',
type: 'POST',
contentType: 'application/json;charset=UTF-8',
async: false,
success: function () {
location.reload();
}
});
}
</script>

</body>
Expand Down
11 changes: 11 additions & 0 deletions src/main/webapp/static/main.css
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;
}