-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
215 lines (182 loc) · 6.22 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"use strict";
const modal = document.querySelector(".modal");
const ContainerEl = document.querySelector(".container");
let playerTxt = document.querySelector(".message");
let restartBtn = document.querySelector("#restartbtn");
let boxes = document.querySelectorAll(".box");
let para = document.querySelector(".content p");
const start = document.querySelector(".start-game");
const startbtn = document.querySelector(".startbtn");
const gameboard = document.getElementById("gameboard");
const player = document.querySelector(".players");
const undo = document.querySelector(".undo");
const undoX = document.getElementById("undoX");
const undoO = document.getElementById("undoO");
var x = document.getElementById("x");
var o = document.getElementById("o");
var c1 = 0;
var c2 = 0;
var games_palyed = 0;
var total_games = 0;
var stack = [];
const O_TXT = "O";
const X_TXT = "X";
let currentPlayer = X_TXT;
var spaces = Array(9).fill(null);
let winnerindicator = getComputedStyle(document.body).getPropertyValue('--dark-color');
var player1 = "";
var player2 = "";
startbtn.addEventListener("click", function () {
player1 = document.querySelector("#player1").value;
player2 = document.querySelector("#player2").value;
total_games = document.querySelector("#numberofgames").value;
if (player1 == "" || player2 == "") {
alert("Please enter your name");
} else {
document.querySelector("#player1").innerHTML = player1;
document.querySelector("#player2").innerHTML = player2;
}
if (total_games > 0) {
start.style.display = "none";
gameboard.style.display = "flex";
player.style.display = "flex";
} else if (total_games === "") {
alert("Please enter number of games");
} else if (total_games <= 0) {
alert("Tu thoda sa....ganda hai kya!");
}
player.innerHTML = `<div class="p1">
<h1>${player1}</h1>
<h2>${c1}</h2>
</div>
<div class="p2">
<h1>${player2}</h1>
<h2>${c2}</h2>
</div>`;
});
const startGame = () => {
boxes.forEach((boxs) => boxs.addEventListener("click", boxClicked));
};
function boxClicked(e) {
const id = e.target.id;
if (spaces[id] == null) {
if (!playerHasWon()) {
spaces[id] = currentPlayer;
e.target.innerText = currentPlayer;
stack.push(id);
}
if (playerHasWon() != false) {
winnerindicator = playerHasWon();
winnerindicator.map((box) => (boxes[box].style.backgroundColor = "#f5f5f5"));
if (currentPlayer == 'X') c1++;
else if (currentPlayer == 'O') c2++;
modal.classList.remove('modal');
modal.classList.add('success');
player.innerHTML = `<div class="p1">
<h1>${player1}</h1>
<h2>${c1}</h2>
</div>
<div class="p2">
<h1>${player2}</h1>
<h2>${c2}</h2>
</div>`;
}
if (Draw() == true) {
playerTxt.innerHTML = `<h2 class="message">Draw !</h2>`;
para.innerText = "No one wins";
modal.classList.remove('modal');
modal.classList.add('success');
Draw() = false;
}
if (playerHasWon() || Draw()) {
games_palyed++;
console.log("Games Played: ", games_palyed, "of", total_games);
if (games_palyed == total_games) {
if (c1 > c2) {
playerTxt.innerHTML = `<h2 class="message">Congratulations ${player1}</h2>`;
para.innerText = `You won the game ;) `;
} else if (c1 < c2) {
playerTxt.innerHTML = `<h2 class="message">Congratulations ${player2}</h2>`;
para.innerText = `You won the game ;) `;
} else {
playerTxt.innerHTML = `<h2 class="message">Draw !</h2>`;
para.innerText = "No one wins";
}
modal.classList.remove('modal');
modal.classList.add('success');
ContainerEl.classList.remove('success');
restartBtn.innerText = "Play Again";
}
}
currentPlayer = currentPlayer === X_TXT ? O_TXT : X_TXT;
if (playerHasWon()) currentPlayer = null;
} else {
alert("Cell already taken by " + spaces[id]);
}
}
const winningCombos = [
[0, 1, 2],
[0, 3, 6],
[0, 4, 8],
[1, 4, 7],
[2, 4, 6],
[2, 5, 8],
[3, 4, 5],
[6, 7, 8]
];
function playerHasWon() {
for (let condition of winningCombos) {
let [a, b, c] = condition;
if (spaces[a] && spaces[a] === spaces[b] && spaces[a] === spaces[c]) {
return [a, b, c];
}
}
return false;
}
function Draw() {
for (var i = 0; i < 9; i++) {
if (spaces[i] == null) {
return false;
}
}
if (playerHasWon() == false) {
return true;
}
return false;
}
undo.style.display = 'none';
undo.addEventListener('click', function () {
if (stack.length > 0) {
if (currentPlayer === 'X') {
var delid = stack.pop();
spaces[delid] = null;
currentPlayer = 'O';
undoX.style.display = "none";
x.className = "fa-rotate-left";
} else {
var delid = stack.pop();
document.getElementById(delid).innerHTML = "";
spaces[delid] = null;
currentPlayer = 'X';
undoO.style.display = "none";
o.className = "fa-rotate-left";
}
}
});
restartbtn.addEventListener("click", restartGame);
function restartGame() {
if (games_palyed - total_games == 0) {
window.location.reload();
return;
}
spaces.fill(null);
boxes.forEach((box) => {
box.innerHTML = "";
box.style.backgroundColor = "";
});
currentPlayer = X_TXT;
modal.classList.remove('success');
modal.classList.add('modal');
ContainerEl.classList.remove('success');
}
startGame();