-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gadeharshada
committed
Nov 24, 2024
0 parents
commit a82da7f
Showing
3 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
let boxes=document.querySelectorAll(".box"); | ||
let resetBtn = document.querySelector("#reset-btn"); | ||
let newGameBtn=document.querySelector("#new-btn"); | ||
let msgContainer=document.querySelector(".msg-container"); | ||
let msg=document.querySelector("#msg"); | ||
|
||
let turnO= true ; | ||
const winPatterns=[ | ||
[0,1,2], | ||
[0,3,6], | ||
[0,4,8], | ||
[1,4,7], | ||
[2,5,8], | ||
[2,4,6], | ||
[3,4,5], | ||
[6,7,8] | ||
]; | ||
|
||
const resetGame=()=> { | ||
turnO=true; | ||
enableBoxes(); | ||
msgContainer.classList.add("hide"); | ||
}; | ||
|
||
boxes.forEach ((box)=> { | ||
box.addEventListener("click",()=>{ | ||
if(turnO){ | ||
box.innerText="O" | ||
turnO=false; | ||
|
||
}else{ | ||
box.innerText="X" | ||
turnO=true; | ||
} | ||
box.disabled=true; | ||
|
||
checkWinner(); | ||
}); | ||
}); | ||
|
||
const disableBoxes = ()=> { | ||
for(let box of boxes){ | ||
box.disabled=true; | ||
} | ||
}; | ||
const enableBoxes = ()=> { | ||
for(let box of boxes){ | ||
box.disabled=false; | ||
box.innerText=""; | ||
} | ||
}; | ||
|
||
const showWinner=(winner)=>{ | ||
msg.innertext='Congratulations,Winner is $(winner)'; | ||
msgContainer.classList.remove("hide"); | ||
disableBoxes(); | ||
}; | ||
|
||
const checkWinner=()=>{ | ||
for(let pattern of winPatterns) { | ||
let pos1Val = boxes[pattern[0]].innerText; | ||
let pos2Val = boxes[pattern[1]].innerText; | ||
let pos3Val = boxes[pattern[2]].innerText; | ||
|
||
if(pos1Val != "" && pos2Val != "" && pos3Val != "") { | ||
if (pos1Val === pos2Val && pos2Val === pos3Val) { | ||
showWinner(pos1Val); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
|
||
newGameBtn.addEventListener("click" , resetGame); | ||
resetBtn.addEventListener("click", resetGame); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="stylesheet" href="style.css"> | ||
<title>Tic-Tac-Toe Game</title> | ||
</head> | ||
<body> | ||
<div class="msg-container hide" > | ||
<p id="msg">Winner</p> | ||
<button id="new-btn">New Game</button> | ||
</div> | ||
<main> | ||
<h1>Tic-Tac-Toe Game</h1> | ||
<div class="container"> | ||
<div class="game"> | ||
<button class="box"></button> | ||
<button class="box"></button> | ||
<button class="box"></button> | ||
<button class="box"></button> | ||
<button class="box"></button> | ||
<button class="box"></button> | ||
<button class="box"></button> | ||
<button class="box"></button> | ||
<button class="box"></button> | ||
</div> | ||
</div> | ||
<button id="reset-btn">Reset Game</button> | ||
</main> | ||
<script src="first.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
*{ | ||
margin:0; | ||
padding:0; | ||
} | ||
body { | ||
background-color: cornflowerblue; | ||
text-align: center; | ||
} | ||
.container{ | ||
height:70vh; | ||
display: flex; | ||
justify-content: center; | ||
text-align: center; | ||
} | ||
|
||
.game{ | ||
height:60vmin; | ||
width:60vmin; | ||
display:flex; | ||
flex-wrap: wrap; | ||
justify-content: center; | ||
text-align: center; | ||
gap:1.5vmin; | ||
} | ||
.box { | ||
height: 18vmin; | ||
width:18vmin; | ||
border-radius: 1rem; | ||
border: none; | ||
box-shadow:0 0 1rem rgb(0,0,0,0.3); | ||
font-size: 8vmin; | ||
color: cadetblue; | ||
background-color: lightskyblue; | ||
} | ||
#reset-btn{ | ||
padding:1rem; | ||
font-size: 1.25rem; | ||
background-color: black; | ||
color: aliceblue; | ||
border-radius: 1rem; | ||
border:none; | ||
} | ||
#new-btn{ | ||
padding:1rem; | ||
font-size: 1.25rem; | ||
background-color: black; | ||
color: aliceblue; | ||
border-radius: 1rem; | ||
border:none; | ||
} | ||
|
||
#msg{ | ||
color:aliceblue; | ||
font-size:5vmin; | ||
} | ||
.msg-container{ | ||
height:100vmin; | ||
display:flex; | ||
justify-content: center; | ||
align-items: center; | ||
flex-direction: column; | ||
gap: 4rem; | ||
} | ||
.hide{ | ||
display: none; | ||
} |