Tic-Tac-Toe

Developer
Size
4,853 Kb
Views
16,192

How do I make an tic-tac-toe?

Tic-Tac-Toe.. What is a tic-tac-toe? How do you make a tic-tac-toe? This script and codes were developed by Mihkel on 08 December 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" href="https://fonts.googleapis.com/css?family=Roboto:400,700"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="container"> <h2 class="notification"></h2> <div class="grid"> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> </div> <div class="stats"> <h4>You: <span class="player">0</span></h4> <h4>Opponent: <span class="opponent">0</span></h4> <h4>Draw: <span class="draw">0</span></h4> </div>
</div>
<div class="modal"> <div class="modal-content"> <div class="modal-box difficulty-container"> <div class="modal-choice difficulty">EASY</div> <div class="modal-choice difficulty">HARD</div> </div> <div class="modal-box choice-container hidden"> <div class="modal-choice tile">X</div> <div class="modal-choice tile">O</div> </div> </div>
</div> <script src="js/index.js"></script>
</body>
</html>

Tic-Tac-Toe - Script Codes CSS Codes

*:before,
*:after,
* {	box-sizing: border-box;
}
body {	background: -webkit-linear-gradient(left, rgba(188,224,238,1) 0%, rgba(41,184,229,1) 50%, rgba(188,224,238,1) 100%);	background: linear-gradient(to right, rgba(188,224,238,1) 0%, rgba(41,184,229,1) 50%, rgba(188,224,238,1) 100%);	font-family: 'Roboto', sans-serif;	color: #333;	margin: 0;	padding: 0;
}
/* GRID */
.container {	margin: 10vh auto 0 auto;	width: 420px;
}
.grid {	display: -webkit-box;	display: -ms-flexbox;	display: flex;	-webkit-box-orient: horizontal;	-webkit-box-direction: normal; -ms-flex-direction: row; flex-direction: row;	-ms-flex-wrap: wrap; flex-wrap: wrap;
}
.square {	display: -webkit-box;	display: -ms-flexbox;	display: flex;	-webkit-box-pack: center; -ms-flex-pack: center; justify-content: center;	-webkit-box-align: center; -ms-flex-align: center; align-items: center;	width: 33.3333%;	height: 140px;	font-size: 120px;	font-weight: 700;	cursor: pointer;	color: #fafafa;	background-color: rgba(255, 152, 0, 0);	-webkit-transition: background-color 500ms;	transition: background-color 500ms; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;
}
.square.highlight {	background-color: rgba(255, 152, 0, 0.9);
}
.square.player {	color: #333;
}
.content {	position: absolute;
}
.square:nth-child(1),
.square:nth-child(2),
.square:nth-child(3),
.square:nth-child(4),
.square:nth-child(5),
.square:nth-child(6) {	border-bottom: 6px solid #0085B2;
}
.square:first-child,
.square:nth-child(2),
.square:nth-child(4),
.square:nth-child(5),
.square:nth-child(7),
.square:nth-child(8) {	border-right: 6px solid #0085B2;
}
/* NOTIFICATION */
.notification {	text-align: center;	color: #fafafa;	font-weight: 100;	letter-spacing: 0.8px;	font-size: 26px;	background-color: orange;	border-radius: 5px;	padding: 10px;	box-shadow: 0 0 10px rgba(0,0,0,0.4);	min-height: 52px;	opacity: 0;	-webkit-transform: scale(0); transform: scale(0);	-webkit-transform-origin: center; transform-origin: center;	-webkit-transition: all 200ms;	transition: all 200ms;
}
.notification.show {	opacity: 1;	-webkit-transform: scale(1); transform: scale(1);
}
/* STATS */
.stats {	display: none;	-webkit-box-orient: horizontal;	-webkit-box-direction: normal; -ms-flex-direction: row; flex-direction: row;	-webkit-box-pack: justify; -ms-flex-pack: justify; justify-content: space-between;	color: #0085B2;
}
/* MODAL */
.modal {	position: fixed;	top: 0;	right: 0;	bottom: 0;	left: 0;	background-color: rgba(0,0,0,0);	-webkit-transition: all 200ms;	transition: all 200ms;	pointer-events: none; opacity: 0;
}
.modal-content {	position: absolute;	top: -10%;	left: 50%;	-webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%);	width: 40vw;	background-color: #fafafa;	box-shadow: 0 0 20px rgba(0,0,0,0.4);	-webkit-transition: all 200ms;	transition: all 200ms;
}
.modal.show-modal {	background-color: rgba(0,0,0,0.4);	pointer-events: all; opacity: 1;
}
.show-modal .modal-content {	top: 10%;
}
.modal-box {	display: -webkit-box;	display: -ms-flexbox;	display: flex;	-webkit-box-orient: horizontal;	-webkit-box-direction: normal; -ms-flex-direction: row; flex-direction: row;
}
.modal-box.hidden {	display: none;
}
.modal-choice { display: -webkit-box; display: -ms-flexbox; display: flex;	-webkit-box-pack: center; -ms-flex-pack: center; justify-content: center;	-webkit-box-align: center; -ms-flex-align: center; align-items: center;	width: 50%;	height: 100px;	-webkit-transition: all 200ms;	transition: all 200ms;	font-size: 56px;	font-weight: 700;	cursor: pointer;
}
.modal-choice:hover {	background-color: rgba(41,184,229,1);	color: #fafafa;
}
/* MEDIA QUERIES */
@media all and (max-width: 768px) {	.modal-content {	width: 80vw;	}
}
@media all and (max-width: 420px) {	.container {	width: 300px;	}	.square {	height: 100px;	font-size: 80px;	}	.modal-choice {	font-size: 38px;	}
}

Tic-Tac-Toe - Script Codes JS Codes

var modal = {	init: function () {	this.cacheDom();	this.addEventListener();	},	cacheDom: function () {	this.el = document.querySelector(".modal");	this.difficultyChoices = Array.prototype.slice.call(document.querySelectorAll(".modal-choice.difficulty"));	this.tileChoices = Array.prototype.slice.call(document.querySelectorAll(".modal-choice.tile"));	this.difficultyContainer = document.querySelector(".difficulty-container");	this.tileChoiceContainer = document.querySelector(".choice-container");	},	show: function () {	this.el.classList.add("show-modal");	},	hide: function () {	this.el.classList.remove("show-modal");	},	hideDifficulty: function () {	this.difficultyContainer.classList.add("hidden");	},	showTileChoice: function () {	this.tileChoiceContainer.classList.remove("hidden");	},	addEventListener: function () {	this.tileChoices.forEach(function (choice) {	choice.addEventListener("click", game.tileChoice.bind(game));	});	this.difficultyChoices.forEach(function (choice) {	choice.addEventListener("click", game.difficultyChoice.bind(game));	});	}
};
var game = {	init: function () {	this.board = [0,0,0,0,0,0,0,0,0];	this.winPatterns = [	[0,1,2], [3,4,5], [6,7,8],	[0,3,6], [1,4,7], [2,5,8],	[0,4,8], [2,4,6]	];	this.PLAYER = 1;	this.AI = 2;	this.EMPTY = 0;	this.PLAYER_TILE;	this.DIFFICULTY;	this.bestMove;	this.isPlayerTurn = false;	this.cacheDom();	this.addEventListeners();	},	cacheDom: function () {	this.squares = Array.prototype.slice.call(document.querySelectorAll(".square"));	this.notification = document.querySelector(".notification");	},	renderBoard: function () {	for (var i = 0; i < this.board.length; i++) {	var tile = this.board[i];	var el = this.squares[i];	switch (tile) {	case this.PLAYER:	tile = this.PLAYER_TILE;	break;	case this.AI:	tile = (this.PLAYER_TILE === "X") ? "O" : "X";	break;	case this.EMPTY:	tile = "";	break;	}	if (el.textContent !== tile) {	el.textContent = tile;	}	}	},	tileChoice: function (e) {	var choice = e.target.textContent;	if (choice !== "X" && choice !== "O") {	choice = "X";	}	this.PLAYER_TILE = choice;	modal.hide();	stats.show(); setTimeout(function () {	this.startGame();	}.bind(this), 200);	},	difficultyChoice: function (e) {	var difficulty = e.target.textContent;	if (difficulty !== "EASY" && difficulty !== "HARD") {	difficulty = "HARD";	}	this.DIFFICULTY = difficulty;	modal.hideDifficulty();	modal.showTileChoice();	},	addEventListeners: function () {	this.squares.forEach(function (square) {	square.addEventListener("click", this.playerMove.bind(this));	}.bind(this));	},	startGame: function () {	if (this.PLAYER_TILE === "X") {	this.isPlayerTurn = true;	} else {	this.aiTurn();	}	},	notificate: function (msg) {	this.notification.classList.toggle("show");	this.notification.textContent = msg;	},	clearSquareClasses: function () {	this.squares.forEach(function (square) {	if (square.classList.contains("player")) {	square.classList.remove("player");	}	});	},	playerMove: function (e) {	if (e.target.textContent || !this.isPlayerTurn) {	return;	}	this.isPlayerTurn = false;	var i = Array.prototype.indexOf.call(this.squares, e.target);	this.board[i] = this.PLAYER;	e.target.classList.add("player");	this.renderBoard();	if (!this.isOver()) { setTimeout(function () { this.aiTurn(); }.bind(this), 200);	} else {	this.newGame();	}	},	checkVictory: function (player, board) {	return this.winPatterns.some(function (pattern) {	return pattern.every(function (tile) {	return board[tile] === player;	}.bind(this));	}.bind(this));	},	isFull: function (board) {	return board.every(function (tile) {	return tile !== this.EMPTY;	}.bind(this));	},	availableMoves: function (board) {	return board.reduce(function (free, tile, i) {	if (tile === this.EMPTY) {	free.push(i);	}	return free;	}.bind(this), []);	},	minimax: function (board, player, depth) {	if (this.checkVictory(this.AI, board)) {	return 10 - depth;	} else if (this.checkVictory(this.PLAYER, board)) {	return depth - 10;	} else if (this.isFull(board)) {	return 0;	}	++depth;	var scores = [];	var moves = [];	var opponent = (player === this.AI) ? this.PLAYER : this.AI;	var availableMoves = this.availableMoves(board);	availableMoves.forEach(function (move) {	board[move] = player;	scores.push(this.minimax(board, opponent, depth));	moves.push(move);	board[move] = this.EMPTY;	}.bind(this));	if (player === this.AI) {	var bestScore = -Number.MAX_VALUE;	for (var i = 0; i < scores.length; i++) {	if (scores[i] > bestScore) {	bestScore = scores[i];	this.bestMove = moves[i];	}	}	return bestScore;	} else {	var bestScore = Number.MAX_VALUE;	for (var i = 0; i < scores.length; i++) {	if (scores[i] < bestScore) {	bestScore = scores[i];	this.bestMove = moves[i];	}	}	return bestScore;	}	},	isOver: function () {	return this.isFull(this.board) || this.checkVictory(this.AI, this.board) || this.checkVictory(this.PLAYER, this.board);	},	aiTurn: function () {	if (this.DIFFICULTY === "HARD") {	this.minimax(this.board, this.AI, 0);	this.board[this.bestMove] = this.AI;	} else {	var moves = this.availableMoves(this.board);	var move = moves[Math.floor(Math.random() * moves.length)];	this.board[move] = this.AI;	}	this.renderBoard();	if (!this.isOver()) { setTimeout(function () { this.isPlayerTurn = true; }.bind(this), 200);	} else {	this.newGame();	}	},	getWinner: function () {	if (this.checkVictory(this.AI, this.board)) {	return this.AI;	} else if (this.checkVictory(this.PLAYER, this.board)) {	return this.PLAYER;	} else {	return "DRAW";	}	},	getWinPattern: function (player) {	return this.winPatterns.filter(function (pattern) {	return pattern.every(function (tile) {	return this.board[tile] === player;	}.bind(this));	}.bind(this))[0];	},	highlightPattern: function () {	var winner = this.getWinner();	if (winner === "DRAW") {	return;	}	var pattern = this.getWinPattern(winner);	pattern.forEach(function (tile) {	this.squares[tile].classList.add("highlight");	}.bind(this));	setTimeout(function () {	pattern.forEach(function (tile) {	this.squares[tile].classList.remove("highlight");	}.bind(this));	}.bind(this), 1750);	},	newGame: function () {	var winner = this.getWinner();	var msg;	this.highlightPattern();	switch (winner) {	case this.AI:	msg = "You lost!";	break;	case this.PLAYER:	msg = "You won!";	break;	case "DRAW":	msg = "It's a draw!";	break;	}	stats.increment(winner);	this.notificate(msg);	this.reset();	},	reset: function () {	setTimeout(function () {	this.board = [0,0,0,0,0,0,0,0,0];	this.clearSquareClasses();	this.bestMove = null;	this.notificate("");	this.renderBoard();	if (this.PLAYER_TILE === "X") {	this.isPlayerTurn = true;	} else {	setTimeout(function () {	this.aiTurn();	}.bind(this), 200);	}	}.bind(this), 2000);	}
};
var stats = {	init: function () {	this.cacheDom();	this.player = 0;	this.opp = 0;	this.draw = 0;	},	cacheDom: function () {	this.el = document.querySelector(".stats");	this.playerEl = document.querySelector(".stats .player");	this.oppEl = document.querySelector(".stats .opponent");	this.drawEl = document.querySelector(".stats .draw");	},	show: function () {	this.el.style.display = "flex";	},	increment: function (player) {	switch (player) {	case 1:	this.player++	this.playerEl.textContent = this.player;	break;	case 2:	this.opp++	this.oppEl.textContent = this.opp;	break;	case "DRAW":	this.draw++	this.drawEl.textContent = this.draw;	break;	}	}
};
game.init();
modal.init();
stats.init();
window.onload = modal.show.bind(modal);
Tic-Tac-Toe - Script Codes
Tic-Tac-Toe - Script Codes
Home Page Home
Developer Mihkel
Username Krokodill
Uploaded December 08, 2022
Rating 3
Size 4,853 Kb
Views 16,192
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!

Mihkel (Krokodill) Script Codes
Create amazing blog posts 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!