Tic Tac Toe

Developer
Size
3,793 Kb
Views
14,168

How do I make an tic tac toe?

FreeCodeCamp Intermediate Zipline: Build a Tic Tac Toe Game. What is a tic tac toe? How do you make a tic tac toe? This script and codes were developed by Kw7oe on 26 November 2022, Saturday.

Tic Tac Toe Previews

Tic Tac Toe - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Tic Tac Toe</title> <link href='https://fonts.googleapis.com/css?family=Bangers|Lobster|Montserrat' rel='stylesheet' type='text/css'> <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> <!-- DOESN'T WORKS IN SAFARI -->
<div class = "container">	<div class = "text-center">	<h1>Tic Tac Toe</h1>	<div class = "board">	<div class = "pos" id = "one">	<p></p>	</div>	<div class = "pos" id = "two">	<p></p>	</div>	<div class = "pos" id = "three">	<p></p>	</div>	<div class = "pos" id = "four">	<p></p>	</div>	<div class = "pos" id = "five">	<p></p>	</div>	<div class = "pos" id = "six">	<p></p>	</div>	<div class = "pos" id = "seven">	<p></p>	</div>	<div class = "pos" id = "eight">	<p></p>	</div>	<div class = "pos" id = "nine">	<p></p>	</div>	</div>	<div>	<dialog id = "choice">	<h2>Playing as</h2>	<button class = "btn btn-default" id = "o">O</button>	<button class = "btn btn-default" id = "x">X</button>	</dialog>	</div>	<div>	<dialog id = "end">	<h1>You Win</h1>	<button class = "btn btn-default" id = "play">Play Again</button>	</dialog>	</div>	</div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Tic Tac Toe - Script Codes CSS Codes

.container {	margin-top: 25px;
}
.pos {	display: inline-block;	float: left;	font-size: 48px;	line-height: 2.3;	color: #808080;	width: 120px;	height: 120px;	border: 5px solid #bfbfbf;	border-radius: 12px;	background-color: #d9d9d9;	margin: 3px;	cursor: pointer;
}
.board {	padding: 0;	display: inline-block;	margin: 40px 0;	width: 380px;
}
h1 {	font-family: 'Lobster', cursive;	color: #808080;
}
#choice {	width: 250px;	height: 180px;	background-color: #e6e6e6;	border: 4px solid #808080;	border-radius: 10px;
}
h2 {	color: #808080;	font-family: "Lobster";
}
#end {	font-family: 'Bangers', cursive;	text-transform: uppercase;	width: 250px;	height: 180px;	background-color: #e6e6e6;	border: 4px solid #808080;	border-radius: 10px;
}
#end .btn-default, #choice .btn-default {	border: 3px solid #808080;	border-radius: 8px;	margin-top: 15px;	font-size: 16px;	text-transform: uppercase;	font-family: 'Montserrat', sans-serif;	background-color: white;	padding: 5px 12px;	transition: color 0.5s, background-color 0.5s;
}
#end .btn-default:hover, #choice .btn-default:hover {	background-color: #808080;	color: white;
}

Tic Tac Toe - Script Codes JS Codes

var player;
var comp;
$(document).ready(function() {	var status = [];	status[0] = 0;	var array = [0,0,0,0,0,0,0,0,0];	var comChoice = [0,2,4,6,8];	var step;	var dialog = document.getElementById('choice'); dialog.showModal();	$(".pos").on("click", function() {	var check = $('p', this).text();	if (check === "" && status[0] === 0) {	$('p', this).hide().html(player).fadeIn(400);	readUserStep(array);	status[0] = checkGameStatus(array);	updateResult(status, array);	if (status[0] === 0) {	step = comStep(array, comChoice);	convertComStep(step);	array[step] = 2;	}	status[0] = checkGameStatus(array);	updateResult(status, array);	}	});	$("#play").on("click", function() {	var dialog = document.getElementById('end'); dialog.close();	step = comStep(array, comChoice);	convertComStep(step);	array[step] = 2;
});	$("#choice button").on("click", function() {	var dialog = document.getElementById('choice'); dialog.close();
});	$("#x").on('click', function() {	player = "X";	comp = "O";	step = comStep(array, comChoice);	convertComStep(step);	array[step] = 2;	})	$("#o").on('click', function() {	player = "O";	comp = "X";	step = comStep(array, comChoice);	convertComStep(step);	array[step] = 2;	})	})
function showResult() {	var dialog = document.getElementById('end'); dialog.showModal();
}
function readUserStep(arr) {	if ($("#one p").text() === player) {	arr[0] = 1;	}	if ($("#two p").text() === player) {	arr[1] = 1;	}	if ($("#three p").text() === player) {	arr[2] = 1;	}	if ($("#four p").text() === player) {	arr[3] = 1;	}	if ($("#five p").text() === player) {	arr[4] = 1;	}	if ($("#six p").text() === player) {	arr[5] = 1;	}	if ($("#seven p").text() === player) {	arr[6] = 1;	}	if ($("#eight p").text() === player) {	arr[7] = 1;	}	if ($("#nine p").text() === player) {	arr[8] = 1;	}
}
function convertComStep(a) {	if (a === 0) {	$("#one p").hide().html(comp).fadeIn(400);	}	else if (a === 1) {	$("#two p").hide().html(comp).fadeIn(400);	}	else if (a === 2) {	$("#three p").hide().html(comp).fadeIn(400);	}	else if (a === 3) {	$("#four p").hide().html(comp).fadeIn(400);	}	else if (a === 4) {	$("#five p").hide().html(comp).fadeIn(400);	}	else if (a === 5) {	$("#six p").hide().html(comp).fadeIn(400);	}	else if (a === 6) {	$("#seven p").hide().html(comp).fadeIn(400);	}	else if (a === 7) {	$("#eight p").hide().html(comp).fadeIn(400);	}	else if (a === 8) {	$("#nine p").hide().html(comp).fadeIn(400);	}
}
function checkHor(a, x) {	var move;	if (a[0] === x && a[1] === x && a[2] === 0) {	move = 2;	}	else if (a[0] === x && a[2] === x && a[1] === 0) {	move = 1;	}	else if (a[1] === x && a[2] === x && a[0] === 0) {	move = 0;	}	else if (a[3] === x && a[4] === x && a[5] === 0) {	move = 5;	}	else if (a[3] === x && a[5] === x && a[4] === 0) {	move = 4;	}	else if (a[4] === x && a[5] === x && a[3] === 0) {	move = 3;	}	else if (a[6] === x && a[7] === x && a[8] === 0) {	move = 8;	}	else if (a[6] === x && a[8] === x && a[7] === 0) {	move = 7;	}	else if (a[7] === x && a[8] === x && a[6] === 0) {	move = 6;	}	else {	move = -99;	}	return move;
}
function checkVer(a, x) {	var move;	if (a[0] === x && a[3] === x && a[6] === 0) {	move = 6;	}	else if (a[0] === x && a[6] === x && a[3] === 0) {	move = 3;	}	else if (a[3] === x && a[6] === x && a[0] === 0) {	move = 0;	}	else if (a[2] === x && a[5] === x && a[8] === 0) {	move = 8;	}	else if (a[2] === x && a[8] === x && a[5] === 0) {	move = 5;	}	else if (a[5] === x && a[8] === x && a[2] === 0) {	move = 2;	}	else if (a[1] === x && a[4] === x && a[7] === 0) {	move = 7;	}	else if (a[1] === x && a[7] === x && a[4] === 0) {	move = 4;	}	else if (a[4] === x && a[7] === x && a[1] === 0) {	move = 1;	}	else {	move = -99;	}	return move;
}
function checkSla(a, x) {	var move;	if ((a[0] === x && a[8] === x && a[4] === 0) || (a[2] === x && a[6] === x && a[4] === 0)) {	move = 4;	}	else if (a[2] === x && a[4] === x && a[6] === 0) {	move = 6;	}	else if (a[6] === x && a[4] === x && a[2] === 0) {	move = 2;	}	else if (a[0] === x && a[4] === x && a[8] === 0) {	move = 8;	}	else if (a[8] === x && a[4] === x && a[0] === 0) {	move = 0;	}	else {	move = -99;	}	return move;
}
function comStep(arr,choice) {	var x = -99;	var i = 0;	var check;	if (x === -99) {	x = checkHor(arr, 2);	}	if (x === -99) {	x = checkVer(arr, 2);	}	if (x === -99) {	x = checkSla(arr, 2);	}	if (x === -99) {	x = checkHor(arr, 1);	}	if (x === -99) {	x = checkVer(arr, 1);	}	if (x === -99) {	x = checkSla(arr, 1);	}	if (x === -99) {	do {	x = choice[Math.floor((Math.random() * 4) + 0)];	i += 1;	if (i > 5) {	x = -99;	break;	}	} while (arr[x] !== 0);	}	if (x === -99) {	do {	x = Math.floor((Math.random() * 9) + 0);	} while (arr[x] !== 0);	}	return x;
}
function checkGameStatus(a) {	var y = 0;	var i;	y = checkFilled(a);	for (i = 1; i < 9; i += 3) {	if (a[i-1] == a[i] && a[i] == a[i+1]) {	if (a[i] == 1) {	return 1;	}	else if (a[i] == 2) {	return 2;	}	}	}	for (i = 3; i < 6; i++) {	if (a[i-3] == a[i] && a[i] == a[i+3]) {	if (a[i] == 1) {	return 1;	}	else if (a[i] == 2) {	return 2;	}	}	}	if (a[0] == a[4] && a[4] == a[8]) {	if (a[4] == 1) {	return 1;	}	else if (a[4] == 2) {	return 2;	}	}	if (a[2] == a[4] && a[4] == a[6]) {	if (a[4] == 1) {	return 1;	}	else if (a[4] == 2) {	return 2;	}	}	if (y === 9 ) {	return 3;	}	return 0;
}
function updateResult(value, a) {	if (value[0] === 1) {	$("#end h1").html("You win");	setTimeout(function() {	showResult();	resetGame(a, value);	}, 1000);	}	else if (value[0] === 2) {	$("#end h1").html("You lose");	setTimeout(function() {	showResult();	resetGame(a, value);	}, 1000);	}	else if (value[0] === 3) {	$("#end h1").html("Draw");	setTimeout(function() {	showResult();	resetGame(a, value);	}, 1000);	}
}
function checkFilled(a) {	var i;	var count = 0;	for (i = 0; i < 9; i++) {	if (a[i] != 0) {	count++;	}	}	return count;
}
function resetGame(a, arr) {	$(".board div p").fadeOut(400, function() {	$('p').html("").fadeIn();	});	for (var i = 0; i < 9; i++) {	a[i] = 0;	}	arr[0] = 0;	console.log("Status: ", arr[0]);
}
Tic Tac Toe - Script Codes
Tic Tac Toe - Script Codes
Home Page Home
Developer Kw7oe
Username kw7oe
Uploaded November 26, 2022
Rating 3
Size 3,793 Kb
Views 14,168
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!

Kw7oe (kw7oe) 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!