Tic Tac Toe Game

Developer
Size
2,701 Kb
Views
12,144

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 Previews

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; }
});
Tic Tac Toe Game - Script Codes
Tic Tac Toe Game - Script Codes
Home Page Home
Developer Jimmy Lin
Username odylic
Uploaded December 11, 2022
Rating 3
Size 2,701 Kb
Views 12,144
Do you need developer help for Tic Tac Toe Game?

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!

Jimmy Lin (odylic) Script Codes
Create amazing captions 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!