-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (59 loc) · 1.71 KB
/
Copy pathscript.js
File metadata and controls
66 lines (59 loc) · 1.71 KB
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
let svgLinkOfX = "./asset/x.svg";
let svgLinkOfO = "./asset/o.svg";
let count = 0;
let currentPlayer = svgLinkOfX;
let arr = [];
function checkForWinner() {
if (count > 3) {
// Row - Wise Checking
if (arr[0] === arr[1] && arr[1] === arr[2]) {
return arr[0];
}
if (arr[3] === arr[4] && arr[4] === arr[5]) {
return arr[3];
}
if (arr[6] === arr[7] && arr[7] === arr[8]) {
return arr[6];
}
// columns - wise checking
if (arr[0] === arr[3] && arr[3] === arr[6]) {
return arr[0];
}
if (arr[1] === arr[4] && arr[4] === arr[7]) {
return arr[1];
}
if (arr[2] === arr[5] && arr[5] === arr[8]) {
return arr[2];
}
//Diagnol checking
if (arr[0] === arr[4] && arr[4] === arr[8]) {
return arr[0];
}
if (arr[2] === arr[4] && arr[4] === arr[6]) {
return arr[2];
}
}
}
let gettingAllTheBoxes = document.querySelector(".content");
gettingAllTheBoxes.addEventListener("click", (e) => {
let elemnt = e.target;
let el = document.getElementById(elemnt.id);
if (el.innerHTML === "") {
el.innerHTML = `<img src = ${currentPlayer} />`;
count++;
if (currentPlayer === svgLinkOfX) {
el.value = "X";
currentPlayer = svgLinkOfO;
} else {
el.value = "O";
currentPlayer = svgLinkOfX;
}
arr[el.id.slice(-1) - 1] = el.value;
}
let winner = checkForWinner();
if (winner) {
alert(`is the winner : ${winner}`);
} else if (!winner && count === 9) {
alert(`It is a draw`);
}
});