Tic Tac Toe Game
How do I make an tic tac toe game?
Tic Tac Toe Game for a FreeCodeCamp zipline.. What is a tic tac toe game? How do you make a tic tac toe game? This script and codes were developed by Joo Vctor De Oliveira Santos on 20 August 2022, Saturday.
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://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="myModal" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title text-center">Tic Tac Toe</h4> </div> <div class="modal-body text-center"> <p><b>X</b> or <b>O</b>?</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal"><b>X</b></button> <button type="button" class="btn btn-default" data-dismiss="modal"><b>O</b></button> </div> </div> </div> </div> <div id="wonModal" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title text-center" id="wonHeader">You Won!</h4> </div> <div class="modal-body text-center"> <button type="button" class="btn btn-default" data-dismiss="modal" onclick="game.restart();"><b>Restart</b></button> </div> </div> </div> </div> </br> </br> </br> <div class="container tab"> <div class="row"> <div class="col-sm-2 col-sm-offset-5"> <table class="table table-bordered"> <tr> <td><button id="a0" class="btn btn-default">X</button></td> <td><button id="a1" class="btn btn-default">X</button></td> <td><button id="a2" class="btn btn-default">X</button></td> </tr> <tr> <td><button id="b0" class="btn btn-default">X</button></td> <td><button id="b1" class="btn btn-default">X</button></td> <td><button id="b2" class="btn btn-default">X</button></td> </tr> <tr> <td><button id="c0" class="btn btn-default">X</button></td> <td><button id="c1" class="btn btn-default">X</button></td> <td><button id="c2" class="btn btn-default">X</button></td> </tr> </table> </div> </div> </div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Tic Tac Toe Game - Script Codes CSS Codes
td{ text-align: center;
}
.tab button{ min-width: 64px; min-height: 64px;
}
Tic Tac Toe Game - Script Codes JS Codes
/* Favorecer a escolha dos blocos centrais-exteriores (e até mesmo testar o centro), isso deve resolver o problema da "armadilha mortal". (inf-direito, top-direito, inf-esquerdo)
*/
//CONFIG GAME
// Bind buttons to buttonClick function
function bindButtons(){ for (var j = 0; j < 3; ++j) { var currentLetter = (j == 0) ? "a" : (j == 1) ? "b" : "c"; for (var i = 0; i < 3; ++i) { $('#' + currentLetter + i).on("click", null, [i,j], buttonClick); } }
}
bindButtons();
// Handle button click event
function buttonClick(event){ var pos = event.data; game.playerClick(pos[0],pos[1]);
}
// Handle the select "character" modal.
function selectXOrO() { $('#myModal').modal(); $('#myModal .modal-footer button').on('click', function(e) { var target = $(e.target); $(this).closest('.modal').on('hidden.bs.modal', function() { game.playerChar = target.text(); game.computerChar = (target.text() == "X") ? "O" : "X"; console.log("Player Select: " + game.playerChar + "\nSo computer is: " + game.computerChar); game.restart(); }); });
}
//CONFIG GAME
var game = { tiles: [ ['z','l','s'],['d','f','g'],['e','w','r'] ], won: false, playerChar: 'X', computerChar: 'O', father: null, score: 0, currentPlayer: 'p', depth:0, canPutThere: function(x, y){ return (this.tiles[y][x] == ""); }, updateScreen: function() { for (var j = 0; j < this.tiles.length; ++j) { var currentLetter = (j == 0) ? "a" : (j == 1) ? "b" : "c"; for (var i = 0; i < this.tiles[j].length; ++i) { $('#' + currentLetter + i).text(this.tiles[j][i]); } } console.log("screen updated"); }, isFinished: function(){ this.won = this.verifyIfWon(); var av = this.restantMoves(); console.log("verifyIfWon: ",this.won," ",av," are Available."); if(this.won != false || av == 0){ if(av == 0) this.won = "tie"; this.someoneWon(); return true; } return false; }, restart: function() { for (var i = 0; i < this.tiles.length; i++) { for (var j = 0; j < this.tiles[i].length; j++) { this.tiles[i][j] = ''; } } //this.tiles = [ ['O','','X'],['X','',''],['X','O','O'] ] this.won = false; this.updateScreen(); console.log("game restarted."); }, playerClick: function(x, y){ if(this.canPutThere(x,y) && this.won == false){ this.tiles[y][x] = this.playerChar; if(!this.isFinished()){ var result = this.simulateComputer(); console.log("Computer: ",result); this.tiles[result[1]][result[0]] = this.computerChar; this.isFinished() } this.updateScreen(); } }, copy: function(){ var ngame = jQuery.extend(true, {}, this); ngame.father = this; ngame.depth = this.depth + 1; return ngame;
}, simulateComputer: function(){ var w = this.verifyIfWon(); if(w != false){ if(w == this.playerChar) this.scores = this.depth - 25; else if(w == this.computerChar) this.scores = 25 - this.depth; }else this.scores = 0; var moves = this.getAvailableMoves(); var simulations = []; var g = this; if(this.depth < 2) moves.forEach(function(x){ console.debug(); var y = g.copy(); y.currentPlayer = y.currentPlayer == 'p' ? 'c' : 'p'; y.tiles[x[1]][x[0]] = y.currentPlayer == 'p' ? g.playerChar : g.computerChar; simulations.push(y);
// if(y.restantMoves != 0) y.simulateComputer(); }); // simulations.forEach() if(this.father != null) this.father.scores += this.scores; var win = false; simulations.forEach(function(x, i){ console.log("d: ", x.depth, " " ,i, " - ", x.scores, " - ", moves[i]); if(x.scores > 0) win = true; }); if(simulations.length == 0)return; if(win){ console.log("i can win!"); return moves[simulations.indexOf(simulations.reduce(function(p,c){return p.scores >= c.scores ? p : c;}))]; }else{ console.log("i can't win, so i going to give you a bad time!"); return moves[simulations.indexOf(simulations.reduce(function(p,c){return (Math.abs(p.scores) <= Math.abs(c.scores) ) ? p : c}))]; } if(simulations.length > 0) return moves[simulations.indexOf(simulations.reduce(function(p,c){return p.scores >= c.scores ? p : c}))]; }, verifyIfWon: function(){ //Horizontal for(var y = 0; y < 3; ++y){ if(this.tiles[y][0] == this.tiles[y][1] && this.tiles[y][1] == this.tiles[y][2] && this.tiles[y][0] != ""){ return this.tiles[y][1]; } } //Vertical for(var x = 0; x < 3; ++x){ if(this.tiles[0][x] == this.tiles[1][x] && this.tiles[1][x] == this.tiles[2][x] && this.tiles[0][x] != "" ){ return this.tiles[1][x]; } } //Diagonal if((this.tiles[0][2] == this.tiles[1][1] && this.tiles[2][0] == this.tiles[1][1] && this.tiles[1][1] != "")){ return this.tiles[0][2]; } if(this.tiles[0][0] == this.tiles[1][1] && this.tiles[2][2] == this.tiles[1][1] && this.tiles[1][1] != ""){ return this.tiles[0][0]; } return false;
}, restantMoves: function(){ var ammount = 0; for(var y = 0; y < this.tiles.length; ++y) for(var x = 0; x < this.tiles[y].length; ++x) ammount += (this.tiles[y][x] == '') ? 1 : 0; return ammount; }, someoneWon: function(){ $('#wonModal').modal(); $('#wonHeader').text(this.won == this.playerChar ? "You Won!" : this.won == this.computerChar ? "Computer Won!" : "It's a Tie"); },
getAvailableMoves: function(){ var ret = []; var ind = 0; for(var y = 0; y < 3; ++y){ for(var x = 0; x < 3; ++x){ if(this.tiles[y][x] == ""){ ret[ind] = [x,y]; ind++; } } } return ret;
}
};
selectXOrO();
Developer | Joo Vctor De Oliveira Santos |
Username | jvhti |
Uploaded | August 20, 2022 |
Rating | 3 |
Size | 3,932 Kb |
Views | 34,408 |
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 |
Markdown Previewer | 11,087 Kb |
Testimonial Gallery | 3,887 Kb |
A Pen by Joo Vctor de Oliveira Santos | 1,647 Kb |
Local Weather | 2,478 Kb |
JQuery UI Playground | 5,215 Kb |
Page Size Menu Example | 2,009 Kb |
Youtube Random Video With Cover Image | 2,600 Kb |
JavaScript Calculator | 2,542 Kb |
Random Quote Machine | 2,611 Kb |
Simon Game | 3,197 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 |
CSS Tooltips | Darylldoyle | 2,599 Kb |
Highbrow Basic HTML Lesson 7 | Kimlarocca | 1,662 Kb |
Sass Get Function | Lukewatts | 2,155 Kb |
BlueBox Fork | Huskynation | 12,675 Kb |
Login with flip 3d animation | Afiq | 3,012 Kb |
Bootstrap Carousel Fade Transition | Rowno | 2,484 Kb |
Sticky div | Kaslab | 2,225 Kb |
Toolbar | Onsen | 5,414 Kb |
Emberjs Bootstrap Modal Carousel | Somethingkindawierd | 4,233 Kb |
Cut and Paste Roll Link | BottomlineInteractive | 2,546 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!