I am teaching myself JavaScript and made a little Tic-Tac-Toe web app. I'm not asking anyone to read through it all, but to just tell me what I need or should not do in the future.
var boxes = document.querySelectorAll("td");
var boxtext = document.querySelectorAll("td span");
var player = document.querySelector("#player");
var playerTurnDisplay = document.querySelector("#playerTurnDisplay");
var resetButton = document.querySelector("#resetButton");
var isXTurn = true;
var boxesLeftToClick = 9;
var gameOver = false;
function init() {
addClickListeners(boxes);
}
function addClickListeners(arr) {
for (var i = 0; i < arr.length; i++) {
arr[i].addEventListener("click", function() {
if (isEmpty(this) && gameOver === false) {
addObjectToBox(this);
isXTurn = !(isXTurn);
boxesLeftToClick--;
displayTurn();
checkWinner(arr);
}
});
resetButton.addEventListener("click", function() {
reset();
})
}
}
// is the box empty [true] or does it have an X or O in it [false]
function isEmpty(box) {
if (box.textContent !== "X" && box.textContent !== "O")
return true;
else
return false;
}
// adds the Object "O" to the board
function addCross(box) {
box.classList.add("cross");
box.textContent = "X";
}
// adds the Object "X" to the board
function addCircle(box) {
box.classList.add("circle");
box.textContent = "O";
}
// displays whos turn it is to the players
function displayTurn() {
if (boxesLeftToClick > 0) {
player.textContent = (isXTurn) ? "X" : "O";
playerTurnDisplay.classList.toggle("xTurn");
playerTurnDisplay.classList.toggle("yTurn");
} else {
playerTurnDisplay.textContent = "Draw";
playerTurnDisplay.classList.toggle(playerTurnDisplay.classList);
playerTurnDisplay.classList.add("gameOver");
}
}
// if someone wins get rid of the background color and display the text of whoever won
function setWinnerDisplay(box, orientation) {
playerTurnDisplay.classList.remove("xTurn");
playerTurnDisplay.classList.remove("yTurn");
playerTurnDisplay.classList.add("gameOver");
playerTurnDisplay.textContent = box.textContent + " wins " + orientation;
gameOver = true;
}
// adds the "line" through the winning boxes
function crossOut(box1, box2, box3) {
box1.classList.add("crossOut");
box2.classList.add("crossOut");
box3.classList.add("crossOut");
}
// all the things that need to happend when a player wins will happen if you call this function
function win(box1, box2, box3, orientation) {
setWinnerDisplay(box1, orientation);
crossOut(box1, box2, box3);
}
// returns true if the boxes are all X's or O's false if they are empty or not the same
function isTheSame(box1, box2, box3) {
if (box1.textContent === box2.textContent && box1.textContent === box3.textContent && isEmpty(box1) === false) return true;
else
return false;
}
function reset() {
for (var i = 0; i < boxes.length; i++) {
boxes[i].textContent = "";
isXTurn = true;
boxes[i].classList.remove("crossOut");
boxes[i].classList.remove("gameOver");
}
gameOver = false;
if (isXTurn) playerTurnDisplay.classList.add("xTurn");
boxesLeftToClick = 9;
}
// checks an array of 9 elements to see if their textContent is the same
function checkWinner(arr) {
if (isTheSame(arr[0], arr[1], arr[2]))
win(arr[0], arr[1], arr[2], "horizontally");
else if (isTheSame(arr[3], arr[4], arr[5]))
win(arr[3], arr[4], arr[5], "horizontally");
else if (isTheSame(arr[6], arr[7], arr[8]))
win(arr[6], arr[7], arr[8], "horizontally");
else if (isTheSame(arr[0], arr[3], arr[6]))
win(arr[0], arr[3], arr[6], "vertically");
else if (isTheSame(arr[1], arr[4], arr[7]))
win(arr[1], arr[4], arr[7], "vertically");
else if (isTheSame(arr[2], arr[5], arr[8]))
win(arr[2], arr[5], arr[8], "vertically");
else if (isTheSame(arr[0], arr[4], arr[8]))
win(arr[0], arr[4], arr[8], "diagonally");
else if (isTheSame(arr[2], arr[4], arr[6]))
win(arr[2], arr[4], arr[6], "diagonally");
}
function addObjectToBox(box) {
if (isXTurn)
addCross(box);
else
addCircle(box);
}
init();
h1 {
text-align: center;
margin: 0;
}
.gameOver {
background: #14FF00;
border: 3px solid #14FF00;
}
#nav-bar {
margin: 0 auto;
width: 430px;
}
button {
margin: 0px auto 3px auto;
display: inline-block;
width: 211px;
border: 2px dotted grey;
font-size: 20px;
text-transform: uppercase;
font-weight: 500;
background: gold;
}
#playerTurnDisplay {
display: inline-block;
text-align: center;
width: 205px;
margin: 0px auto 20px auto;
}
.xTurn {
background: red;
border: 3px solid red;
}
.yTurn {
background: blue;
border: 3px solid blue;
}
table {
margin: auto;
}
td {
width: 205px;
height: 205px;
text-align: center;
}
.vertical {
border-left: 1px solid black;
border-right: 1px solid black;
}
.horizontal {
border-top: 1px solid black;
border-bottom: 1px solid black;
}
.circle {
font-size: 150px;
color: blue;
transition: all 0.1s;
--webkit-transition: all 0.1s;
--moz-transition: all 0.1s;
}
.cross {
font-size: 150px;
color: red;
transition: all 0.1s;
--webkit-transition: all 0.1s;
--moz-transition: all 0.1s;
}
.crossOut {
background: gold;
color: black;
transition: all 0.4s;
--webkit-transition: all 0.4s;
--moz-transition: all 0.4s;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TicTacToe</title>
<link rel="stylesheet" href="css/master.css" />
</head>
<body>
<h1>Tic Tac Toe</h1>
<div id="nav-bar">
<button id="resetButton">reset</button>
<h3 class="xTurn" id="playerTurnDisplay"><span id="player">X</span>'s turn</h3>
</div>
<table>
<tbody>
<tr>
<td><span id="top-left"></span>
</td>
<td class="vertical"><span id="top-mid"></span>
</td>
<td><span id="top-right"></span>
</td>
</tr>
<tr>
<td class="horizontal"><span id="mid-left"></span>
</td>
<td class="vertical horizontal"><span id="mid-mid"></span>
</td>
<td class="horizontal"><span id="mid-right"></span>
</td>
</tr>
<tr>
<td><span id="bottom-left"></span>
</td>
<td class="vertical"><span id="bottom-mid"></span>
</td>
<td><span id="bottom-right"></span>
</td>
</tr>
</tbody>
</table>
<script src="js/master.js" charset="utf-8"></script>
</body>
</html>