The 2048 Game API is a robust backend implementation of the popular 2048 puzzle game using FastAPI. This project provides a RESTful API that allows clients to create new games, make moves, and retrieve game states, enabling the development of various front-end interfaces or game bots.
The API is built with Python and FastAPI, offering high performance and easy-to-use endpoints for game management. It includes features such as concurrent game handling, move validation, and game state tracking. The project is designed with a focus on code quality, incorporating linting, type checking, and automated testing to ensure reliability and maintainability.
.
├── lefthook.yml
├── README.md
├── src
│ ├── __init__.py
│ ├── app
│ │ ├── __init__.py
│ │ ├── app.py
│ │ └── game.py
│ └── frontend
│ └── index.html
└── tests
├── __init__.py
└── test_app.py
Key Files:
src/app/app.py
: Main FastAPI application file containing API routes and game logic integration.src/app/game.py
: Implementation of the 2048 game logic.tests/test_app.py
: Comprehensive test suite for the API endpoints and game functionality.lefthook.yml
: Configuration file for the Lefthook Git hooks manager, setting up pre-commit checks.
Prerequisites:
- Python 3.7+
- Poetry (for dependency management)
To set up the project:
-
Clone the repository:
git clone <repository-url> cd 2048-game-api
-
Install dependencies using Poetry:
poetry install
-
Activate the virtual environment:
poetry shell
To start the FastAPI server:
uvicorn src.app.app:app --reload
The API will be available at http://localhost:8000
.
-
Create a new game:
POST /game/new
Response:
{ "game_id": "uuid", "state": { "board": [[0, 0, 2, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 2]], "score": 0, "game_over": false } }
-
Make a move:
POST /game/move
Request body:
{ "game_id": "uuid", "direction": "up" }
Response:
{ "moved": true, "state": { "board": [[2, 0, 2, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 2]], "score": 4, "game_over": false } }
-
Get game state:
GET /game/{game_id}
Response:
{ "board": [[2, 0, 2, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 2]], "score": 4, "game_over": false }
To run the test suite:
pytest
-
Issue: API returns 404 for all requests
- Ensure the server is running and you're using the correct URL.
- Check if the
STAGE
environment variable is set correctly.
-
Issue: Unable to make moves
- Verify that you're sending a valid
game_id
anddirection
in the request body. - Check if the game has ended (
game_over: true
in the state).
- Verify that you're sending a valid
-
Issue: Unexpected game behavior
- Enable debug mode by setting
STAGE=development
in your environment. - Check the server logs for detailed error messages.
- Enable debug mode by setting
For more detailed debugging:
- Set
STAGE=development
to enable verbose logging. - Inspect the logs in
stderr
for trace-level information.
The 2048 Game API follows a straightforward request-response flow:
- Client initiates a request (new game, move, or get state).
- FastAPI routes the request to the appropriate endpoint handler.
- The handler interacts with the
Game2048
class to perform game operations. - The game state is updated and stored in memory.
- The response is formatted and sent back to the client.
Client <-> FastAPI Router <-> Endpoint Handlers <-> Game2048 Class
^ ^
| |
+---------------------Memory Storage------------------+
Note: The current implementation stores game states in memory, which may not be suitable for production environments with high concurrency or persistence requirements.