Tic Tac Toe

Developer
Size
3,220 Kb
Views
24,288

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 Ujjwal Sharma on 25 August 2022, 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> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.6/darkly/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="jumbotron text-center"> <h1>Tic Tac Toe</h1>
</div>
<div class="container" ng-app="ttt" ng-controller="MainController"> <div class="alert alert-danger text-center" ng-hide="user.token"> <strong class="h3" style="display: block">Pick a Side!</strong> <p>Now that you have decided to play this game, lets help you decide.</p> <button class="dark btn btn-lg" ng-click="setUserToken('x')">X</button> <button class="light btn btn-lg" ng-click="setUserToken('o')">O</button> </div> <div class="alert alert-info text-center" ng-show="user.token"> <div class="row"> <div class="col-xs-6 col-md-6 col-lg-6"> <strong>User Score ( {{user.token | uppercase}} )</strong> <h1>{{ user.score }}</h1> </div> <div class="col-xs-6 col-md-6 col-lg-6"> <strong>AI Score ( {{ai.token | uppercase}} )</strong> <h1>{{ ai.score }}</h1> </div> </div> <p></p> </div> <div class="game text-center" ng-show="user.token"> <table class="center-block"> <tr ng-repeat="row in board track by $index"> <td ng-repeat="col in row track by $index" ng-click="userMove($parent.$index,$index)"> {{ board[$parent.$index][$index] | uppercase }} </td> </tr> </table> </div> <button class="btn btn-lg btn-danger center-block" ng-show="user.token" ng-click="resetBoard()">Reset</button>
</div> <script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Tic Tac Toe - Script Codes CSS Codes

.thumbnail { color: black;
}
button.dark { background-color: black; color: white; margin-top: 20px;
}
button.light, button.light:hover, button.light:focus { background-color: white; color: black; margin-top: 20px;
}
.game > table { background-color: white; color: black; width: 320px; height: 320px; padding: 10px; border-radius: 10px;
}
.game tr { height: 100px;
}
.game td { width: 100px; font-size: 50px;
}
tr:first-child { border-bottom: 1px solid black;
}
tr:last-child { border-top: 1px solid black;
}
td:first-child { border-right: 1px solid black;
}
td:last-child { border-left: 1px solid black;
}
button.btn-lg.btn-danger { margin-top: 20px;
}
.col-md-6:first-child { border-right: 2px white solid;
}

Tic Tac Toe - Script Codes JS Codes

var Player = function(token) { this.score = 0; this.token = token;
}
angular.module('ttt', [])
.controller('MainController', function($scope) { $scope.user = new Player(); $scope.ai = new Player(); $scope.board = [ ['', '', ''], ['', '', '', ], ['', '', ''] ]; $scope.setUserToken = function(token) { $scope.user.token = token; if (token == 'x') $scope.ai.token = 'o'; else $scope.ai.token = 'x'; }; $scope.userMove = function(row, column) { if (!$scope.board[row][column]) { setBoard($scope.user.token, row, column); computeAI(); } }; $scope.resetBoard = function() { $scope.board = [ ['', '', ''], ['', '', '', ], ['', '', ''] ]; console.log('reset'); } function setBoard(token, row, column) { $scope.board[row][column] = token; checkVictory(); }; function aiMove(row, column) { if (!$scope.board[row][column]) setBoard($scope.ai.token, row, column); }; function computeAI() { // Check for AI victory first for (var i = 0; i < 3; i++) { if (checkToken($scope.board[i][0], $scope.ai) && checkToken($scope.board[i][2], $scope.ai) && $scope.board[i][1] == '') { aiMove(i, 1); return; } if (checkToken($scope.board[i][0], $scope.ai) && checkToken($scope.board[i][1], $scope.ai) && $scope.board[i][2] == '') { aiMove(i, 2); return; } if (checkToken($scope.board[i][1], $scope.ai) && checkToken($scope.board[i][2], $scope.ai) && $scope.board[i][0] == '') { aiMove(i, 0); return; } } for (var j = 0; j < 3; j++) { if (checkToken($scope.board[0][j], $scope.ai) && checkToken($scope.board[2][j], $scope.ai) && $scope.board[1][j] == '') { aiMove(1, j); return; } if (checkToken($scope.board[0][j], $scope.ai) && checkToken($scope.board[1][j], $scope.ai) && $scope.board[2][j] == '') { aiMove(2, j); return; } if (checkToken($scope.board[1][j], $scope.ai) && checkToken($scope.board[2][j], $scope.ai) && $scope.board[0][j] == '') { aiMove(0, j); return; } } if (checkToken($scope.board[0][0], $scope.ai) && checkToken($scope.board[2][2], $scope.ai) && $scope.board[1][1] == '') { aiMove(1, 1); return; } if (checkToken($scope.board[0][2], $scope.ai) && checkToken($scope.board[2][0], $scope.ai) && $scope.board[1][1] == '') { aiMove(1, 1); return; } // Check for User victory later for (var i = 0; i < 3; i++) { if (checkToken($scope.board[i][0], $scope.user) && checkToken($scope.board[i][2], $scope.user) && $scope.board[i][1] == '') { aiMove(i, 1); return; } if (checkToken($scope.board[i][0], $scope.user) && checkToken($scope.board[i][1], $scope.user) && $scope.board[i][2] == '') { aiMove(i, 2); return; } if (checkToken($scope.board[i][1], $scope.user) && checkToken($scope.board[i][2], $scope.user) && $scope.board[i][0] == '') { aiMove(i, 0); return; } } for (var j = 0; j < 3; j++) { if (checkToken($scope.board[0][j], $scope.user) && checkToken($scope.board[2][j], $scope.user) && $scope.board[1][j] == '') { aiMove(1, j); return; } if (checkToken($scope.board[0][j], $scope.user) && checkToken($scope.board[1][j], $scope.user) && $scope.board[2][j] == '') { aiMove(2, j); return; } if (checkToken($scope.board[1][j], $scope.user) && checkToken($scope.board[2][j], $scope.user) && $scope.board[0][j] == '') { aiMove(0, j); return; } } if (checkToken($scope.board[0][0], $scope.user) && checkToken($scope.board[2][2], $scope.user) && $scope.board[1][1] == '') { aiMove(1, 1); return; } if (checkToken($scope.board[0][2], $scope.user) && checkToken($scope.board[2][0], $scope.user) && $scope.board[1][1] == '') { aiMove(1, 1); return; } // If nothing works, just random your way out. var empties = []; for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { if ($scope.board[i][j] == '') empties.push([i,j]); } } var final = empties[Math.floor(empties.length * Math.random())]; aiMove(final[0],final[1]); return; } function checkVictory() { var b = $scope.board; for (var i = 0; i < 3; i++) { if (b[i][0] != '' && b[i][0] == b[i][1] == b[i][2]) $scope.resetBoard(); } for (var i = 0; i < 3; i++) { if (b[0][i] != '' && b[0][i] == b[1][i] == b[2][i]) $scope.resetBoard(); } if (b[0][0] != '' && b[0][0] == b[1][1] == b[2][2]) $scope.resetBoard(); if (b[2][0] != '' && b[2][0] == b[1][1] == b[0][2]) $scope.resetBoard(); } function checkToken(token, player) { if (player.token == token.toLowerCase()) return true; return false; }
});
Tic Tac Toe - Script Codes
Tic Tac Toe - Script Codes
Home Page Home
Developer Ujjwal Sharma
Username ryzokuken
Uploaded August 25, 2022
Rating 3
Size 3,220 Kb
Views 24,288
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!

Ujjwal Sharma (ryzokuken) Script Codes
Create amazing marketing copy 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!