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 Paulo Sérgio on 19 November 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.6/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="container" ng-app="app"> <div class="row" ng-controller="MainController"> <div class="col-md-4 col-md-offset-4 main-containter"> <div class="row"> <div class="col-xs-12"> <div class="panel panel-default"> <div class="panel-heading text-center"><b>Tic Tac Toe Game</b></div> <div class="panel-body" ng-if='start'> <span class="pull-left left"><b>{{turn === userChoice? 'User Turn' : ''}}</b></span> <span class="pull-right right"><b>{{turn === computerChoice? 'Computer Turn' : ''}}</b></span> </div> <div class="panel-body" ng-if="!newgame"> <button ng-click="startNewGame()" class="btn btn-primary btn-block">New Game</button> </div> </div> </div> </div> <!-- end row --> <div class="col-xs-4" ng-repeat="button in buttons"> <!-- send index of element --> <pg-button index="{{$index}}"></pg-button> </div> <!-- end ng-repeat div --> </div> </div>
</div> <script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js'></script>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.1.1/ui-bootstrap-tpls.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Tic Tac Toe Game - Script Codes CSS Codes
.main-containter { margin-top: 100px; background-color: #BBB; padding: 0px 20px 40px 10px; width: 420px; min-height: 470px; border-radius: 30px;
}
.panel { margin-left: -10px;
}
.panel,
.panel-heading { width: 420px; border-radius: 30px 30px 0px 0px;
}
.tictac-container { min-height: 100px; min-width: 100px; max-height: 100px; max-width: 100px; background-color: black; border-radius: 2px; margin: 15px 15px 0px 15px!important; border-radius: 10px; color: white; text-align: center; line-height: 90px; font-size: 80px;
}
.tictac-external { z-index: 10000; position: absolute; min-height: 115px; min-width: 115px; max-height: 115px; max-width: 115px; top: 7px; left: 23px; transition-property: background-color; transition-duration: 3s;
}
.tictac-external-left { border-left-style: solid; border-left: 10px solid white; border-color: #ffffff;
}
.tictac-external-right { border-right-style: solid; border-right: 10px solid white; border-color: #ffffff;
}
.tictac-external-bottom { border-bottom-style: solid; border-bottom: 10px solid white; border-color: #ffffff;
}
.tictac-external-top { border-top-style: solid; border-top: 10px solid white; border-color: #ffffff;
}
.col-xs-4 { width: 120px !important;
}
.tictac-external:hover { background-color: blue; transition-property: background-color color; transition-duration: 3s; color: red !important;
}
.pull-left,
.pull-right { font-size: 20px; margin-left: 20px; margin-right: 20px; font-style: oblique;
}
.panel-heading { font-size: 20px;
}
.left { background-image: -webkit-gradient(linear, left top, right top, color-stop(0, #f22), color-stop(0.45, #2ff), color-stop(0.6, #2f2), color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22)); background-image: gradient(linear, left top, right top, color-stop(0, #f22), color-stop(0.45, #2ff), color-stop(0.6, #2f2), color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22)); color: transparent; -webkit-background-clip: text; background-clip: text;
}
.right { background-image: -webkit-gradient(linear, left top, right top, color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2), color-stop(0.75, #2f2), color-stop(0.9, #f22)); background-image: gradient(linear, left top, right top, color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2), color-stop(0.75, #2f2), color-stop(0.9, #f22)); color: transparent; -webkit-background-clip: text; background-clip: text;
}
Tic Tac Toe Game - Script Codes JS Codes
angular.module('app' , ['ui.bootstrap'])
.controller('MainController' , function($scope , $uibModal , $rootScope , MainService , $timeout){ $scope.buttons = []; $scope.userChoice = ''; $scope.computerChoice = ''; $scope.turn = '?'; $scope.game = []; $scope.newgame = true; $scope.timeout = $timeout; $scope.modal = $uibModal; makeModal($uibModal , templateModal(), 'static' , 'sm' , 'ChoiceCtrl'); MainService.setUiObject($uibModal); $scope.startNewGame = function(){ //$scope.buttons = []; $scope.userChoice = ''; $scope.computerChoice = ''; $scope.turn = '?'; $scope.game = []; $scope.newgame = true; setNewGame($scope); $timeout(function(){ makeModal($uibModal , templateModal(), 'static' , 'sm' , 'ChoiceCtrl'); } , 1000); } //receive broadcast message from service MainService //after this, so start game $scope.$on('choice', function(evt, message){ //console.log("message: " + message); // and you can put your $get method here $scope.userChoice = message; $scope.computerChoice = (message === 'X' ? 'O' : 'X'); $scope.turn = MainService.setTurn($scope.computerChoice , $scope.userChoice); //first to start MainService.setGame($scope.game); MainService.setUser($scope.userChoice); MainService.setComputer($scope.computerChoice); MainService.startGame($scope); $scope.start = true; }) for(var i = 0 ; i < 9 ; i++){ $scope.buttons.push(i); $scope.game.push('?'); }
})
.controller('ChoiceCtrl' , function($scope , MainService , $uibModalInstance){ $scope.choice = ''; $scope.select = function(symbol){ MainService.setChoice(symbol); $scope.choice = symbol; $uibModalInstance.close(); }
})
.controller('EndGameCtrl' , function($scope, MainService , $uibModalInstance){ $scope.close = function(){ $uibModalInstance.close(); }
})
.directive('pgButton', function(MainService , $timeout){ return { restrict: 'E', template: templateBase(), link: function(scope, element, attrs){ var container = element.find('.tictac-external'); //console.log(container); MainService.addAttr(container , parseInt(attrs['index'])); element.on('click' , function(){ var game = MainService.getGame(); var index = parseInt(attrs['index']); var move = game[index]; //turn of user if(MainService.getUser() === MainService.getTurn() && move === '?' && scope.$parent.newgame){ game[index] = MainService.getUser(); MainService.setGame(game); scope.$parent.game = game; scope.$parent.$apply(); //MainService.setTurn(MainService.getComputer()); if(isDrawGame(game)){ console.log('draw game test'); scope.$parent.newgame = false; scope.$parent.userChoice = ''; scope.$parent.computerChoice = ''; scope.$parent.turn = '?'; scope.$parent.game = []; MainService.setGame([]); scope.$parent.$apply(); //return; makeModal(MainService.getUiObject() , templateEndGame('Draw Game!!!'), 'static' , 'sm' , 'EndGameCtrl'); //templateEndGame(message) return; } if(isEndGame(game)){ console.log('end game test'); scope.$parent.newgame = false; scope.$parent.userChoice = ''; scope.$parent.computerChoice = ''; scope.$parent.turn = '?'; scope.$parent.game = []; MainService.setGame([]); scope.$parent.$apply(); makeModal(MainService.getUiObject() , templateEndGame('You Win!!!'), 'static' , 'sm' , 'EndGameCtrl'); return; } //computer move scope.$parent.turn = scope.$parent.computerChoice; scope.$parent.$apply(); computerMove(game, scope.$parent.computerChoice , scope.$parent.userChoice); MainService.setGame(game); scope.$parent.game = game; $timeout(function(){ if(isDrawGame(game)){ console.log('draw game test'); scope.$parent.newgame = false; scope.$parent.userChoice = ''; scope.$parent.computerChoice = ''; scope.$parent.turn = '?'; scope.$parent.game = []; MainService.setGame([]); scope.$parent.$apply(); //return; makeModal(MainService.getUiObject() , templateEndGame('Draw Game!!!'), 'static' , 'sm' , 'EndGameCtrl'); //templateEndGame(message) return; } if(isEndGame(game)){ console.log('end game test'); scope.$parent.newgame = false; scope.$parent.userChoice = ''; scope.$parent.computerChoice = ''; scope.$parent.turn = '?'; scope.$parent.game = []; MainService.setGame([]); scope.$parent.$apply(); makeModal(MainService.getUiObject() , templateEndGame('Computer Win!!!'), 'static' , 'sm' , 'EndGameCtrl'); return; } scope.$parent.turn = scope.$parent.userChoice; scope.$parent.$apply(); } , 1000); //end timeout } //end if }) //end on click } }
})
.service('MainService' , function($rootScope){ var choice = '', turn = '', endGame = false, game = [], computerChoice = '', userChoice = '', setUiObject = null; this.getUiObject = function(){ return setUiObject; } this.getUser = function(){ return userChoice; } this.setUiObject = function(obj){ setUiObject = obj; } this.getComputer = function(){ return computerChoice; } this.getTurn = function(){ return turn; } this.setUser = function(choice){ userChoice = choice; } this.setComputer = function(choice){ computerChoice = choice; } this.addAttr = function(container , index){ switch(index){ case 0: container.addClass('tictac-external-bottom tictac-external-right'); break; case 1: container.addClass('tictac-external-bottom tictac-external-right tictac-external-left'); break; case 2: container.addClass('tictac-external-bottom tictac-external-left'); break; case 3: container.addClass('tictac-external-bottom tictac-external-top tictac-external-right'); break; case 4: container.addClass('tictac-external-bottom tictac-external-top tictac-external-left tictac-external-right'); break; case 5: container.addClass('tictac-external-bottom tictac-external-top tictac-external-left'); break; case 6: container.addClass('tictac-external-top tictac-external-right'); break; case 7: container.addClass('tictac-external-top tictac-external-left tictac-external-right'); break; case 8: container.addClass('tictac-external-top tictac-external-left'); break; } }; //end function this.setChoice = function(symbol){ choice = symbol; $rootScope.$broadcast('choice', choice); }; this.startGame = function(scope){ //first move if(allEqual()){ //if is computer move if(turn === computerChoice){ scope.timeout(function(){ computerMove(game, computerChoice , userChoice); scope.game = game; scope.turn = userChoice; turn = userChoice; } , 1000); } } } this.setGame = function(gameObj){ game = gameObj; } this.getGame = function(){ return game; } this.setTurn = function(computerChoice , userChoice){ turn = (Math.random() >= 0.5 ? computerChoice : userChoice); return turn; } function allEqual(){ for(var i =0; i< game.length ; i++){ if(game[i] !== '?') return false; } return true; }
})
function templateModal(){ return '<div class="modal-body text-center" id="modal-body">\ Would you like to be X or O?\ <br>\ <br>\ <br>\ <b><button ng-click="select(\'X\')" class="btn btn-primary">X</button></b>\ <b><button ng-click="select(\'O\')" class="btn btn-danger">O</button></b>\ </div>'
}
function templateBase(){ return '<div class="tictac-external"></div>\ <div class="tictac-container text-center">{{game[$index] === "?" ? "" : game[$index]}}</div>';
}
function templateEndGame(message){ return '<div class="modal-body">' + '<span class="text-center">' + message + '</span>' + '</div>' + '<div class="modal-footer">' + '<button ng-click="close()">Close</button>' + '</div>';
}
var isEndGame = function(game){ if(game.length === 0) return true; if((game[0] === game[1] && game[1]=== game[2]) && game[0] !== '?'){ return true; } if((game[3] === game[4] && game[4] === game[5]) && game[3] !== '?'){ return true; } if((game[6] === game[7] && game[7] === game[8]) && game[6] !== '?'){ return true; } if((game[0] === game[3] && game[3] === game[6]) && game[6] !== '?'){ return true; } if((game[1] === game[4] && game[4] === game[7]) && game[1] !== '?'){ return true; } if((game[2] === game[5] && game[5] === game[8]) && game[2] !== '?'){ return true; } if((game[0] === game[4] && game[4] === game[8]) && game[0] !== '?'){ return true; } if( (game[2] === game[4] && game[4] === game[6]) && game[6] !== '?'){ return true; } return false;
}
function isDrawGame(game){ for(var i = 0; i < game.length ; i++){ if(game[i] === '?'){ return false; } } return true;
}
var makeModal = function(uiobject , template, static , size , controller){ var modal = uiobject.open({ animation: true, ariaLabelledBy: 'modal-title', ariaDescribedBy: 'modal-body', template: template, backdrop : 'static', keyboard : false, controller: controller, size: size }); return modal;
}
function setNewGame(scope){ for(var i = 0 ; i < 9 ; i++){ //scope.buttons.push(i); scope.game.push('?'); }
}
function computerMove(game, computerChoice , userChoice){ if(!game) return -1; if(!Array.isArray(game)) return -1; if(game.length == 0 || game.length < 8) return -1; for(var i = 0; i < 3 ; i++){ //horizontal test if user in advantage if(game[i] == userChoice && game[i + 1] == userChoice && game[i+2] == '?'){ game[i+2] = computerChoice; return 1; } if(game[i] == userChoice && game[i + 2] == userChoice && game[i+1] == '?'){ game[i+1] = computerChoice; return 1; } if(game[i+1] == userChoice && game[i + 2] == userChoice && game[i] == '?'){ game[i] = computerChoice; return 1; } //vertical advantage if(game[i] == userChoice && game[i + 3] == userChoice && game[i+6] == '?'){ game[i+6] = computerChoice; return 1; } if(game[i] == userChoice && game[i + 6] == userChoice && game[i+3] == '?'){ game[i+3] = computerChoice; return 1; } if(game[i+3] == userChoice && game[i + 6] == userChoice && game[i] == '?'){ game[i] = computerChoice; return 1; } //crossed user advantage test if(game[0] == userChoice && game[4] == userChoice && game[8] == '?'){ game[8] = computerChoice; return 1; } if(game[4] == userChoice && game[8] == userChoice && game[0] == '?'){ game[0] = computerChoice; return 1; } if(game[0] == userChoice && game[8] == userChoice && game[4] == '?'){ game[4] = computerChoice; return 1; } //reverse crossed game user advantage if(game[2] == userChoice && game[4] == userChoice && game[6] == '?'){ game[6] = computerChoice; return 1; } if(game[4] == userChoice && game[6] == userChoice && game[2] == '?'){ game[2] = computerChoice; return 1; } if(game[2] == userChoice && game[6] == userChoice && game[4] == '?'){ game[4] = computerChoice; return 1; } //end user test //test computer advantage //horizontal if(game[i] == computerChoice && game[i + 1] == computerChoice && game[i+2] == '?'){ game[i+2] = computerChoice; return 1; } if(game[i] == computerChoice && game[i + 2] == computerChoice && game[i+1] == '?'){ game[i+1] = computerChoice; return 1; } if(game[i+1] == computerChoice && game[i + 2] == computerChoice && game[i] == '?'){ game[i] = computerChoice; return 1; } //vertical if(game[i] == computerChoice && game[i + 3] == computerChoice && game[i+6] == '?'){ game[i+6] = computerChoice; return 1; } if(game[i] == computerChoice && game[i + 6] == computerChoice && game[i+3] == '?'){ game[i+3] = computerChoice; return 1; } if(game[i+3] == computerChoice && game[i + 6] == computerChoice && game[i] == '?'){ game[i] = computerChoice; return 1; } //end computer test //crossed reverse advantage if(game[2] == computerChoice && game[4] == computerChoice && game[6] == '?'){ game[6] = computerChoice; return 1; } if(game[4] == computerChoice && game[6] == computerChoice && game[2] == '?'){ game[2] = computerChoice; return 1; } if(game[2] == computerChoice && game[6] == computerChoice && game[4] == '?'){ game[4] = computerChoice; return 1; } //crossed advantage //crossed user advantage test if(game[0] == computerChoice && game[4] == computerChoice && game[8] == '?'){ game[8] = computerChoice; return 1; } if(game[4] == computerChoice && game[8] == computerChoice && game[0] == '?'){ game[0] = computerChoice; return 1; } if(game[0] == computerChoice && game[8] == computerChoice && game[4] == '?'){ game[4] = computerChoice; return 1; } } //empty test var index = findEmptyPosition(game); if(index >= 0){ game[index] = computerChoice; return; } return -1;
}
function findEmptyPosition(game){ for(var i = 0; i < game.length ; i++){ if(game[i] == '?') return i; } return -1;
}
Developer | Paulo Sérgio |
Username | paulo101977 |
Uploaded | November 19, 2022 |
Rating | 3 |
Size | 6,111 Kb |
Views | 18,216 |
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 |
Project Singapoure | 4,037 Kb |
Spinner Loader SVG Pure Css Radius Loader | 2,148 Kb |
Simon Game | 4,988 Kb |
Tributo a Renato Russo | 5,611 Kb |
Donut Chart D3js | 3,246 Kb |
Menu Navigation Items | 5,500 Kb |
Twitch.tv API Query | 3,631 Kb |
Input Range Multiple CSS Jquery Javascript Scss Sass | 2,880 Kb |
Pomodoro Clock | 3,306 Kb |
WeatherMap | 13,360 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 |
Web Spiral - p5.js | TWAIN | 2,183 Kb |
Image grid with captions | Mchernin34 | 2,222 Kb |
Impress JS Algorhytmic Generator | Jeffscottward | 7,906 Kb |
Chuck Norris Background | Manz | 1,967 Kb |
RWD Conversion Practice | Jxqr97 | 2,743 Kb |
Hoi hoi | JohnTheCat | 7,248 Kb |
Amazing CSS Menu with Notification Badges | Faizanasad | 2,549 Kb |
Sign Up Form | Sicontis | 5,272 Kb |
Bezier Animation with straight paths | Rhernando | 2,087 Kb |
Bootstrap Carousel Fade Transition | Rowno | 2,484 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!