HTML file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic Tac Toe</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="container">
<div class="container_box">
<div class="heading">
<h1>Tic Tac Toe</h1>
</div>
<div class="game_box">
<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 class="btn_box">
<button id="new_game">New Game</button>
<button id="reset">Restart</button>
</div>
<div class="para">
<p id="para">Winner</p>
</div>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
CSS file
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.container{
width: 100vw;
height: 100vh;
background-color: #6f6866;
display: flex;
justify-content: center;
/* align-items: center; */
}
.container_box{
margin-top: 2.5rem;
height: 90vh;
width: 30vw;
background-color: #788585;
display: flex;
flex-direction: column;
align-items: center;
}
.heading{
margin-top: 20px;
}
.game_box{
margin-top: 40px;
height: 60%;
width: 80%;
display: flex;
justify-content: center;
flex-wrap: wrap;
column-gap: 20px;
}
.box{
border: none;
height: 100px;
width: 100px;
background-color: #9caea9;
border-radius: 20px;
color: #38302E;
font-size: 40px;
font-weight: bold;
}
.btn_box{
margin-top: 10px;
height: 50px;
width: 230px;
}
#new_game{
padding: 5px;
font-size: 18px;
font-weight: bold;
border-radius: 10px;
margin: 4px;
background-color: #9caea9;
}
#reset{
margin: 4px;
padding: 5px;
font-size: 18px;
font-weight: bold;
border-radius: 10px;
background-color: #9caea9;
}
.para{
height: 100px;
width: 400px;
}
#para{
font-size: 25px;
color: gold;
padding-top: 20px;
display: flex;
justify-content: center;
}
JavaScript file
let boxes = document.querySelectorAll('.box');
let newGame = document.querySelector('#new_game');
let restart = document.querySelector('#reset');
let msg = document.querySelector('#para');
let turnO = true; // playerO playerX
let winPattern = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
let reset = () => {
let turnO = true;
enableBoxes();
msg.innerHTML = "Winner";
}
boxes.forEach((box) => {
box.addEventListener('click', () => {
if(turnO){
box.innerText = 'O';
turnO = false;
}
else{
box.innerText = 'X';
turnO = true;
}
box.disabled = true;
checkWinner();
})
})
let disableBoxes = () => {
for (let box of boxes){
box.disabled = true;
}
}
let enableBoxes = () => {
for (let box of boxes){
box.disabled = false;
box.innerText = "";
}
}
let showWinner = (sins) =>{
msg.innerText = `Congrats, Winner is player ${sins}`
disableBoxes()
}
let checkWinner = () => {
for (let sunny of winPattern){
let pos1Val = boxes[sunny[0]].innerText;
let pos2Val = boxes[sunny[1]].innerText;
let pos3Val = boxes[sunny[2]].innerText;
if(pos1Val != "" && pos2Val != "" && pos3Val != ""){
if(pos1Val == pos2Val && pos2Val == pos3Val){
console.log("winner")
showWinner(pos1Val);
}
}
}
}
restart.addEventListener('click', reset)
newGame.addEventListener('click', reset)
Result
0 Comments