-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
150 lines (135 loc) · 4.08 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
const playingField = document.querySelector('.playing-field');
const boxes = Array.from(document.querySelectorAll('.box'));
const newGameBtn = document.querySelector('.new-game-btn');
let scoreX = document.querySelector('.score-x');
let scoreO = document.querySelector('.score-o');
let scoreCountO = 0;
let scoreCountX = 0;
let move = 0;
let result = '';
let winner = document.querySelector('.winner');
const winningCombo = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
const audio = new Audio()
// Проверка победителя
function checkWinner() {
for (i = 0; i < winningCombo.length; i++){
let a = boxes[winningCombo[i][0]];
let b = boxes[winningCombo[i][1]];
let c = boxes[winningCombo[i][2]];
if (a.innerHTML == 'X' && b.innerHTML == 'X' && c.innerHTML == 'X'){
result = 1;
showResult(result);
scoreUpdate(result);
a.style.color = b.style.color = c.style.color = 'brown';
break
} else if (a.innerHTML == 'O' && b.innerHTML == 'O' && c.innerHTML == 'O'){
result = 2;
showResult(result);
scoreUpdate(result);
a.style.color = b.style.color = c.style.color = 'brown';
break
} else if (move === 9){
result = 3;
showResult(result);
};
};
}
// при клике ставит крестик или нолик
function makeMove(e) {
if (e.target.classList.contains('box') && e.target.innerHTML === '') {
if (move < 9 && move % 2 === 0) {
e.target.innerHTML = 'X';
} else if (move < 9 && move % 2 !== 0){
e.target.innerHTML = 'O';
};
move = move + 1;
};
checkWinner();
}
// звук при клике
function playSound() {
audio.src = `./sound.mp3`;
audio.currentTime = 0;
audio.play();
}
// обновление счета
function scoreUpdate(player){
if (player === 1){
scoreCountX = scoreCountX * 1 + 1;
scoreX.innerHTML = `: ${scoreCountX}`;
localStorage.setItem('scoreX', scoreCountX);
} else if (player === 2){
scoreCountO = scoreCountO * 1 + 1;
scoreO.innerHTML = `: ${scoreCountO}`;
localStorage.setItem('scoreO', scoreCountO);
}
}
function removePointerEvents(){
playingField.style.pointerEvents = "none";
}
function returnPointerEvents(){
playingField.style.pointerEvents = "auto";
}
// отображение победителя
function showResult(victor){
if (victor === 1) {
winner.innerText = `The winner is Player-X!
Game won in ${move} moves.
`;
} else if (victor === 2){
winner.innerText = `The winner is Player-O!
Game won in ${move} moves.
`;
} else if (victor === 3) {
winner.innerText = `It's a draw! It was
${move} moves before a draw.
`;
}
removePointerEvents();
}
// новая игра
function newGame(){
boxes.forEach(el => {
el.innerHTML = '';
el.style.color = 'aliceblue';
});
returnPointerEvents();
winner.innerHTML = '';
move = 0;
}
// local storage
function scoreUpdateRefresh1(score1) {
scoreX.innerHTML = `: ${score1}`;
}
function scoreUpdateRefresh2(score2) {
scoreO.innerHTML = `: ${score2}`;
}
function getLocalStorage1() {
if(localStorage.getItem('scoreX')) {
scoreCountX = localStorage.getItem('scoreX');
scoreUpdateRefresh1(scoreCountX);
};
}
function getLocalStorage2() {
if (localStorage.getItem('scoreO')) {
scoreCountO = localStorage.getItem('scoreO');
scoreUpdateRefresh2(scoreCountO)
}
}
// клик по ячейке
playingField.addEventListener('click', makeMove);
playingField.addEventListener('click', playSound);
// клик по кнопке новая игра
newGameBtn.addEventListener('click', newGame);
// get local storage при обновлении страницы
window.addEventListener('load', getLocalStorage1);
window.addEventListener('load', getLocalStorage2);