Sure! I can help you create a simple online Tic Tac Toe game using HTML, CSS, and JavaScript all within a single file. Additionally, I'll provide a Python script to serve this game using a simple HTTP server.
1. Single-File Tic Tac Toe Game (HTML, CSS, JavaScript)
Below is the complete code for a Tic Tac Toe game encapsulated in a single HTML file. You can save this code as index.html.
HTML Structure: The game consists of a title, a status text to indicate whose turn it is or who won, a 3x3 grid for the game board, and a reset button.
CSS Styling: The game board is styled using CSS Grid for layout. Each cell has a hover effect to enhance user interaction.
JavaScript Logic:
cells: All the interactive cells in the grid.
currentPlayer: Tracks whose turn it is ('X' or 'O').
gameBoard: An array representing the state of the game.
winningConditions: All possible win conditions.
handleCellClick: Handles the user clicking on a cell, updates the game state, and checks for a win or draw.
checkResult: Determines if a player has won or if there's a draw.
resetGame: Resets the game to its initial state.
2. Python Script to Serve the Game
You can use Python's built-in http.server module to serve the HTML file. Here's a simple Python script that starts a local web server and serves the index.html file.
python
import http.server
import socketserver
import os
PORT = 8000
DIRECTORY = os.path.dirname(os.path.abspath(__file__))
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
if __name__ == "__main__":
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Serving at http://localhost:{PORT}")
print("Press Ctrl+C to stop the server.")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
How to Use the Python Script:
Save the Python Script: Save the above Python code in a file named serve.py in the same directory where your index.html is located.
Run the Server:
Open a terminal or command prompt.
Navigate to the directory containing serve.py and index.html.
Run the server with the command:
bash
python serve.py
Make sure you have Python 3 installed. If python points to Python 2 on your system, you might need to use python3 instead:
bash
python3 serve.py
Access the Game:
Open your web browser.
Go to http://localhost:8000 to see your Tic Tac Toe game.
Stopping the Server:
Go back to the terminal where the server is running.
Press Ctrl+C to stop the server.
Alternative: Using Flask for Serving (Optional)
If you prefer using Flask, a lightweight web framework for Python, here's how you can set it up:
Step 1: Install Flask
If you don't have Flask installed, you can install it using pip:
bash
pip install Flask
Step 2: Create the Flask App
Create a Python script named app.py with the following content:
python
from flask import Flask, send_from_directory
import os
app = Flask(__name__)
# Define the directory containing the index.html
DIRECTORY = os.path.dirname(os.path.abspath(__file__))
@app.route('/')
def serve_game():
return send_from_directory(DIRECTORY, 'index.html')
if __name__ == "__main__":
app.run(debug=True, port=8000)
Step 3: Run the Flask App
In the terminal, navigate to the directory containing app.py and index.html, then run:
bash
python app.py
Step 4: Access the Game
Open your web browser and go to http://localhost:8000 to play the Tic Tac Toe game.
Conclusion
You now have a simple Tic Tac Toe game that you can run locally on your machine. The single HTML file contains all the necessary code for the game, and the Python script serves it to your browser. You can further enhance the game by adding features like player score tracking, AI opponents, or even deploying it to a web server for online access.
If you have any questions or need further assistance, feel free to ask!