Skip to content

Commit fe95a93

Browse files
Merge pull request #14 from sushanttwayana/main
TIC TAC TOE GAME
2 parents 361c43e + 32613fb commit fe95a93

3 files changed

Lines changed: 308 additions & 0 deletions

File tree

‎tic-tac-toe/index.html‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Tic Tac Toe</title>
8+
9+
<link rel="stylesheet" href="./style.css">
10+
11+
<script src="./script.js" defer> </script>
12+
</head>
13+
<body>
14+
15+
<div class="board " id="board">
16+
17+
<div class="cell " data-cell></div>
18+
<div class="cell " data-cell></div>
19+
<div class="cell" data-cell></div>
20+
<div class="cell" data-cell></div>
21+
<div class="cell" data-cell></div>
22+
<div class="cell" data-cell></div>
23+
<div class="cell" data-cell></div>
24+
<div class="cell" data-cell></div>
25+
<div class="cell" data-cell></div>
26+
27+
28+
<div class="winning-msg " id="winning-msg">
29+
<div id="displaymsg"></div>
30+
31+
<button id="restartBtn">Restart</button>
32+
</div>
33+
34+
</div>
35+
</body>
36+
</html>

‎tic-tac-toe/script.js‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
const cellElements = document.querySelectorAll('[data-cell]');
2+
const board = document.getElementById("board");
3+
const winningMessageDisplay = document.getElementById("winning-msg");
4+
const winningMessageText = document.getElementById("displaymsg");
5+
const restatBtn = document.getElementById("restartBtn");
6+
7+
const X_CLASS = "x";
8+
const CIRCLE_CLASS ="circle";
9+
let circleTurn = "" ; //undefined halema 0 first
10+
const WINNING_COMBINATIONS = [
11+
[0, 1, 2],
12+
[3, 4, 5],
13+
[6, 7, 8],
14+
[0, 3, 6],
15+
[1, 4, 7],
16+
[2, 5, 8],
17+
[0, 4, 8],
18+
[2, 4, 6]
19+
];
20+
21+
startGame();
22+
23+
restatBtn.addEventListener("click", startGame);
24+
25+
function startGame() {
26+
circleTurn = false;
27+
cellElements.forEach(cell => {
28+
//reset everthing after the restart button is clicked
29+
cell.classList.remove(X_CLASS);
30+
cell.classList.remove(CIRCLE_CLASS);
31+
cell.removeEventListener("click", handleClick);
32+
33+
// ekchtoti kunai cell click bhaisakesi tyo cell feri click garda value return garda
34+
// only once the click event is fired for a cell
35+
cell.addEventListener('click', handleClick, {once : true});
36+
});
37+
38+
setBoardHover();
39+
winningMessageDisplay.classList.remove('show');
40+
}
41+
42+
function handleClick(e) {
43+
// console.log("clicked");
44+
45+
const cell = e.target;
46+
const currentClass = circleTurn ? CIRCLE_CLASS : X_CLASS;
47+
// console.log(currentClass)
48+
//placeMark
49+
placeMark(cell, currentClass);
50+
51+
//check for win
52+
if (checkWin(currentClass)) {
53+
// console.log("winner");
54+
endGame(false); //no draw case
55+
}
56+
else if (checkDraw()) {
57+
endGame(true);
58+
}
59+
else {
60+
// Switch Truns
61+
swapTurns();
62+
setBoardHover();
63+
}
64+
65+
66+
// Check for Draw
67+
function endGame(draw) {
68+
if(draw) {
69+
winningMessageText.innerHTML = `DRAW!!`;
70+
}
71+
else {
72+
winningMessageText.innerHTML = `
73+
${circleTurn ? "O's" : "X's"} Wins!`;
74+
}
75+
76+
winningMessageDisplay.classList.add('show');
77+
}
78+
79+
80+
}
81+
82+
function placeMark(cell, currentClass) {
83+
cell.classList.add(currentClass);
84+
}
85+
86+
function swapTurns() {
87+
circleTurn = !circleTurn;
88+
}
89+
90+
function setBoardHover() {
91+
92+
board.classList.remove(X_CLASS);
93+
board.classList.remove(CIRCLE_CLASS);
94+
95+
if (circleTurn) {
96+
board.classList.add(CIRCLE_CLASS);
97+
}
98+
else {
99+
board.classList.add(X_CLASS);
100+
}
101+
}
102+
103+
function checkWin ( currentClass) {
104+
// .some returns true if any of the comb is true
105+
return WINNING_COMBINATIONS.some(combination => {
106+
return combination.every(index => { //harek cell ko lagi check
107+
return cellElements[index].classList.contains(currentClass); // same class xa ki nai check
108+
});
109+
});
110+
}
111+
112+
function checkDraw() {
113+
return [...cellElements].every ( cell => { //destructring of array
114+
return cell.classList.contains(X_CLASS) ||
115+
cell.classList.contains(CIRCLE_CLASS);
116+
})
117+
}

‎tic-tac-toe/style.css‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
*, *::after, *::before {
2+
box-sizing: border-box;
3+
}
4+
5+
body {
6+
margin:0;
7+
background: #edf0f3;
8+
}
9+
10+
.board {
11+
width: 100vw;
12+
height: 100vh;
13+
display: grid;
14+
justify-content: center;
15+
align-content: center;
16+
justify-items: center;
17+
align-items: center;
18+
grid-template-columns: repeat(3, auto);
19+
}
20+
21+
.cell {
22+
width : 100px;
23+
height: 100px;
24+
/* background-color: #222; */
25+
border: 1px solid #222;
26+
display: flex;
27+
justify-content: center;
28+
align-items: center;
29+
position: relative;
30+
cursor: pointer;
31+
}
32+
33+
.cell:first-child,
34+
.cell:nth-child(2),
35+
.cell:nth-child(3) {
36+
border-top: none;
37+
}
38+
39+
.cell:nth-child(3n + 1) {
40+
border-left: none;
41+
}
42+
43+
.cell:nth-child(3n + 3) {
44+
border-right: none;
45+
}
46+
47+
.cell:nth-child(7),
48+
.cell:nth-child(8),
49+
.cell:nth-child(9) {
50+
border-bottom: none;
51+
}
52+
53+
.cell.x::before,
54+
.cell.x::after,
55+
.cell.circle::before,
56+
.cell.circle::after {
57+
background-color: #222;
58+
}
59+
60+
.board.x .cell:not(.x):not(.circle):hover::before,
61+
.board.x .cell:not(.x):not(.circle):hover::after,
62+
.board.circle .cell:not(.x):not(.circle):hover::before,
63+
.board.circle .cell:not(.x):not(.circle):hover::after {
64+
background-color: rgb(194, 189, 189);
65+
}
66+
67+
.cell.x::before,
68+
.cell.x::after,
69+
.board.x .cell:not(.x):not(.circle):hover::before,
70+
.board.x .cell:not(.x):not(.circle):hover::after {
71+
content: '';
72+
width : calc( 90px * 0.12);
73+
height: 90px;
74+
/* background-color: #222; */
75+
position: absolute;
76+
}
77+
78+
.cell.x::before,
79+
.board.x .cell:not(.x):not(.circle):hover::before {
80+
transform: rotate(45deg);
81+
}
82+
83+
.cell.x::after,
84+
.board.x .cell:not(.x):not(.circle):hover::after {
85+
transform: rotate(-45deg);
86+
}
87+
88+
.cell.x,
89+
.cell.circle {
90+
cursor: not-allowed;
91+
}
92+
93+
.cell.circle::before,
94+
.cell.circle::after,
95+
.board.circle .cell:not(.circle):not(.x):hover::before,
96+
.board.circle .cell:not(.circle):not(.x):hover::after {
97+
content: '';
98+
width : 90px;
99+
height: 90px;
100+
/* background-color: #222; */
101+
position: absolute;
102+
border-radius: 50%;
103+
}
104+
105+
.cell.circle::before,
106+
.board.circle .cell:not(.circle):not(.x):hover::before {
107+
height: 90px;
108+
width: 90px;
109+
/* background-color: #222; */
110+
}
111+
112+
.cell.circle::after,
113+
.board.circle .cell:not(.circle):not(.x):hover::after {
114+
width: 70px;
115+
height: 70px;
116+
background-color: #edf0f3;
117+
}
118+
119+
.winning-msg {
120+
position: fixed;
121+
top : 0;
122+
right : 0;
123+
left : 0;
124+
bottom: 0;
125+
background: rgba(0, 0 ,0, 0.9);
126+
display: flex;
127+
justify-content: center;
128+
align-items: center;
129+
color : white;
130+
font-size: 5rem;
131+
flex-direction: column;
132+
display: none;
133+
}
134+
135+
.winning-msg button {
136+
font-size: 2.5rem;
137+
padding : 5px 15px;
138+
border-radius: 5px;
139+
text-transform: uppercase;
140+
font-family: open-sans;
141+
border: 1px solid black;
142+
cursor: pointer;
143+
margin-top: 25px;
144+
letter-spacing: 1px;
145+
}
146+
147+
.winning-msg button:hover {
148+
background-color: black;
149+
border: transparent;
150+
color: white;
151+
}
152+
153+
.winning-msg.show {
154+
display: flex;
155+
}

0 commit comments

Comments
 (0)