-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (54 loc) · 1.68 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
// console.log('hello');
// x or 0 is going
// change x and 0
// check if user won
let player= 'X';
let gameOver = false;
const changePlayer= () => {
return player === 'X' ? "O" : "X"; //change player
}
const checkwon =() =>{
let boxTexts = document.getElementsByClassName("boxText");
let winPos = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6],
];
winPos.forEach(ele => {
if(boxTexts[ele[0]].innerText === boxTexts[ele[1]].innerText &&
boxTexts[ele[1]].innerText === boxTexts[ele[2]].innerText &&
boxTexts[ele[0]].innerText !==""){
document.getElementById("result").innerText = boxTexts[ele[0]].innerText + " won";
window.alert(boxTexts[ele[0]].innerText+ ' Won');
gameOver=true;
document.querySelector("img").style.width="100px";
}
});
};
let boxes = document.getElementsByClassName("box");
Array.from(boxes).forEach((box) => {//converts to array
let boxText=box.querySelector(".boxText")
box.addEventListener("click",()=> {
if(boxText.innerText === "" && !gameOver){
boxText.innerHTML= player;
player = changePlayer(); // change from X to O
checkwon();
document.getElementsByClassName("player")[0].innerHTML = player;
}
});
});
//reset button function
let reset = document.getElementsByClassName("reset")[0];
reset.addEventListener("click",()=> {
let boxTexts = document.querySelectorAll(".boxText");
boxTexts.forEach(boxText => {
boxText.innerHTML = "";
});
player = "X";
gameOver = false;
});