Sure! I'll provide you with a simple Tic Tac Toe game contained within a single HTML file (including HTML, CSS, and JavaScript) and a Python script to serve this file using Flask.
1. Tic Tac Toe Game (tic_tac_toe.html)
Save the following code as tic_tac_toe.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Online Tic Tac Toe</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
#game {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 5px;
justify-content: center;
margin: 0 auto;
}
.cell {
width: 100px;
height: 100px;
background-color: #f2f2f2;
font-size: 64px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
.cell:hover {
background-color: #e6e6e6;
}
#status {
margin-top: 20px;
font-size: 24px;
}
#reset {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Online Tic Tac Toe</h1>
<div id="game">
<div class="cell" data-index="0"></div>
<div class="cell" data-index="1"></div>
<div class="cell" data-index="2"></div>
<div class="cell" data-index="3"></div>
<div class="cell" data-index="4"></div>
<div class="cell" data-index="5"></div>
<div class="cell" data-index="6"></div>
<div class="cell" data-index="7"></div>
<div class="cell" data-index="8"></div>
</div>
<div id="status">Player X's turn</div>
<button id="reset">Reset Game</button>
<script>
const cells = document.querySelectorAll('.cell');
const statusText = document.getElementById('status');
const resetButton = document.getElementById('reset');
let currentPlayer = 'X';
let gameState = ["", "", "", "", "", "", "", "", ""];
let isGameActive = true;
const winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
function handleCellClick(event) {
const clickedCell = event.target;
const cellIndex = parseInt(clickedCell.getAttribute('data-index'));
if (gameState[cellIndex] !== "" || !isGameActive) {
return;
}
updateCell(clickedCell, cellIndex);
checkResult();
}
function updateCell(cell, index) {
gameState[index] = currentPlayer;
cell.textContent = currentPlayer;
}
function switchPlayer() {
currentPlayer = currentPlayer === "X" ? "O" : "X";
statusText.textContent = `Player ${currentPlayer}'s turn`;
}
function checkResult() {
let roundWon = false;
for (let i = 0; i < winningConditions.length; i++) {
const [a, b, c] = winningConditions[i];
if (gameState[a] && gameState[a] === gameState[b] && gameState[a] === gameState[c]) {
roundWon = true;
break;
}
}
if (roundWon) {
statusText.textContent = `Player ${currentPlayer} wins!`;
isGameActive = false;
return;
}
if (!gameState.includes("")) {
statusText.textContent = "It's a draw!";
isGameActive = false;
return;
}
switchPlayer();
}
function resetGame() {
currentPlayer = 'X';
gameState = ["", "", "", "", "", "", "", "", ""];
isGameActive = true;
statusText.textContent = `Player ${currentPlayer}'s turn`;
cells.forEach(cell => cell.textContent = "");
}
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
resetButton.addEventListener('click', resetGame);
</script>
</body>
</html>
2. Python Server Script (server.py)
We'll use Flask, a lightweight web framework for Python, to serve the HTML file. First, ensure you have Flask installed. You can install it using pip:
pip install Flask
Save the following code as server.py in the same directory as tic_tac_toe.html:
from flask import Flask, send_from_directory
app = Flask(__name__)
@app.route('/')
def serve_game():
return send_from_directory('.', 'tic_tac_toe.html')
if __name__ == '__main__':
app.run(debug=True)
3. Running the Game
-
Setup:
- Ensure both
tic_tac_toe.html and server.py are in the same directory.
-
Install Flask:
- If you haven't installed Flask yet, open your terminal or command prompt and run:
pip install Flask
-
Start the Server:
-
Access the Game:
- Open your web browser and go to
http://127.0.0.1:5000/.
- You should see the Tic Tac Toe game interface.
4. Making the Game Accessible Online
To make the game accessible over the internet, you'll need to deploy the Flask app to a hosting service that supports Python applications, such as Heroku, PythonAnywhere, or AWS Elastic Beanstalk. Here's a brief overview using Heroku:
-
Install Heroku CLI:
- Follow the instructions on Heroku CLI to install it.
-
Prepare Your Application:
- Create a
requirements.txt file listing Flask as a dependency:
Flask
- Create a
Procfile to specify the command to run the app:
web: python server.py
-
Deploy to Heroku:
Note: Deploying applications online may incur costs and requires adherence to the hosting provider's policies. Always secure your applications appropriately when making them publicly accessible.
5. Enhancements
This is a basic implementation of Tic Tac Toe. Here are some ideas to enhance the game:
Feel free to expand upon this foundation to create a more robust and feature-rich game!