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
1 change: 0 additions & 1 deletion src/main/java/com/tictactoe/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public int getEmptyFieldIndex() {
.map(Map.Entry::getKey)
.findFirst().orElse(-1);
}

public List<Sign> getFieldData() {
return field.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/tictactoe/InitServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 session = req.getSession(true);

Field field = new Field();
Map<Integer, Sign> fieldData = field.getField();
List<Sign> data = field.getFieldData();

session.setAttribute("field", field);
session.setAttribute("data", data);
session.setAttribute("queue", Sign.CROSS);

getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
92 changes: 92 additions & 0 deletions src/main/java/com/tictactoe/LogicServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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 session = req.getSession();
Field field = extractField(session);

int index = getSelectedIndex(req);
if(checkWin(resp, session, field)){
return;
}
Sign currentSign = field.getField().get(index);

if(Sign.EMPTY != currentSign) {
RequestDispatcher dispatcher = req.getServletContext().getRequestDispatcher("/index.jsp");
dispatcher.forward(req, resp);
return;
}
if(session.getAttribute("queue") == Sign.CROSS) {
field.getField().put(index, Sign.CROSS);
if(checkWin(resp, session, field)){
return;
}
else {
session.setAttribute("queue", Sign.NOUGHT);
}
}
else {
field.getField().put(index, Sign.NOUGHT);
if(checkWin(resp, session, field)){
return;
}
else {
session.setAttribute("queue", Sign.CROSS);
}
}
if(field.getEmptyFieldIndex() == -1){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what -1 means?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method getEmptyFieldIndex return -1 if there is no empty field, what mean a draw

session.setAttribute("draw", true);
List<Sign> data = field.getFieldData();

session.setAttribute("data", data);
resp.sendRedirect("/index.jsp");
return;
}
List<Sign> data = field.getFieldData();

session.setAttribute("data", data);
session.setAttribute("field", field);

resp.sendRedirect("/index.jsp");
}
private boolean checkWin(HttpServletResponse response, HttpSession session, Field field) throws IOException {
Sign winner = field.checkWin();
if(Sign.CROSS == winner || Sign.NOUGHT == winner) {
session.setAttribute("winner", winner);

List<Sign> data = field.getFieldData();

session.setAttribute("data", data);
response.sendRedirect("/index.jsp");
return true;
}
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 session) {
Object fieldAttribute = session.getAttribute("field");
if(Field.class != fieldAttribute.getClass()) {
session.invalidate();
throw new RuntimeException("Session is broken, try one more time");
}
return (Field) fieldAttribute;
}
}
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 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");
}
}
61 changes: 59 additions & 2 deletions src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -1,16 +1,73 @@
<%@ page import="com.tictactoe.Sign" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html>
<head>
<link href="static/main.css" rel="stylesheet">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<title>Tic-Tac-Toe</title>
<script src="<c:url value="/static/jquery-3.6.0.min.js"/>"></script>
</head>
<body>
<body class="<c:choose>
<c:when test='${queue == Sign.CROSS}'>cross-turn</c:when>
<c:when test='${queue == Sign.NOUGHT}'>nought-turn</c:when>
</c:choose>">
<h1>Tic-Tac-Toe</h1>
<table>
<tr>
<td onclick="window.location='/logic?click=motherfucker'">${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>

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

<script>
<c:if test="${queue == CROSSES}">
<h1>Ход Х</h1>
</c:if>

<c:if test="${queue == NOUGHTS}">
<h1>Ход О</h1>
</c:if>
<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>
<br>
<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>
Expand Down
68 changes: 68 additions & 0 deletions src/main/webapp/static/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
}
h1 {
color: #333;
}

table {
margin: 20px auto;
border-collapse: collapse;
}

td {
width: 100px;
height: 100px;
border: 2px solid #333;
font-size: 2em;
font-weight: bold;
text-align: center;
vertical-align: middle;
cursor: pointer;
transition: background-color 0.3s ease;
}

td:hover {
background-color: #e0e0e0;
}

button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
margin-top: 20px;
border: none;
border-radius: 5px;
font-size: 1em;
cursor: pointer;
transition: background-color 0.3s ease;
}

button:hover {
background-color: #45a049;
}

hr {
margin: 20px 0;
border: none;
border-top: 2px solid #333;
}

h1:first-of-type {
margin-top: 0;
}

h1:last-of-type {
margin-bottom: 20px;
}
body.cross-turn {
background-color: #f0f8ff; /* Цвет фона для хода крестиков */
}

body.nought-turn {
background-color: #fffaf0; /* Цвет фона для хода ноликов */
}