-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayers.js
109 lines (104 loc) · 4.57 KB
/
players.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
let grid=[
[0,0,0],
[0,0,0],
[0,0,0]
];
let who=0;
function delay(time) {
return new Promise(resolve => setTimeout(resolve,time));
}
start();
async function start() {
let choose=Math.floor(Math.random()*2);
if (choose===0) {
document.getElementById('start').innerHTML='X first'
who=1;
} else {
document.getElementById('start').innerHTML='O first'
who=2;
}
}
function xo(line,colomn) {
if (who===2) {
O(line,colomn);
} else if (who===1) {
X(line,colomn);
}
}
function O(line,colomn) {
if (grid[line][colomn]===0&&!result()) {
document.getElementById('start').innerHTML='X turn'
document.getElementById(`${line}${colomn}`).innerHTML=`<img class="game" src="o.png">`;
grid[line][colomn]=1;
who=1;
result();
}
}
function X(line,colomn) {
if (grid[line][colomn]===0&&!result()) {
document.getElementById('start').innerHTML='O turn';
document.getElementById(`${line}${colomn}`).innerHTML=`<img class="game" src="x.png">`;
grid[line][colomn]=2;
who=2;
result();
}
}
function result() {
let p=document.getElementById('sd');
let l1=grid[0][0]===grid[0][1]&&grid[0][1]===grid[0][2];
let l2=grid[1][0]===grid[1][1]&&grid[1][1]===grid[1][2];
let l3=grid[2][0]===grid[2][1]&&grid[2][1]===grid[2][2];
let c1=grid[0][0]===grid[1][0]&&grid[1][0]===grid[2][0];
let c2=grid[0][1]===grid[1][1]&&grid[1][1]===grid[2][1];
let c3=grid[0][2]===grid[1][2]&&grid[1][2]===grid[2][2];
let d1=grid[0][0]===grid[1][1]&&grid[1][1]===grid[2][2];
let d2=grid[2][0]===grid[1][1]&&grid[1][1]===grid[0][2];
if (l1&&grid[0][0]!==0){
document.getElementById('start').innerHTML='<br>';
grid[0][0]===1 ? p.innerHTML=`<p id="score">O won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>` : p.innerHTML=`<p id="score">X won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>`;
return true;
} else if (l2&&grid[1][0]!==0) {
document.getElementById('start').innerHTML='<br>';
grid[1][0]===1 ? p.innerHTML=`<p id="score">O won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>` : p.innerHTML=`<p id="score">X won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>`;
return true;
}else if (l3&&grid[2][0]!==0) {
document.getElementById('start').innerHTML='<br>';
grid[2][0]===1 ? p.innerHTML=`<p id="score">O won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>` : p.innerHTML=`<p id="score">X won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>`;
return true;
} else if (c1&&grid[0][0]!==0) {
document.getElementById('start').innerHTML='<br>';
grid[0][0]===1 ? p.innerHTML=`<p id="score">O won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>` : p.innerHTML=`<p id="score">X won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>`;
return true;
} else if (c2&&grid[0][1]!==0) {
document.getElementById('start').innerHTML='<br>';
grid[0][1]===1 ? p.innerHTML=`<p id="score">O won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>` : p.innerHTML=`<p id="score">X won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>`;
return true;
} else if (c3&&grid[0][2]!==0) {
document.getElementById('start').innerHTML='<br>';
grid[0][2]===1 ? p.innerHTML=`<p id="score">O won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>` : p.innerHTML=`<p id="score">X won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>`;
return true;
}else if ((d1&&grid[1][1]!==0)||(d2&&grid[1][1]!==0)) {
document.getElementById('start').innerHTML='<br>';
grid[1][1]===1 ? p.innerHTML=`<p id="score">O won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>` : p.innerHTML=`<p id="score">X won</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>`;
return true;
} else if (grid.every(row => row.every(cell => cell !== 0))) {
document.getElementById('start').innerHTML='<br>';
p.innerHTML = `<p id="score">It's a tie!</p>
<button id="retry"><a href="players.html"><img id="arrow" src="refresh.png"></a></button>`;
return true;
} else return false;
}