-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
139 lines (113 loc) · 2.61 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
//start button
let startButton = document.querySelector(".start");
startButton.addEventListener("click", () => {
startButton.parentNode.parentNode.style.display = "none";
startGame()
});
//restart button
let restartButton = document.querySelector(".restart");
restartButton.addEventListener("click", () => {
startGame()
popend.style.display ="none";
});
let popend = document.querySelector(".popup-end");
//game play
let chanceCount =0;
let x = "x";
let o = "o";
arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
let chance = false;
function play(event) {
chanceCount++;
let element = event.target;
if (element.clicked) return;
element.clicked = true;
if (chance) {
element.textContent = x;
arr[element.i][element.j] = x;
} else {
element.textContent = o;
arr[element.i][element.j] = o;
}
chance = !chance;
console.log(check(arr));
if (check(arr)) {
popend.style.display = "flex";
let resultDiv = document.getElementById("result");
if (chance) resultDiv.textContent = "YAYY! 'O' WON THE GAME";
else resultDiv.textContent = "YAYY! 'X' WON THE GAME";
}
else
{
if(chanceCount ==9)
{
let resultDiv = document.getElementById("result");
resultDiv.textContent = "OHH! IT'S A TIE";
popend.style.display = "flex";
}
}
}
let container = document.querySelector(".container");
function startGame() {
chanceCount=0;
container.innerHTML =""
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
let div = document.createElement("div");
container.append(div);
div.i = i;
div.j = j;
div.clicked = false;
div.addEventListener("click", play);
}
}
arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
}
function check(arr) {
let same = true;
let char = null;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i == j) {
if (char == null) {
char = arr[i][j];
continue;
}
if (arr[i][j] !== char) {
same = false;
}
}
}
}
if (same) return true;
(same = true), (char = null);
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i + j == 2) {
if (char == null) {
char = arr[i][j];
continue;
}
if (arr[i][j] !== char) {
same = false;
}
}
}
}
if (same) return true;
for (let i = 0; i < 3; i++) {
if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2]) return true;
}
for (let j = 0; j < 3; j++) {
if (arr[0][j] == arr[1][j] && arr[1][j] == arr[2][j]) return true;
}
return false;
}