This is my solution to the eight queens puzzle (and the more general n queens problem).
This project uses Poetry for dependency management. To install dependencies simply run:
poetry install
Note that this application depends on psycopg2cffi and is therefore subject to its build and runtime prerequisites.
To compute and visualize the solutions run the main
submodule inside the project package and pass the number of queens as an argument (defaults to 8 if not
provided):
python -m eight_queens_puzzle.main [QUEEN_NUMBER]
To compute and write the solutions to a PostgreSQL database run the project package as a module following the same pattern as above:
python -m eight_queens_puzzle [QUEEN_NUMBER]
And lastly, the view_db_solutions.py
script can retrieve stored solutions from the database and display them. E.g.:
python view_db_solutions.py [QUEEN_NUMBER] | less
The parameters for the connection to the database are to be configured by the use of libpq environment variables. These can be placed in an .env
file
for convenience. Further details can be found in the SQLAlchemy docs.
Tests are located under tests/
and are run with pytest.
A Dockerfile
and a Compose file are provided to ease development/deployment.
The following commands will drop you into a shell where you can run tests and code that don't depend on a database connection:
docker build -t eight-queens-puzzle .
docker run -it eight-queens-puzzle
To spin up a PostgreSQL instance with data persisted to a volume:
export POSTGRES_PASSWORD=create-db-with-this-password
docker compose up -d
The above will also create an instance of the application and write the solutions for a standard 8x8 chessboard to the database. To run further commands, such as those in the usage section (or drop into a shell before doing so):
docker compose run app [COMMAND] [ARGS...]