-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
165 lines (153 loc) · 4.46 KB
/
script.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const box = function (boxNode) {
const getBoxNode = () => boxNode;
let value = "";
const getValue = () => value;
const isEmpty = () => getValue() === "";
function makePassive() {
value = "p";
}
function fill(image, val) {
const img = document.createElement("img");
img.src = image;
getBoxNode().appendChild(img);
value = val;
}
function clean() {
if (getBoxNode().firstElementChild) {
getBoxNode().removeChild(getBoxNode().firstElementChild);
}
value = "";
}
return { getValue, fill, isEmpty, clean, makePassive };
};
const player = function (name, source) {
const img = source;
const getName = () => name;
const getImg = () => img;
return { name, getName, getImg };
};
const game = (function () {
const page = document.querySelector(".game-page");
const player1 = player("x", "./icons/x.png");
const player2 = player("o", "./icons/o.png");
let currentPlayer = player1;
let boxes = [];
const playButton = document.querySelector(".play-button");
const newGameButtons = Array.prototype.slice.call(
document.querySelectorAll(".new-game")
);
const welcomePage = document.querySelector(".welcome-page");
const closeModal = document.querySelector(".close");
const modal = document.querySelector(".game-end");
const boxObjs = [];
const winnerWindow = document.querySelector(".game-end");
const winnerIcon = document.querySelector(".game-end .winner-icon");
const winnerMessage = document.querySelector(".winner-message");
const turnImg = document.querySelector("div.turn>img");
turnImg.src = currentPlayer.getImg();
function changeCurrentPlayer() {
currentPlayer = currentPlayer === player1 ? player2 : player1;
turnImg.src = currentPlayer.getImg();
}
function endGame(winner) {
boxObjs.filter((obj) => obj.isEmpty()).map((obj) => obj.makePassive());
winnerWindow.style.display = "flex";
if (winner === "tie") {
winnerMessage.textContent = "IT'S A TIE!";
winnerIcon.style["background-image"] = 'url("./icons/banner.png")';
} else {
winnerMessage.textContent = "YOU WON!";
if (winner === "x") {
winnerIcon.style["background-image"] = `url("${player1.getImg()}")`;
} else {
winnerIcon.style["background-image"] = `url("${player2.getImg()}")`;
}
}
}
function isBoardFull() {
return !boxObjs.some((obj) => obj.isEmpty());
}
function didPlayerWin() {
const values = boxObjs.map((boxObj) => boxObj.getValue());
console.log(boxObjs[0].getValue());
const rows = [values.slice(0, 3), values.slice(3, 6), values.slice(6, 9)];
const columns = [
[values[0], values[3], values[6]],
[values[1], values[4], values[7]],
[values[2], values[5], values[8]],
];
const cross = [
[values[0], values[4], values[8]],
[values[2], values[4], values[6]],
];
if (
rows.some(
(row) =>
row.every((value) => value === "x") ||
row.every((value) => value === "o")
) ||
columns.some(
(column) =>
column.every((value) => value === "x") ||
column.every((value) => value === "o")
) ||
cross.some(
(line) =>
line.every((value) => value === "x") ||
line.every((value) => value === "o")
)
) {
return 1;
}
console.log(values);
return 0;
}
function checkGameOver() {
if (didPlayerWin()) {
endGame(currentPlayer.getName());
} else if (isBoardFull()) {
endGame("tie");
} else {
changeCurrentPlayer();
}
}
function createBoard() {
boxes = Array.prototype.slice.call(document.getElementsByClassName("box"));
boxes.forEach((node) => {
const boxObj = box(node);
boxObjs.push(boxObj);
node.addEventListener("click", () => {
if (boxObj.isEmpty()) {
boxObj.fill(currentPlayer.getImg(), currentPlayer.getName());
checkGameOver();
}
});
});
}
function displayBoard() {
createBoard();
page.style.display = "flex";
}
function resetGame() {
boxObjs.forEach((obj) => {
obj.clean();
});
}
function hide(item) {
item.style.display = "none";
}
playButton.addEventListener("click", () => {
displayBoard();
hide(welcomePage);
});
newGameButtons.map((button) =>
button.addEventListener("click", () => {
hide(modal);
resetGame();
})
);
closeModal.addEventListener("click", () => {
hide(modal);
});
return {};
})();