-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
135 lines (108 loc) · 3.2 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"use strict";
const playerFactory = (sign) => {
const _sign = sign;
const getSign = () => {
return _sign;
};
return { getSign };
};
const gameBoard = (function () {
const _boardArray = ["", "", "", "", "", "", "", "", ""];
const addToBoardArray = (sign, index) => {
_boardArray.splice(index, 1, sign);
};
const getBoardArray = () => {
return _boardArray;
};
const reset = () => {
for (let i = 0; i < _boardArray.length; i++) {
_boardArray[i] = "";
}
};
return { addToBoardArray, getBoardArray, reset };
})();
const displayController = (function () {
const board = document.querySelector(".board");
const resetBtn = document.querySelector(".reset-btn");
const winMsg = document.querySelector(".win-msg p");
const boardCells = document.querySelectorAll(".board-cell");
board.addEventListener("click", (e) => {
const cellIndex = parseInt(e.target.getAttribute("data-index"));
gameController.playRound(cellIndex);
});
resetBtn.addEventListener("click", () => {
gameController.reset();
displayController.reset();
gameBoard.reset();
});
const displaySignOnBoard = (sign, cellIndex) => {
document.querySelector(`[data-index="${cellIndex}"]`).textContent = sign;
};
const reset = () => {
boardCells.forEach((cell) => {
cell.textContent = "";
});
winMsg.parentElement.style.display = "";
};
const displayWinner = (playerSign) => {
winMsg.parentElement.style.display = "block";
winMsg.textContent = `Player ${playerSign} won`;
};
const displayDraw = () => {
winMsg.parentElement.style.display = "block";
winMsg.textContent = `Draw!`;
};
return { displaySignOnBoard, displayWinner, reset, displayDraw };
})();
const gameController = (function () {
const _PlayerOne = playerFactory("X");
const _PlayerTwo = playerFactory("O");
let _round = 1;
let _playerSign;
let _gameDone = false;
const playRound = (cellIndex) => {
if (gameBoard.getBoardArray()[cellIndex] || _gameDone) {
return;
} else {
if (_round % 2 === 0) {
_playerSign = _PlayerTwo.getSign();
} else {
_playerSign = _PlayerOne.getSign();
}
_round += 1;
gameBoard.addToBoardArray(_playerSign, cellIndex);
displayController.displaySignOnBoard(_playerSign, cellIndex);
if (_getWinner(cellIndex, _playerSign)) {
_gameDone = true;
displayController.displayWinner(_playerSign);
} else if (_round === 10) {
displayController.displayDraw();
}
}
};
const _getWinner = (cellIndex, playerSign) => {
const _winConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
//finds the combination possible for the player choose cell and returns true if all index are same sign
return _winConditions
.filter((combination) => combination.includes(cellIndex))
.some((possibleCombination) =>
possibleCombination.every(
(index) => gameBoard.getBoardArray()[index] === playerSign
)
);
};
const reset = () => {
_round = 1;
_gameDone = false;
};
return { playRound, reset, _round };
})();