Skip to content

Commit 72b960c

Browse files
Create javascript.js
1 parent 03fad49 commit 72b960c

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed

Tic Tac Toe/javascript.js

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// Created by THE YOUNG PROGRAMMER aka NEMONET
2+
3+
let scores = {
4+
x: 0,
5+
o: 0
6+
};
7+
8+
function startGame (pl) {
9+
let player = pl;
10+
const elm = document.querySelectorAll(".box");
11+
const finePositions = getFinePositions();
12+
const position = finePositions[Math.floor(Math.random() * (finePositions.length - 0) + 0)];
13+
14+
if (!pl) {
15+
player = Math.floor(Math.random() * (3 - 1) + 1);
16+
switch (player) {
17+
case 1:
18+
player = "O";
19+
break;
20+
case 2:
21+
player = "X";
22+
break;
23+
}
24+
}
25+
26+
if (getResult()) {
27+
return 0;
28+
}
29+
30+
if (finePositions.length == 9) {
31+
swal(`Current Player is ${player}`,`${(()=>{
32+
if (player == "X") {
33+
return "Tap any box to start ...";
34+
}
35+
return "First is Device ,\n then tap any box for next move ...";
36+
})()}`);
37+
}
38+
if (player == "O") {
39+
const el = elm[position];
40+
el.setAttribute("o", "");
41+
startGame("X");
42+
}
43+
else {
44+
document.getElementById("playero").setAttribute("nplayer","");
45+
document.getElementById("playerx").removeAttribute("nplayer");
46+
finePositions.forEach((index)=>{
47+
elm[index].addEventListener("click",addX);
48+
});
49+
}
50+
}
51+
52+
function getResult () {
53+
let x = 0;
54+
let o = 0;
55+
let i = 0;
56+
let winner = "";
57+
let scoreArr = [];
58+
const boxes = document.querySelectorAll(".box");
59+
boxes.forEach((el)=> {
60+
if (el.hasAttribute("x")) {
61+
scoreArr.push("X");
62+
}
63+
else if (el.hasAttribute("o")) {
64+
scoreArr.push("O");
65+
}
66+
else {
67+
scoreArr.push(i++);
68+
}
69+
});
70+
const isDone = checkResult(scoreArr);
71+
if (isDone[0]) {
72+
scores[isDone[1].toLowerCase()]++;
73+
updateScores();
74+
boxes.forEach((el ,index)=>{
75+
el.removeEventListener("click",addX);
76+
if (index == isDone[2][0] || index == isDone[2][1] || index == isDone[2][2]) {
77+
return 0;
78+
}
79+
el.setAttribute("out","");
80+
});
81+
swal({
82+
title: `Player ${isDone[1]} Won !`,
83+
text: "do you want to restart the game ?",
84+
icon: "success",
85+
buttons: true
86+
})
87+
.then((willDelete) => {
88+
boxes.forEach((el)=>{
89+
el.removeAttribute("x");
90+
el.removeAttribute("o");
91+
el.removeAttribute("out");
92+
});
93+
if (willDelete) {
94+
startGame();
95+
} else {
96+
const playbox = document.getElementById("playbox");
97+
document.getElementById("playerx").removeAttribute("nplayer");
98+
document.getElementById("playero").removeAttribute("nplayer");
99+
100+
playbox.style.display = "flex";
101+
swal("Tap play Button to Start !");
102+
}
103+
});
104+
return true;
105+
}
106+
}
107+
108+
function checkResult (res) {
109+
const req = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];
110+
let status = false;
111+
let pl = "no one";
112+
let correct = null;
113+
req.forEach((i)=>{
114+
if (!status) {
115+
if (res[i[0]] == "X" && res[i[1]] == "X" && res[i[2]] == "X" ) {
116+
status = true;
117+
pl = "X";
118+
correct = i;
119+
}
120+
else if (res[i[0]] == "O" && res[i[1]] == "O" && res[i[2]] == "O" ){
121+
status = true;
122+
pl = "O";
123+
correct = i;
124+
}
125+
else if (getFinePositions(). length == 0) {
126+
status = true;
127+
pl = "No one";
128+
correct = [9,9,9];
129+
}
130+
}
131+
return 0;
132+
});
133+
return [status , pl ,correct] ;
134+
}
135+
136+
function addX (el) {
137+
const elm = document.querySelectorAll(".box");
138+
el.srcElement.setAttribute("x","");
139+
elm.forEach((el)=>{
140+
el.removeEventListener("click",addX);
141+
});
142+
document.getElementById("playerx").setAttribute("nplayer","");
143+
document.getElementById("playero").removeAttribute("nplayer");
144+
setTimeout(function() {
145+
startGame("O");
146+
}, 1000);
147+
}
148+
149+
function updateScores () {
150+
const xbox = document.getElementById("playerx");
151+
const obox = document.getElementById("playero");
152+
153+
xbox.innerHTML = `<b>Player X</b> : ${scores.x}`;
154+
obox.innerHTML = `<b>Player O</b> : ${scores.o}`;
155+
}
156+
157+
function getFinePositions () {
158+
const boxes = document.querySelectorAll(".box");
159+
let finePositions = [];
160+
161+
boxes.forEach((el, index) => {
162+
if (el.hasAttribute("x") || el.hasAttribute("o")) {} else {
163+
finePositions.push(index);
164+
}
165+
});
166+
return finePositions;
167+
}
168+
169+
window.onload = () => {
170+
swal("Tap play Button to start !");
171+
document.getElementById("playbtn").onclick = (el) => {
172+
setTimeout(function() {
173+
const data = document.getElementById("data");
174+
175+
data.style.display = "flex";
176+
el.target.parentNode.style.display = "none";
177+
startGame();
178+
}, 300);
179+
};
180+
};

0 commit comments

Comments
 (0)