-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.js
80 lines (61 loc) · 1.86 KB
/
project.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
var player = 1;
var win = false;
var squares = document.getElementsByClassName("square");
Array.from(squares).forEach((square) => {
square.addEventListener("click", turn);
});
document.getElementById("turn").innerText ="players 1's turn" ; // Added `document.`
function turn(props) {
const square = props.target;
if(square.innerText !=""){
alert("Please enter a valid move");
}
else if (player == 1) { // Fixed typo in `player`
square.innerText = "X";
square.style.fontSize = "50px";
square.style.textAlign = "center";
setTimeout(() =>{
checkWin();
player = 2;
document.getElementById("turn").innerText ="players 2's turn" ; // Added `document.`
}, 50);
}
else{
square.innerText = "O";
square.style.fontSize = "50px";
square.style.textAlign = "center";
setTimeout(() =>{
checkWin();
player = 1;
document.getElementById("turn").innerText ="players 1's turn" ; // Added `document.`
}, 50);
}
}
function checkWin() {
let wincombo=[
[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]
]
wincombo.map(win=>
{
let win0=document.getElementById("square"+win[0]).innerText;
let win1=document.getElementById("square"+win[1]).innerText;
let win2=document.getElementById("square"+win[2]).innerText;
if(win0==win1 && win1==win2 && win0!="")
{
alert("Player "+player+" wins");
reset();
}
}
)
let allFilled = Array.from(squares).every(square => square.innerText !== "");
if (allFilled) {
alert("It's a draw!");
reset();
}
}
function reset() {
Array.from(squares).forEach((square) => {
square.innerText = "";
});
player = 1;
}