Tic-tac-toe

Developer
Size
3,730 Kb
Views
4,048

How do I make an tic-tac-toe?

What is a tic-tac-toe? How do you make a tic-tac-toe? This script and codes were developed by Yas on 05 January 2023, Thursday.

Tic-tac-toe Previews

Tic-tac-toe - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Tic-tac-toe</title> <link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Comfortaa:700|Roboto:300,400'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <link href="https://fonts.googleapis.com/css?family=Comfortaa:700|Droid+Sans+Mono|Roboto:300,400" rel="stylesheet">
<body> <div id="container"> <div id="frame"> <div class="game-text text-left" id="p1">Player-1: <span id="s1">0</span></div> <div class="game-text text-right" id="p2">Player-2: <span id="s2">0</span></div> <div class="vertical" id="v-left"></div> <div class="vertical" id="v-right"></div> <div class="horizontal" id="h-top"></div> <div class="horizontal" id="h-bottom"></div> <div class="x xo" id="x1">X</div> <div class="x xo" id="x2">X</div> <div class="x xo" id="x3">X</div> <div class="x xo" id="x4">X</div> <div class="x xo" id="x5">X</div> <div class="x xo" id="x6">X</div> <div class="x xo" id="x7">X</div> <div class="x xo" id="x8">X</div> <div class="x xo" id="x9">X</div> <div class="o xo" id="o1">O</div> <div class="o xo" id="o2">O</div> <div class="o xo" id="o3">O</div> <div class="o xo" id="o4">O</div> <div class="o xo" id="o5">O</div> <div class="o xo" id="o6">O</div> <div class="o xo" id="o7">O</div> <div class="o xo" id="o8">O</div> <div class="o xo" id="o9">O</div> <div class="win-hor-line" id="line1"></div> <div class="win-hor-line" id="line2"></div> <div class="win-hor-line" id="line3"></div> <div class="win-ver-line" id="line4"></div> <div class="win-ver-line" id="line5"></div> <div class="win-ver-line" id="line6"></div> <div class="win-diag-line" id="line7"></div> <div class="win-diag-line" id="line8"></div> <div class="box pos1" id="box1" onclick="draw('1')">&nbsp;</div> <div class="box pos2" id="box2" onclick="draw('2')">&nbsp;</div> <div class="box pos3" id="box3" onclick="draw('3')">&nbsp;</div> <div class="box pos4" id="box4" onclick="draw('4')">&nbsp;</div> <div class="box pos5" id="box5" onclick="draw('5')">&nbsp;</div> <div class="box pos6" id="box6" onclick="draw('6')">&nbsp;</div> <div class="box pos7" id="box7" onclick="draw('7')">&nbsp;</div> <div class="box pos8" id="box8" onclick="draw('8')">&nbsp;</div> <div class="box pos9" id="box9" onclick="draw('9')">&nbsp;</div> <div class="turnLine">&nbsp;</div> </div> </div>
</body> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Tic-tac-toe - Script Codes CSS Codes

body { background-color: #6ec2b1;
}
#frame { positon: relative; width: 500px; height: 600px; margin: 0 auto; /*background-color: #999;*/
}
.vertical { position: absolute; height: 350px; width: 8px; background-color: #3d9986;
}
#s1 { font-weight: bold; font-size: 28px;
}
#s2 { font-weight: bold; font-size: 28px;
}
#v-left { margin-left: 170px; margin-top: 90px;
}
#v-right { margin-left: 292px; margin-top: 90px;
}
.horizontal { position: absolute; width: 350px; height: 8px; background-color: #3d9986;
}
#h-top { margin-left: 60px; margin-top: 200px;
}
#h-bottom { margin-left: 60px; margin-top: 320px;
}
.win-hor-line { display: block; position: absolute; height: 8px; width: 310px; background-color: #21a5e3; opacity: 0;
}
#line1 { margin-left: 80px; margin-top: 142px;
}
#line2 { margin-left: 80px; margin-top: 260px;
}
#line3 { margin-left: 80px; margin-top: 380px;
}
.win-ver-line { display: block; position: absolute; width: 8px; height: 310px; background-color: #21a5e3; opacity: 0;
}
#line4 { margin-left: 110px; margin-top: 110px;
}
#line5 { margin-left: 230px; margin-top: 110px;
}
#line6 { margin-left: 354px; margin-top: 110px;
}
.win-diag-line { display: block; position: absolute; width: 8px; height: 410px; background-color: #21a5e3; opacity: 0;
}
#line7 { margin-left: 232px; margin-top: 61px; transform: rotate(135deg);
}
#line8 { margin-left: 232px; margin-top: 61px; transform: rotate(45deg);
}
.game-text { color: #3d6b62; font-family: 'Roboto', sans-serif; font-weight: 300; font-size: 22px; margin: 30px 30px;
}
.text-left { float: left;
}
.text-right { float: right; padding-right: 28px;
}
.xo { position: absolute; font-family: 'Comfortaa', cursive; font-size: 80px; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;
}
.x { color: #063c31; padding: 2px 22px;
}
.o { color: #dcf8f3; padding: 2px 18px;
}
#x1, #o1, #box1 { margin-top:94px; margin-left:62px; opacity: 0;
}
#x2, #o2, #box2 { margin-top:94px; margin-left:184px; opacity: 0;
}
#x3, #o3, #box3 { margin-top:94px; margin-left:306px; opacity: 0;
}
#x4, #o4, #box4 { margin-top:212px; margin-left:62px; opacity: 0;
}
#x5, #o5, #box5 { margin-top:212px; margin-left:184px; opacity: 0;
}
#x6, #o6, #box6 { margin-top:212px; margin-left:306px; opacity: 0;
}
#x7, #o7, #box7 { margin-top:332px; margin-left:62px; opacity: 0;
}
#x8, #o8, #box8 { margin-top:332px; margin-left:184px; opacity: 0;
}
#x9, #o9, #box9 { margin-top:332px; margin-left:306px; opacity: 0;
}
.box { display: block; position: absolute; width: 102px; height: 102px;
}
.turnLine { display: block; position: absolute; height: 4px; width: 130px; background-color: #fff; margin-top: 65px; margin-left: 20px; opacity: .75;
}

Tic-tac-toe - Script Codes JS Codes

var turnSymbol = "x";
var lastTurn = "x";
var turnNumber = 0;
var arrX = [0,0,0,0,0,0,0,0];
var arrO = [0,0,0,0,0,0,0,0];
var numArr = [1,2,3,4,5,6,7,8,9];
var xIs = 2;
var p1Score = 0;
var p2Score = 0;
var posi = "pos1 pos2 pos3 pos4 pos5 pos6 pos7 pos8 pos9";
function clearGame(num) { $("#line" + num).animate({'opacity':0}, 'slow'); numArr.forEach(function (elem) { $("#x" + elem.toString()).animate({'opacity':0}, 'slow'); $("#o" + elem.toString()).animate({'opacity':0}, 'slow'); }); $("#s1").html(p1Score.toString()); $("#s2").html(p2Score.toString()); arrX = [0,0,0,0,0,0,0,0]; arrO = [0,0,0,0,0,0,0,0]; turnNumber = 0; $('#box1').attr('onclick', 'draw("1")'); $('#box2').attr('onclick', 'draw("2")'); $('#box3').attr('onclick', 'draw("3")'); $('#box4').attr('onclick', 'draw("4")'); $('#box5').attr('onclick', 'draw("5")'); $('#box6').attr('onclick', 'draw("6")'); $('#box7').attr('onclick', 'draw("7")'); $('#box8').attr('onclick', 'draw("8")'); $('#box9').attr('onclick', 'draw("9")'); $(".turnLine").css({"background-color":'white'}, 'fast'); if (lastTurn === "x") { console.log("O starts"); turnSymbol = "o" lastTurn = "o" } else { console.log("x starts"); turnSymbol = "x"; lastTurn = "x" }
}
function draw(pos) { $('#box' + pos).attr('onclick', ''); if(turnSymbol === "x") { $("#x" + pos).animate({'opacity':100}, 'slow'); $(".turnLine").animate({"margin-left":'325px'}, 'fast'); gotThree("x", pos); } else { $("#o" + pos).animate({'opacity':100}, 'slow'); $(".turnLine").animate({"margin-left":'20px'}, 'fast'); var z = pos gotThree("o", z); } turnNumber++; if (turnNumber === 9) { setTimeout(function() { clearGame(0); }, 1500); }
}
function isAWinner() { function hasThree(value) { return value >= 3; } function drawLine(num) { if(winLine < 4) { $("#line" + num).animate({'opacity':'1'}, 'fast'); } else if (num > 6) { $("#line" + num).animate({'opacity':'1'}, 'fast'); } else { $("#line" + num).animate({'opacity':'1'}, 'fast'); } setTimeout(function() { clearGame(winLine); }, 2000); } var x = arrX.filter(hasThree); console.log("x arr:", arrX); var o = arrO.filter(hasThree); console.log("o arr:", arrO); if(x.length > 0) { if(xIs === 1) { p1Score++; $(".turnLine").animate({"margin-left":'20px'}, '1'); $(".turnLine").css({"background-color":'orange'}, 'fast'); } else { p2Score++; $(".turnLine").animate({"margin-left":'325px'}, '1'); $(".turnLine").css({"background-color":'orange'}, 'fast'); } var winLine = arrX.findIndex(hasThree) + 1; drawLine(winLine); } if(o.length > 0) { if(xIs === 1) { p2Score++; $(".turnLine").animate({"margin-left":'325px'}, 1); $(".turnLine").css({"background-color":'orange'}, 'fast'); } else { p1Score++; $(".turnLine").animate({"margin-left":'20px'}, 1); $(".turnLine").css({"background-color":'orange'}, 'fast'); } var winLine = arrO.findIndex(hasThree) + 1; drawLine(winLine); } else { if (turnSymbol === "x") { turnSymbol = "o"; } else { turnSymbol = "x"; } }
}
function gotThree(symb, pos) { console.log("gotThree"); console.log("symbol is:", symb); console.log("position is:", pos); switch(pos) { case '1': if(symb === "x") { arrX[0]++; arrX[3]++; arrX[6]++; } else { arrO[0]++; arrO[3]++; arrO[6]++; } break; case '2': if(symb === "x") { arrX[0]++; arrX[4]++; } else { arrO[0]++; arrO[4]++; } break; case '3': if(symb === "x") { arrX[0]++; arrX[5]++; arrX[7]++; } else { arrO[0]++; arrO[5]++; arrO[7]++; } break; case '4': if(symb === "x") { arrX[1]++; arrX[3]++; } else { arrO[1]++; arrO[3]++; } break; case '5': if(symb === "x") { arrX[1]++; arrX[4]++; arrX[6]++; arrX[7]++; } else { arrO[1]++; arrO[4]++; arrO[6]++; arrO[7]++; } break; case '6': if(symb === "x") { arrX[1]++; arrX[5]++; } else { arrO[1]++; arrO[5]++; } break; case '7': if(symb === "x") { arrX[2]++; arrX[3]++; arrX[7]++; } else { arrO[2]++; arrO[3]++; arrO[7]++; } break; case '8': if(symb === "x") { arrX[2]++; arrX[4]++; } else { arrO[2]++; arrO[4]++; } break; case '9': if(symb === "x") { arrX[2]++; arrX[5]++; arrX[6]++; } else { arrO[2]++; arrO[5]++; arrO[6]++; } break; } isAWinner();
}
Tic-tac-toe - Script Codes
Tic-tac-toe - Script Codes
Home Page Home
Developer Yas
Username Yas46
Uploaded January 05, 2023
Rating 3
Size 3,730 Kb
Views 4,048
Do you need developer help for Tic-tac-toe?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Yas (Yas46) Script Codes
Create amazing love letters with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!