Tic Tac Toe Game
About this app
A fun and colorful Tic Tac Toe game.
Tic Tac Toe Game
Related apps
Put this on your site
Source
<html>
<head>
<meta charset="utf8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Tic Tac Toe Game</title>
<meta name="description" content="A fun and colorful Tic Tac Toe game.">
<meta name="keywords" content="Tic Tac Toe, game, fun, AI, colorful">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript">
try {
let board = Array(9).fill(null);
let currentPlayer = "X";
let winner = null;
function checkWin() {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return board[a];
}
}
return null;
}
function handleClick(i) {
if (board[i] || winner) {
return;
}
board[i] = currentPlayer;
currentPlayer = currentPlayer === "X" ? "O" : "X";
winner = checkWin();
if(currentPlayer === "O") {
computerMove();
}
}
function computerMove() {
let bestScore = -Infinity;
let move;
for (let i = 0; i < 9; i++) {
if (!board[i]) {
board[i] = 'O';
let score = minimax(board, 0, false);
board[i] = null;
if (score > bestScore) {
bestScore = score;
move = i;
}
}
}
handleClick(move);
}
function minimax(board, depth, isMaximizing) {
let result = checkWin();
if (result !== null) {
return result === 'O' ? 1 : -1;
}
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < 9; i++) {
if (!board[i]) {
board[i] = 'O';
let score = minimax(board, depth + 1, false);
board[i] = null;
bestScore = Math.max(score, bestScore);
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < 9; i++) {
if (!board[i]) {
board[i] = 'X';
let score = minimax(board, depth + 1, true);
board[i] = null;
bestScore = Math.min(score, bestScore);
}
}
return bestScore;
}
}
document.addEventListener("DOMContentLoaded", function() {
document.querySelectorAll(".cell").forEach((cell, i) => {
cell.addEventListener("click", () => {
handleClick(i);
cell.textContent = board[i];
});
});
});
} catch (error) {
throw error;
}
</script>
<style>
.cell {
width: 60px;
height: 60px;
border: 1px solid #000;
display: inline-block;
line-height: 60px;
text-align: center;
font-size: 2em;
margin: -1px;
}
.cell:nth-child(3n+1) {
clear: both;
}
.X {
color: red;
}
.O {
color: blue;
}
</style>
</head>
<body>
<div id="main-container" class="container">
<div id="tic-tac-toe-board">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
</div>
</body>
</html>
NEW APPS
These are apps made by the community!