Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added html script for tictac-toe game #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions TicTacToe/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic Tac Toe</title>

<link rel="stylesheet" href="styles.css" >
</head>
<body>
<nav>
<ul>
<li>MyTicTacToe.com</li>
</ul>
</nav>

<div class="gameContainer">
<div class="container">
<div class="box bt-0 bl-0"><span class="boxtext"></span></div>
<div class="box bt-0" ><span class="boxtext"></span></div>
<div class="box bt-0 br-0"><span class="boxtext"></span></div>
<div class="box bl-0"><span class="boxtext"></span></div>
<div class="box"><span class="boxtext"></span></div>
<div class="box br-0"><span class="boxtext"></span></div>
<div class="box bl-0 bb-0"><span class="boxtext"></span></div>
<div class="box bb-0"><span class="boxtext"></span></div>
<div class="box bb-0 br-0"><span class="boxtext"></span></div>
</div>
<div class="gameInfo">
<h1>Welcome to Tic Tac Toe Game</h1>
<div>
<span class="info">Turn for X</span>
<button id="reset">Reset</button>
</div>
<div class="imageBox">

</div>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
63 changes: 63 additions & 0 deletions TicTacToe/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
console.log("Welcome to Tic Tac Toe");
let turn = "X";
let gameOver = false;


//Function to change the turn
const changeTurn = ()=>{
return turn === "X" ? "0" : "X"
}

//Function to check win
const checkWin = () => {
let boxtext = document.getElementsByClassName("boxtext");
let wins = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
]
wins.forEach(e =>{
if((boxtext[e[0]].innerText === boxtext[e[1]].innerText) &&(boxtext[e[1]].innerText === boxtext[e[2]].innerText) && (boxtext[e[1]].innerText !== "") )
{
document.querySelector(".info").innerHTML = boxtext[e[0]].innerText + " Won"
gameOver = true;
}
})
}


//Game Logic

let boxes = document.getElementsByClassName("box");
Array.from(boxes).forEach(element => {
let boxtext = element.querySelector(".boxtext");
element.addEventListener("click", ()=> {
if(boxtext.innerHTML === "")
{
boxtext.innerHTML = turn;
turn = changeTurn();
checkWin();
if(!gameOver){
document.querySelector(".info").innerHTML = "Turn for " + turn;
}
}
})
})

//Add on event listener for reset

reset.addEventListener("click",() =>{
let boxtext = document.getElementsByClassName("boxtext");
Array.from(boxtext).forEach((element) =>{
element.innerHTML = "";
})
turn = "X"
gameOver = false
document.querySelector(".info").innerHTML = "Turn for " + turn;
})

92 changes: 92 additions & 0 deletions TicTacToe/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
*{
margin: 0;;
padding: 0;
}

nav{
background-color:rgb(34, 2, 35);
color:white;
height: 55px;
font-size:22px;
padding:0 15px;
display:flex;
align-items:center;
}

nav ul{
list-style-type:none;
}

.gameContainer{
display:flex;
margin-top:50px;
justify-content:center;
}

.container{
display:grid;
grid-template-rows: repeat(3,10vw);
grid-template-columns: repeat(3,10vw);
}

.box{
border:2px solid black;
font-size:8vw;
cursor:pointer;
display:flex;
justify-content:center;
align-items:center;
}
.box:hover{
background-color:rgb(247, 235, 247);

}
.gameInfo{
padding:0 34px;
}
.gameInfo div{
padding:15px 0;
}
.info{
font-size: 1.2rem;
padding: 0 5px;
}
#reset{
padding: 4px 11px;
margin: 0 9px;
background-color: #ddd3e7;
font-size: 1.2rem;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
}



.br-0{
border-right:0;
}
.bl-0{
border-left:0;
}
.bt-0{
border-top:0;
}
.bb-0{
border-bottom:0;
}


@media screen and (max-width:600px)
{
.gameContainer{
flex-wrap: wrap;
}
.gameInfo{
margin-top: 20px;
}
.container{
grid-template-rows: repeat(3,20vw);
grid-template-columns: repeat(3,20vw);
}
}