-
Notifications
You must be signed in to change notification settings - Fork 0
/
xo.js
52 lines (52 loc) · 2.39 KB
/
xo.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
let turn="x";
let reset=document.querySelector(".reset");
reset.addEventListener("click",function(){
location.reload();
})
function winning() {
let choices=[];
for(let i=0;i<10;i++){
choices[i]=document.getElementById('item'+i)
}
if (choices[1].innerText==choices[2].innerText&&choices[2].innerText==choices[3].innerText&&choices[1].innerText!="") {
winner(choices[1].innerText.toString());
}else if (choices[4].innerText==choices[5].innerText&&choices[5].innerText==choices[6].innerText&&choices[6].innerText!="") {
winner(choices[4].innerText.toString())
}else if (choices[7].innerText==choices[8].innerText&&choices[8].innerText==choices[9].innerText&&choices[8].innerText!="") {
winner(choices[7].innerText)
}else if (choices[1].innerText==choices[4].innerText&&choices[4].innerText==choices[7].innerText&&choices[7].innerText!="") {
winner(choices[1].innerText)
}else if (choices[2].innerText==choices[5].innerText&&choices[5].innerText==choices[8].innerText&&choices[8].innerText!="") {
winner(choices[2].innerText)
}else if (choices[3].innerText==choices[6].innerText&&choices[6].innerText==choices[9].innerText&&choices[9].innerText!="") {
winner(choices[3].innerText)
}else if (choices[1].innerText==choices[5].innerText&&choices[5].innerText==choices[9].innerText&&choices[9].innerText!="") {
winner(choices[1].innerText)
}else if (choices[3].innerText==choices[5].innerText&&choices[5].innerText==choices[7].innerText&&choices[7].innerText!="") {
winner(choices[3].innerText)
}
}
function Gamepad(id) {
let element=document.getElementById(id);
if (turn=="x" && element.innerHTML=="") {
let tag=document.createElement("span")
element.appendChild(tag);
let insert=document.createTextNode("x");
turn="o";
tag.appendChild(insert);
}else if(turn=="o" && element.innerText==""){
tag=document.createElement("span");
element.appendChild(tag);
insert=document.createTextNode("o");
turn="x";
tag.appendChild(insert);
}
winning()
}
function winner(win){
let stuff=document.querySelector(".display");
let teg=document.createElement("span");
let text=document.createTextNode(win+" is the winner ")
teg.appendChild(text)
stuff.appendChild(teg);
}