-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
53 lines (48 loc) · 1.61 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
var currentPlayer = 'X';
var boxes = document.querySelectorAll('.box');
var player = document.getElementById("player");
var winner = document.getElementById("winner");
boxes.forEach(box => box.addEventListener('click',playerClick));
function playerClick(event) {
if (event.target.innerText != '' ||winner.innerText != ''){
return;
}
event.target.innerText = currentPlayer;
if (isWinner()) {
winner.innerText = "Player " + currentPlayer + " WINS!!";
}
else{
currentPlayer = (currentPlayer =='X') ? 'O': 'X';
player.innerText = "Current Player: " + currentPlayer;
}
if (isDraw()) {
winner.innerText = "Game is DRAWN!!"
}
}
var reset = document.getElementById("reset");
reset.addEventListener("click", clear);
function clear(){
winner.innerText = "";
currentPlayer = 'X';
player.innerText = "Current player: " + currentPlayer;
boxes.forEach(box => box.innerText="");
}
const winPositions = ['012', '345', '678', '036', '147', '258', '048','246'];
function isWinner() {
for(let position of winPositions) {
let position1 = boxes[position[0]].innerText;
let position2 = boxes[position[1]].innerText;
let position3 = boxes[position[2]].innerText;
if (position1 == currentPlayer && position2 == currentPlayer && position3 == currentPlayer){
return true;
}
}
return false;
}
function isDraw() {
let draw = true;
for (let box of boxes){
draw = draw && box.innerText != '';
}
return draw;
}