This project is a real-time Tic-Tac-Toe game built with FastAPI for the backend and React + Vite for the frontend. The application uses WebSockets to facilitate real-time updates between players.
WebSockets were chosen for this project to enable real-time communication between the client and server. Unlike traditional HTTP requests that require continuous polling to check for updates, WebSockets maintain a persistent connection that allows the server to push updates to the client immediately. This is crucial for a game like Tic-Tac-Toe, where real-time updates ensure a smooth and responsive gaming experience.
The project is divided into two main parts: the backend and the frontend.
- main.py: This file contains the FastAPI application setup and the WebSocket endpoint to handle real-time communication.
- game.py: This file contains the logic for the Tic-Tac-Toe game, including the game state, player moves, and winner determination.
- App.jsx: This file contains the React component for the game interface, including the WebSocket client logic for communication with the backend.
- App.css: This file contains the styling for the game interface.
Ensure you have the following installed:
- Python 3.8 or higher
- Node.js and npm
Create a virtual environment and activate it:
python -m venv venvsource venv/bin/activate- Install the required Python packages:
pip install fastapi uvicorn - Navigate to the
/backenddirectory and run the FastAPI server:uvicorn main:app --reloadThe backend server will start at http://localhost:8000.
- Navigate to the frontend directory and install the npm packages:
npm install - Start the Vite development server:
npm run devThe frontend will start at http://localhost:3000.
- Ensure the backend server is running at http://localhost:8000.
- Open a browser and navigate to http://localhost:3000 to start playing the game.
- To set a move double click on a box.
- /backend:
game.py: Contains the game logic.main.py: Sets up the FastAPI server and handles Websockets.
- /frontend: standard Vite+React structure.
/srccontains the main pages and logic.