Tic Tac Toe
Tic Tac Toe with 4 modes and challenges HTML - CSS - JS
by Ali sadeghi
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Tic Tac Toe</title>
</head>
<body>
<div id="xo-game">
<div id="lines">
<div id="automatic-click"></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div id="win-horizontal-lines">
<hr id="win-line1" />
<hr id="win-line2" />
<hr id="win-line3" />
</div>
<div id="win-vertical-lines">
<hr id="win-line4" />
<hr id="win-line5" />
<hr id="win-line6" />
</div>
<div id="win-diagonal-lines">
<hr id="win-line7" />
<hr id="win-line8" />
</div>
</div>
<div id="choose-xo">
<div id="choose-xo-div">
<p>Do you want to be <span>X</span> or <span>O</span> ?</p>
<div id="xo-btns">
<div id="x-btn">X</div>
<div id="o-btn">O</div>
</div>
</div>
</div>
<div id="disable-click"></div>
<div id="end-screen">
<p id="win-txt"></p>
<div id="end-game-btns">
<a
href="https://github.com/Ali-Sdg90/tic-tac-toe"
target="_blank"
title="Github"
id="endGame-github-btn"
>⭐</a
>
<a href="#" id="endGame-reload-page-btn" title="Play again"
>↺</a
...
CSS
* {
padding: 0;
margin: 0;
box-sizing: border-box;
cursor: default;
}
:root {
--background-color: rgb(121, 204, 248);
--game-color: rgb(61, 102, 124);
--lineColor: rgb(31, 51, 62);
}
::selection {
background: transparent;
}
body {
background: var(--background-color);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100%;
overflow: hidden;
}
#xo-game {
width: 70vh;
height: 70vh;
}
#lines {
width: 100%;
height: 100%;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 0.53vh;
background: var(--lineColor);
transition: background 0.7s;
}
#lines div {
display: flex;
justify-content: center;
align-items: center;
background: var(--background-color);
font-size: 20vh;
font-family: cursive;
cursor: pointer;
color: var(--game-color);
overflow: hidden;
}
hr {
background: var(--lineColor);
border: 0.25vh solid red;
border-radius: 10vh;
background: red;
margin: auto;
opacity: 0;
}
#win-horizontal-lines,
#win-vertical-lines,
#win-diagonal-lines {
display: none;
/* display: grid; */
height: 100%;
width: 100%;
position: relative;
}
#win-horizontal-lines {
grid-template-rows: repeat(3, 1fr);
grid-template-columns: 1fr;
top: -100%;
}
#win-horizontal-lines hr {
width: 100%;
height: 1.2vh;
}
#win-vertical-lines {
grid-template-rows: 1fr;
grid-template-columns: repeat(3, 1fr);
top: -200%;
}
#win-vertical-lines hr {
width: 1.2vh;
height: 100%;
}
#win-diagonal-lines {
top: -300%;
}
#win-diagonal-lines hr {
position: absolute;
width: 90vh;
height: 1.2vh;
top: 50%;
left: 50%;
}
#win-diagonal-lines hr:nth-child(1) {
transform: translate(-50%, -50%) rotate(45deg);
}
#win-diagonal-lines hr:nth-child(2) {
transform: translate(-50%, -50%)...
JavaScript
const XOBlocks = Array.from(document.querySelectorAll("#lines div"));
const disableClick = document.getElementById("disable-click");
const challengeMenu = document.getElementById("challenge-menu");
const runChallengesInp = document.getElementById("run-challenges-inp");
const showChallengesInp = document.getElementById("show-challenges-inp");
let playerXO = "X";
let opponentXO = "O";
let debugOutput = "";
let possibleOpponentPlace = [];
let roundNO = 0;
let hardGameMode = true;
let askXO = true;
let gameMode = "default"; //"default" "only-player" "w-friend" "unbeatable"
let pageColorChange = true;
let challengesCanRun = false;
let showDebug = false;
let canPlay = true;
let XOMatrix = [
["", "", ""],
["", "", ""],
["", "", ""],
];
// localStorage.clear();
//2023/01/01 IRAN AZAD.
const welcomeBlack = document.getElementById("black-welcome");
welcomeBlack.style.display = "block";
welcomeBlack.style.opacity = "1";
setTimeout(() => {
welcomeBlack.style.opacity = "0";
setTimeout(() => {
welcomeBlack.style.display = "none";
}, 500);
}, 10);
//Add settings if available
if (localStorage.getItem("XO-Setting")) {
let localSettingObj = JSON.parse(localStorage.getItem("XO-Setting"));
hardGameMode = localSettingObj.hardGameModeSave;
askXO = localSettingObj.askXOSave;
gameMode = localSettingObj.gameModeSave;
pageColorChange = localSettingObj.pageColorChangeSave;
challengesCanRun = localSettingObj.challengesCanRunSave;
showDebug = localSettingObj.showDebugSave;
}
//Add challenges if available
if (localStorage.getItem("XO-Challenge")) {
let localChallengeObj = JSON.parse(localStorage.getItem("XO-Challenge"));
document.getElementById("win-Default-challenge-inp").checked =
localChallengeObj.defaultWinSave;
document.getElementById("draw-Default-challenge-inp").checked =
localChallengeObj.defaultDrawSave;
document.getElementById("lose-Default-challenge-inp").checked =
...