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 tic-tac-toe #33

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
2 changes: 2 additions & 0 deletions WebDev.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ Submission has to be done by sharing your github repo link and deployment link o
<!-- Add you name in below list as -->
<!-- - Your Name - [Repo Name](Link) [Site](Site Link) -->
<!-- - Sanyu Daver - [Tic Tac](https://github.com/sanyud/TicTac) [Site](www.copsiitbhu.co.in) -->

<!-- - Kriti Chaudhari- [csoc-2021-task-1](https://github.com/SymmetricEntropy/csoc-2021-task-1) [Site](Site Link) -->
203 changes: 203 additions & 0 deletions tic-tac-toe/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
playerNum = document.querySelector('.playerNum');
buttonX = document.querySelector('#x');
buttonO = document.getElementById('o');
symbol = document.querySelector('.symbol');
table = document.querySelector('.table');
boxes = document.querySelectorAll('.box');
winnerBox = document.querySelector('.winner');
winnerName = document.querySelector('#name');
replay =document.querySelector('.replay');

var player1, player2, winner, noOfMoves=0, mode;
var whichSymbol, whichMode;
var playerXBoxes = [0, 0, 0, 0, 0, 0, 0, 0, 0];
var playerOBoxes = [0, 0, 0, 0, 0, 0, 0, 0, 0];

function reset(){
playerXBoxes = [0, 0, 0, 0, 0, 0, 0, 0, 0];
playerOBoxes = [0, 0, 0, 0, 0, 0, 0, 0, 0];
for(var i=0; i<9; i++){
boxes[i].textContent = "";
}
winner=undefined;
mode = undefined;
noOfMoves=0;
}

document.getElementById('single').addEventListener('click', clickSingleMulti);
document.getElementById('multi').addEventListener('click', clickSingleMulti);


function clickSingleMulti(n){
whichMode = n;
n.target.style.boxShadow = "0 0 10px 1px yellow";
setTimeout(()=>{playerNum.style.display = "none";
symbol.style.display = "block"}, 200);
setTimeout(() => whichMode.target.style.boxShadow = "none", 200);

mode = (n.target.textContent == 'Single Player')? "single" : "multi";

}



buttonX.addEventListener('click', clickOX);
buttonO.addEventListener('click', clickOX);

function clickOX(e){
console.log(e);
whichSymbol = e;
if(e.target.textContent == 'x'){
player1 = 'x';
player2 = 'o';
}
else{
player1 = 'o';
player2 = 'x';
}
player = player1;
e.target.style.boxShadow = "0 0 10px 1px yellow";
setTimeout(changeScreen, 200);
setTimeout(() => whichSymbol.target.style.boxShadow = "none", 200);
}

function changeScreen(){
symbol.style.display = "none";
table.style.display = "block";
}

for(var i=0; i<9; i++){
boxes[i].addEventListener('click', clickBox);
}


function clickBox(boxInfo){
var boxId = boxInfo.target.id;
console.log(boxId);
console.log(boxInfo);
if(boxInfo.target.textContent != "") return;
boxInfo.target.style.cssText = 'color: white; font-size: 70px';
boxInfo.target.textContent= player;

if(player == 'x'){
playerXBoxes[boxId-1] = 1;
console.log(playerXBoxes);
checkXWinner();
}
else{
playerOBoxes[boxId-1] = 1;
console.log(playerOBoxes);
checkOWinner();
}
noOfMoves++;
if(noOfMoves == 9)
gameTie();
player = player=='o'?'x':'o';
if(mode=='single' && noOfMoves <= 7){
setTimeout(botMove, 500);
}
}

function botMove(){
var botBoxId;
do{
botBoxId = Math.floor(Math.random()*9) +1;
console.log(document.getElementById(botBoxId));
}while(document.getElementById(botBoxId) != null && document.getElementById(botBoxId).textContent != "");
//Use minMax() to get botBoxId and then delete above 3 lines.
//minMax();

console.log(botBoxId);
botBox = document.getElementById(botBoxId)
console.log(document.getElementById(botBoxId));
botBox.style.cssText = 'color: white; font-size: 70px';
botBox.textContent= player;

console.log(player);
if(player == 'x'){
playerXBoxes[botBoxId-1] = 1;
console.log(playerXBoxes);
checkXWinner();
}
else{
playerOBoxes[botBoxId-1] = 1;
console.log(playerOBoxes);
checkOWinner();
}
noOfMoves++;
if(noOfMoves == 9)
gameTie();
player = player=='o'?'x':'o';

}


function checkXWinner(){

if(playerXBoxes[0]+playerXBoxes[1]+playerXBoxes[2]==3 ||
playerXBoxes[3]+playerXBoxes[4]+playerXBoxes[5]==3 ||
playerXBoxes[6]+playerXBoxes[7]+playerXBoxes[8]==3 ||
playerXBoxes[0]+playerXBoxes[3]+playerXBoxes[6]==3 ||
playerXBoxes[1]+playerXBoxes[4]+playerXBoxes[7]==3 ||
playerXBoxes[2]+playerXBoxes[5]+playerXBoxes[8]==3 ||
playerXBoxes[0]+playerXBoxes[4]+playerXBoxes[8]==3 ||
playerXBoxes[2]+playerXBoxes[4]+playerXBoxes[6]==3){
winner = 'x';
setTimeout(winnerFound , 500);
}


}

function checkOWinner(){
//the win cases
if(playerOBoxes[0]+playerOBoxes[1]+playerOBoxes[2]==3 ||
playerOBoxes[3]+playerOBoxes[4]+playerOBoxes[5]==3 ||
playerOBoxes[6]+playerOBoxes[7]+playerOBoxes[8]==3 ||
playerOBoxes[0]+playerOBoxes[3]+playerOBoxes[6]==3 ||
playerOBoxes[1]+playerOBoxes[4]+playerOBoxes[7]==3 ||
playerOBoxes[2]+playerOBoxes[5]+playerOBoxes[8]==3 ||
playerOBoxes[0]+playerOBoxes[4]+playerOBoxes[8]==3 ||
playerOBoxes[2]+playerOBoxes[4]+playerOBoxes[6]==3){
winner = 'o';
setTimeout(winnerFound , 500);
}
}

function gameTie(){
winnerName.textContent = "Game ties!";
setTimeout(()=>{}, 500);
table.style.display = 'none';
winnerBox.style.display = 'block';
replay.style.display = 'block';
}

function winnerFound(){
console.log(winner);
if((winner == 'x' && player1 == 'x') || (winner == 'o' && player1 == 'o'))
winner = "Player 1";
else
winner = "Player 2";
winnerName.textContent = winner+" wins!!";

table.style.display = 'none';
winnerBox.style.display = 'block';
replay.style.display = 'block';

}

replay.addEventListener('click', toReplay);
function toReplay(e){
e.target.style.boxShadow = "0 0 10px 1px yellow";
setTimeout(changeScreenToStart, 200);
}

function changeScreenToStart(){
winnerBox.style.display = 'none';
replay.style.display = 'none';
playerNum.style.display = "block";
reset();
setTimeout(()=>replay.style.boxShadow='none', 200);
}


117 changes: 117 additions & 0 deletions tic-tac-toe/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
div.container{
color: #4293f5;
text-align: center;
font-size: 40px;
padding: 8px;
}

.container h2{
text-shadow: 3px 1px lightgrey;
}



div.symbol,
div.playerNum{
color: white;
background-color: #4293f5;
text-align: center;
margin: 10% 30% 20% 30%;
padding: 20px 20px 20px 20px;
font-size: 25px;
border-radius: 10px;
box-shadow: 0 4px 8px 0 grey;
}

div.symbol{
display: none;
}

span#single , span#multi{
font-size: 40px;
}

span#x, span#o{
font-size: 80px;
}


span#x, span#o, span#single , span#multi{
display: inline-block;
background-color: white;
color: #4293f5;
border-radius: 10px;
margin-top: 20px;
margin-bottom: 2px;
padding-top: 3px;
padding-bottom: 3px;
width: 48%;
height: 80px;
}

span#single:hover , span#multi:hover ,
span#x:hover , span#o:hover, .replay:hover{
transform: scale(0.98);
}

.table{
display: none;
}
table{
border-collapse: collapse;
margin-left: auto;
margin-right: auto;
background-color: #4293f5;
}
td{
border: 4px solid white;
width: 100px;
height:100px;
}

table tr td:first-child{
border-left: 0;
}
table tr td:last-child{
border-right: 0;
}
table tr:first-child td{
border-top: 0;
}
table tr:last-child td{
border-bottom: 0;
}

td:hover,
.replay:hover
{
background-color: #4dc3ff;
}

.winner, .replay{
display: none;
color: white;
background-color: #4293f5;
text-align: center;
margin-top: 6%;
margin-left: 30%;
margin-right: 30%;
padding: 20px 20px 20px 20px;
font-size: 25px;
border-radius: 10px;
box-shadow: 0 4px 8px 0 grey;
}

#name{
display: block;
background-color: white;
color: #4293f5;
font-size: 30px;
border-radius: 10px;
margin-top: 20px;
margin-bottom: 20px;
padding-top: 6px;
padding-bottom: 6px;
width: 100%;
height: 50px;
}
59 changes: 59 additions & 0 deletions tic-tac-toe/tic-tac-toe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html>
<head>
<title>Tic-Tac-Toe</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h2>TIC-TAC-TOE</h2>
<div class="playerNum">
Which mode would you like?
<br>
<span id="single">Single Player</span>
<span id="multi">Two Players</span>
</div>


<div class="symbol">
Player 1: Which would be your symbol?
<br>
<span id="x">x</span>
<span id="o">o</span>
</div>

<div class="table">
<table>
<tr>
<td class="box" id="1"></td>
<td class="box" id="2"></td>
<td class="box" id="3"></td>
</tr>
<tr>
<td class="box" id="4"></td>
<td class="box" id="5"></td>
<td class="box" id="6"></td>
</tr>
<tr>
<td class="box" id="7"></td>
<td class="box" id="8"></td>
<td class="box" id="9"></td>
</tr>
</table>


</div>

<div class="winner">
<span id="name"></span>
</div>

<div class="replay">
Replay
</div>


</div>
<script src="script.js"></script>
</body>
</html>