I'm a beginner in OOP in JavaScript. I have programmed a Tic-Tac-Toe game. The program works, but I don't know if I followed the principles of OOP in this case. I know, that's functions in methods - that's wrong. How can I get rid of functions in methods? Thank you.
var ceil = document.getElementsByClassName("game-item"),
reset = document.getElementById("reset-game"),
message = document.getElementById("message"),
stepCount = 0,
winCombinations = [
[1, 2, 3],
[1, 4, 7],
[1, 5, 9],
[2, 5, 8],
[3, 6, 9],
[3, 5, 7],
[4, 5, 6],
[7, 8, 9]
],
dataX = [],
dataO = [];
class Player {
constructor(name) {
this.name = name;
}
}
class Game {
constructor() {
this.playerX = new Player("X");
this.playerO = new Player("O");
this.currentPlayer = this.playerX.name;
console.log(this.currentPlayer);
}
addX() {
for (var i = 0; i < ceil.length; i++) {
ceil[i].addEventListener("click", currentStep);
}
var z = this.currentPlayer;
function currentStep() {
var num = +this.getAttribute("data-ceil");
if (!this.textContent) {
this.innerText = z;
z === "X"
? dataX.push(num) && this.classList.add("x")
: dataO.push(num) && this.classList.add("o");
if (
(dataO.length > 2 || dataX.length > 2) &&
(checkWin(dataO, num) || checkWin(dataX, num))
) {
for (var i = 0; i < ceil.length; i++) {
ceil[i].removeEventListener("click", currentStep);
}
return (message.innerText = "Win player " + z);
}
changePlayer();
stepCount++;
(stepCount == 9) ? (message.innerText = 'Tie') : (message.innerText = 'The player ' + z);
}
}
function changePlayer() {
z === 'X' ? (z = "O") : (z = "X");
}
function checkWin(arr, number) {
for (var w = 0, wLen = winCombinations.length; w < wLen; w++) {
var someWinArr = winCombinations[w],
count = 0;
if (someWinArr.indexOf(number) !== -1) {
for (var k = 0, kLen = someWinArr.length; k < kLen; k++) {
if (arr.indexOf(someWinArr[k]) !== -1) {
count++;
if (count === 3) {
return true;
}
}
}
count = 0;
}
}
}
}
reset() {
reset.addEventListener("click", function() {
for (var i = 0; i < ceil.length; i++) {
ceil[i].innerText = "";
}
dataO = [];
dataX = [];
this.currentPlayer = "X";
stepCount = 0;
message.innerText = "The player " + this.currentPlayer;
for (var i = 0; i < ceil.length; i++) {
ceil[i].classList.remove("x", "o");
// ceil[i].addEventListener("click", currentStep);
}
});
}
}
const game = new Game();
game.addX();
game.reset();
* {
box-sizing: border-box;
outline: none;
}
.game-title {
display: block;
margin-bottom: 30px;
font-size: 35px;
font-weight: bold;
text-align: center;
}
.game {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
width: 152px;
margin: 0 auto 50px;
border: 1px solid #000;
}
.game-item {
position: relative;
width: 50px;
height: 50px;
line-height: 48px;
border: 1px solid #000;
cursor: pointer;
text-align: center;
font-size: 30px;
font-weight: bold;
transition: all linear 0.3s;
}
.game-item:hover {
background-color: #ccc;
}
.game-item.o {
background-color: #abfdab;
}
.game-item.x {
background-color: #f99;
}
#reset-game {
display: block;
margin: 0 auto;
border: 1px solid #000;
border-radius: 20px;
background-color: #fff;
padding: 10px 20px;
cursor: pointer;
transition: all linear 0.3s;
}
#reset-game:hover {
background-color: #ccc;
}
<span class="game-title" id="message">The player X</span>
<div class="game">
<div class="game-item" data-ceil="1"></div>
<div class="game-item" data-ceil="2"></div>
<div class="game-item" data-ceil="3"></div>
<div class="game-item" data-ceil="4"></div>
<div class="game-item" data-ceil="5"></div>
<div class="game-item" data-ceil="6"></div>
<div class="game-item" data-ceil="7"></div>
<div class="game-item" data-ceil="8"></div>
<div class="game-item" data-ceil="9"></div>
</div>
<button id="reset-game">Clear</button>