-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
140 lines (137 loc) · 5.3 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
let selectObtn = document.querySelector(".playerO");
let selectXbtn = document.querySelector(".playerX");
let playBoard = document.querySelector(".play-board");
let welcome = document.querySelector(".welcome");
let players = document.querySelector(".players");
let allBox = document.querySelectorAll("section div");
let resultBox = document.querySelector(".result-box");
let wonText = document.querySelector(".won-text");
let replayBtn = document.getElementById("replay");
window.onload = () => {
for (let i = 0; i < allBox.length; i++) {
//add onclick attribute in all available span
allBox[i].setAttribute("onclick", "clickedBox(this)");
}
selectXbtn.addEventListener("click", function () {
playBoard.classList.add("show");
welcome.classList.add("hide");
});
selectObtn.addEventListener("click", function () {
playBoard.classList.add("show");
welcome.classList.add("hide");
players.setAttribute("class", "players active player");
});
};
let xicon = "fas fa-times";
let oicon = "far fa-circle";
let psign = "x";
let runBot = true;
function clickedBox(element) {
if (players.classList.contains("player")) {
sign = "o";
element.innerHTML = `<i class="${oicon}"></i>`;
players.classList.add("active");
element.setAttribute("id", "o");
} else {
element.innerHTML = `<i class="${xicon}"></i>`;
players.classList.add("active");
element.setAttribute("id", "x");
}
selectWinner();
element.style.pointerEvents = "none";
let randomTimeDelay = (Math.random() * 1000 + 200).toFixed(); //generating random number so bot will randomly delay to select unselected box
setTimeout(() => {
bot(); //calling bot function
}, randomTimeDelay); //passing random delay value
}
function bot() {
let arr = [];
if (runBot) {
for (let i = 0; i < allBox.length; i++) {
if (allBox[i].childElementCount == 0) {
arr.push(i);
}
}
let randomNumber = arr[Math.floor(Math.random() * arr.length)];
if (arr.length > 0) {
if (players.classList.contains("player")) {
allBox[randomNumber].setAttribute("id", "x");
allBox[randomNumber].innerHTML = `<i class="${xicon}"></i>`;
players.classList.remove("active");
} else {
allBox[randomNumber].innerHTML = `<i class="${oicon}"></i>`;
psign = "o";
players.classList.remove("active");
allBox[randomNumber].setAttribute("id", "o");
}
selectWinner();
}
allBox[randomNumber].style.pointerEvents = "none";
playBoard.style.pointerEvents = "auto"; //add pointerEvents auto in playboard so user can again click on box
psign = "x";
}
}
function getIdVal(classname) {
return document.querySelector(".box" + classname).id; //return id value
}
function checkIdSign(val1, val2, val3, sign) {
//checking all id value is equal to sign (X or O) or not if yes then return true
if (getIdVal(val1) == sign && getIdVal(val2) == sign && getIdVal(val3) == sign) {
return true;
}
}
function selectWinner() {
//if the one of following winning combination match then select the winner
if (
checkIdSign(1, 2, 3, "x") ||
checkIdSign(4, 5, 6, "x") ||
checkIdSign(7, 8, 9, "x") ||
checkIdSign(1, 4, 7, "x") ||
checkIdSign(2, 5, 8, "x") ||
checkIdSign(3, 6, 9, "x") ||
checkIdSign(1, 5, 9, "x") ||
checkIdSign(3, 5, 7, "x")
) {
runBot = false; //passing the false boolen value to runBot so bot won't run again
bot(); //calling bot function
setTimeout(() => {
//after match won by someone then hide the playboard and show the result box after 700ms
resultBox.classList.add("show");
playBoard.classList.remove("show");
}, 700); //1s = 1000ms
wonText.innerHTML = `Player <p>x</p> won the game!`; //displaying winning text with passing playerSign (X or O)
} else if (
checkIdSign(1, 2, 3, "o") ||
checkIdSign(4, 5, 6, "o") ||
checkIdSign(7, 8, 9, "o") ||
checkIdSign(1, 4, 7, "o") ||
checkIdSign(2, 5, 8, "o") ||
checkIdSign(3, 6, 9, "o") ||
checkIdSign(1, 5, 9, "o") ||
checkIdSign(3, 5, 7, "o")
) {
runBot = false; //passing the false boolen value to runBot so bot won't run again
bot(); //calling bot function
setTimeout(() => {
//after match won by someone then hide the playboard and show the result box after 700ms
resultBox.classList.add("show");
playBoard.classList.remove("show");
}, 700); //1s = 1000ms
wonText.innerHTML = `Player <p>o</p> won the game!`; //displaying winning text with passing playerSign (X or O)
} else {
//if all boxes/element have id value and still no one win then draw the match
if (getIdVal(1) != "" && getIdVal(2) != "" && getIdVal(3) != "" && getIdVal(4) != "" && getIdVal(5) != "" && getIdVal(6) != "" && getIdVal(7) != "" && getIdVal(8) != "" && getIdVal(9) != "") {
runBot = false; //passing the false boolen value to runBot so bot won't run again
bot(); //calling bot function
setTimeout(() => {
//after match drawn then hide the playboard and show the result box after 700ms
resultBox.classList.add("show");
playBoard.classList.remove("show");
}, 700); //1s = 1000ms
wonText.textContent = "Match has been drawn!"; //displaying draw match text
}
}
}
replayBtn.onclick = () => {
window.location.reload(); //reload the current page on replay button click
};