Tic Tac Toe Game
How do I make an tic tac toe game?
What is a tic tac toe game? How do you make a tic tac toe game? This script and codes were developed by Jimmy Lin on 11 December 2022, Sunday.
Tic Tac Toe Game - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Tic Tac Toe Game</title> <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet">
<div class="container text-center"> <h1>Tic-Tac-Toe</h1> </br> <a class= "btn btn-primary" id="turnX">Turn X</a> <a class= "btn btn-secondary" id="turnO">TurnO</a> <div class="container text-center" id="game"> <div class="row"> <div class="text-center "> <a class="btn btn-primary tic" id="0">#</a> <a class="btn btn-primary tic" id="1">#</a> <a class="btn btn-primary tic" id="2">#</a> </div> </div> <div class="row"> <div class="text-center "> <a class="btn btn-primary tic" id="3">#</a> <a class="btn btn-primary tic" id="4">#</a> <a class="btn btn-primary tic" id="5">#</a> </div> </div> <div class="row"> <div class="text-center "> <a class="btn btn-primary tic" id="6">#</a> <a class="btn btn-primary tic" id="7">#</a> <a class="btn btn-primary tic" id="8">#</a> </div> </div> </div> </div>
</div> <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 Game - Script Codes CSS Codes
#game { margin-top:30px; padding:10px; border-style: solid; width: 300px; border-color: grey;
}
.btn { padding:20px;
}
h1 { font-family: lobster; font-size:50px;
}
Tic Tac Toe Game - Script Codes JS Codes
$(document).ready(function(){ var turn="X" var turns=["#","#","#","#","#","#","#","#","#"]; var computersTurn="0"; var gameOn=false; //keeps track of computer's turn, so no loop var count=0; $('#turnX').click(function(){ turn = "X"; computersTurn ="O"; $("#turnX").addClass("btn-primary"); $("#turnO").removeClass("btn-primary"); reset(); }); $('#turnO').click(function(){ turn = "O"; computersTurn ="X"; $("#turnO").addClass("btn-primary"); $("#turnX").removeClass("btn-primary"); reset(); }); function computerTurn(){ //break while loop var taken = false; while(taken===false && count !== 9) { //generate computer turn var computersMove=(Math.random()*10).toFixed(); var move=$("#"+computersMove).text(); if(move==="#"){ $("#"+computersMove).text(computersTurn); taken=true; turns[computersMove]=computersTurn; count++; } } } function playerTurn(turn, id){ var spotTaken= $("#" +id).text(); if(spotTaken==="#"){ count++; turns[id]=turn; $("#"+id).text(turn); winCondition(turns, turn); if(gameOn===false){ computerTurn(); winCondition(turns,computersTurn); } } } function winCondition(turnArray, currentTurn){ if (turnArray[0] ===currentTurn && turnArray[1]===currentTurn && turnArray[2]===currentTurn){ gameOn=true; reset(); alert("Player "+currentTurn+ " wins! (Top row across)"); } else if (turnArray[0] ===currentTurn && turnArray[3]===currentTurn && turnArray[6]===currentTurn){ gameOn=true; reset(); alert("Player "+ currentTurn + " wins! (Left row down)"); } else if (turnArray[0] ===currentTurn && turnArray[4]===currentTurn && turnArray[8]===currentTurn){ gameOn=true; reset(); alert("Player "+ currentTurn + " wins! (Left top to Bottom Right diagonal"); } else if (turnArray[3] ===currentTurn && turnArray[4]===currentTurn && turnArray[5]===currentTurn){ gameOn=true; reset(); alert("Player "+ currentTurn + " wins! (Middle Row Across)"); } else if (turnArray[1] ===currentTurn && turnArray[4]===currentTurn && turnArray[7]===currentTurn){ gameOn=true; reset(); alert("Player "+ currentTurn + " wins! (Middle Row Down)"); } else if (turnArray[2] ===currentTurn && turnArray[5]===currentTurn && turnArray[8]===currentTurn){ gameOn=true; reset(); alert("Player "+ currentTurn + " wins! (Right Row Down)"); } else if (turnArray[2] ===currentTurn && turnArray[4]===currentTurn && turnArray[6]===currentTurn){ gameOn=true; reset(); alert("Player "+ currentTurn + " wins! (Right Top to Left Bottom Diagonal)"); } else if (turnArray[6] ===currentTurn && turnArray[7]===currentTurn && turnArray[8]===currentTurn){ gameOn=true; reset(); alert("Player "+ currentTurn + " wins! (Bottom Row Across)"); } else if (count==9){ gameOn=true; reset(); alert("Draw"); } else { gameOn=false; } } $(".tic").click(function(){ var slot = $(this).attr('id'); playerTurn(turn, slot); }); function reset(){ turns=["#", "#","#","#","#","#","#","#","#"]; count=0; $(".tic").text("#"); gameOn=true; }
});

Developer | Jimmy Lin |
Username | odylic |
Uploaded | December 11, 2022 |
Rating | 3 |
Size | 2,701 Kb |
Views | 12,138 |
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!
Name | Size |
Local Weather | 2,560 Kb |
Twitch JSON API | 2,556 Kb |
Random Quote Generator | 2,674 Kb |
Portfolio Template | 2,581 Kb |
Simon Game | 3,485 Kb |
Firefly tribute | 2,476 Kb |
RPG Style Text Dialogue | 2,635 Kb |
TwitchTV JSON API 2 | 2,691 Kb |
Mark Down Previewer | 3,062 Kb |
Pomodoro Clock | 2,384 Kb |
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!
Name | Username | Size |
Project_one | MOHIM | 9,592 Kb |
Blog | Rottingroom | 1,430 Kb |
Lazy Load for Background Images | The_ruther4d | 2,977 Kb |
Animating characters with jQuery | 042 | 2,776 Kb |
Headroom.js demo | WickyNilliams | 3,982 Kb |
CSS- UI Element States Pseudo-Classes | Tesla809 | 2,206 Kb |
Buttons for autumn | Nikazawila | 1,795 Kb |
Nice responsive team page | Infomiho | 3,139 Kb |
Single element checkbox | Ivijaygupta | 1,996 Kb |
Mini Profile | Frytyler | 3,828 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!