Calculator Tools Calculator Tools
Create

Tic Tac Toe

Oct 20, 2023

About this app

Play Tic Tac Toe against the computer

Tic Tac Toe Tic Tac Toe

Related apps

Put this on your site

Source

                    <html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">

<title>Tic Tac Toe</title>
<meta name="description" content="Play Tic Tac Toe against the computer">
<meta name="keywords" content="tic tac toe, game, computer, human">

<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 {
// App Javascript Goes Here. Place your entire script content inside the try block for error handling.
var currentPlayer = 'X';
var computerPlayer = 'O';
var gameOver = false;
var board = ['', '', '', '', '', '', '', '', ''];

// Function to check if the current player has won
function checkWin(player) {
if (
(board[0] === player && board[1] === player && board[2] === player) ||
(board[3] === player && board[4] === player && board[5] === player) ||
(board[6] === player && board[7] === player && board[8] === player) ||
(board[0] === player && board[3] === player && board[6] === player) ||
(board[1] === player && board[4] === player && board[7] === player) ||
(board[2] === player && board[5] === player && board[8] === player) ||
(board[0] === player && board[4] === player && board[8] === player) ||
(board[2] === player && board[4] === player && board[6] === player)
) {
gameOver = true;
return true;
}
return false;
}

// Function to check if the game is a draw
function checkDraw() {
for (var i = 0; i < board.length; i++) {
if (board[i] === '') {
return false;
}
}
gameOver = true;
return true;
}

// Function to make the computer's move
function makeComputerMove() {
var availableMoves = [];
for (var i = 0; i < board.length; i++) {
if (board[i] === '') {
availableMoves.push(i);
}
}
var randomIndex = Math.floor(Math.random() * availableMoves.length);
var computerMove = availableMoves[randomIndex];
board[computerMove] = computerPlayer;
$('#' + computerMove).text(computerPlayer);
$('#' + computerMove).addClass('text-primary');
currentPlayer = 'X';

if (checkWin(computerPlayer)) {
$('#message').text('Computer wins!');
} else if (checkDraw()) {
$('#message').text("It's a draw!");
}
}

// This will run when the DOM is ready.
$(document).ready(function () {
$('.cell').click(function () {
if (!gameOver && $(this).text() === '') {
var cellId = $(this).attr('id');
board[cellId] = currentPlayer;
$(this).text(currentPlayer);
$(this).addClass('text-danger');
if (checkWin(currentPlayer)) {
$('#message').text('You win!');
gameOver = true;
} else if (checkDraw()) {
$('#message').text("It's a draw!");
} else {
currentPlayer = 'O';
makeComputerMove();
}
}
});
});

} catch (error) {
// This will throw the error to the parent window.
throw error;
}
</script>

<style>
/* App CSS Goes Here */
.container {
text-align: center;
margin-top: 50px;
}

h1 {
font-family: 'Pacifico', cursive;
color: #ff6b6b;
margin-bottom: 30px;
}

.board {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
max-width: 300px;
margin: 0 auto;
}

.cell {
font-size: 30px;
padding: 10px;
text-align: center;
background-color: #f1f1f1;
cursor: pointer;
}

.cell:hover {
background-color: #e2e2e2;
}

#message {
font-family: 'Open Sans', sans-serif;
margin-top: 20px;
font-size: 18px;
}
</style>
</head>

<body>
<div id="main-container" class="container">
<h1>Tic Tac Toe</h1>
<div class="board">
<div id="0" class="cell"></div>
<div id="1" class="cell"></div>
<div id="2" class="cell"></div>
<div id="3" class="cell"></div>
<div id="4" class="cell"></div>
<div id="5" class="cell"></div>
<div id="6" class="cell"></div>
<div id="7" class="cell"></div>
<div id="8" class="cell"></div>
</div>
<div id="message"></div>
</div>
</body>

</html>