Skip to main content

nqueenplay

    1   2   3   4   5   6   7   8 
  ---------------------------------
8 |   |   | Q |   |   |   |   |   | 8
  ---------------------------------
7 |   |   |   |   | Q |   |   |   | 7
  ---------------------------------
6 |   |   |   |   |   |   |   | Q | 6
  ---------------------------------
5 |   |   |   | Q |   |   |   |   | 5
  ---------------------------------
4 | Q |   |   |   |   |   |   |   | 4
  ---------------------------------
3 |   |   |   |   |   |   | Q |   | 3
  ---------------------------------
2 |   | Q |   |   |   |   |   |   | 2
  ---------------------------------
1 |   |   |   |   |   | Q |   |   | 1
  ---------------------------------
    1   2   3   4   5   6   7   8 

nqueenplay is N-Queens puzzle player. This tool will generate randomly or randomly locked puzzle, you may use this as "a boxing bag" to practice problem solving algorithm.

GitHub GitHub repo size GitHub contributors

Installation

This python package available on pip installation using

pip install nqueenplay

Requirements

Available on Python 3

Documentation

A to Z explanations to use this tool

Generate Random Puzzle

To generate a random puzzle you can do:

N = 4 # any integer
nqueens = NQueen(N)

or

N = 4 # any integer
nqueens = NQueen(n=N, number_lock=0) # 0 = no lock

Generate Random Locked Puzzle

Random locked mean the distribution of queen is randomize, and there is locking mechanism to make sure the queen position won't change for another run. To generate a random locked puzzle you can do:

N = 4 # any integer
lock = 1 # any integer
nqueens = NQueen(n=N, number_lock=lock)

Get Number of Queens

To get number of queen:

N = 4
nqueens = NQueen(n=N, number_lock=1)
number_of_queen = nqueens.get_number_of_queens()

Output:

print(number_of_queen)
# 4

Get Queens Position

To get queen position:

N = 4
nqueens = NQueen(n=N, number_lock=1)
positions = nqueens.get_queen_positions()

Output:

print(positions)
# [(1, 1), (2, 4), (3, 3), (4, 2)]

Each tuple is The Queen coordinate, there is 4 attack pairs

Check Number of Attack Pairs

To check how many attack pairs in the current board:

N = 4
nqueens = NQueen(n=N, number_lock=1)
pairs = nqueens.get_attack_pairs()

Output:

print(pairs) 
# [[(1, 1), (3, 3)], [(2, 4), (3, 3)], [(2, 4), (4, 2)], [(3, 3), (4, 2)]]

Each tuple is The Queen coordinate, there is 4 attack pairs

Show The Board

To show the current board:

N = 4
nqueens = NQueen(n=N, number_lock=1)
nqueens.show()

output:

    1   2   3   4 
  -----------------
4 |   | Q |   |   | 4
  -----------------
3 |   |   | Q |   | 3
  -----------------
2 |   |   |   | Q | 2
  -----------------
1 | Q |   |   |   | 1
  -----------------
    1   2   3   4 

Show The Attack Pairs

To show the current board attack pairs:

N = 4
nqueens = NQueen(n=N, number_lock=1)
nqueens.show_attack_pairs()

output

# attack_pairs [(1, 3), (2, 3), (2, 4), (3, 4)]
# Number of attacking pair(s): 4

Move The Queen

Queen is column locked, so you can only move one queen to a different row

Move the queen upside

Move Queen to upside with specific range:

N = 4
nqueens = NQueen(n=N, number_lock=1)
nqueens.show()
nqueens.move_up(queen_pos=1, movement_length=2)
nqueens.show()

output:

    1   2   3   4 
  -----------------
4 |   | Q |   |   | 4
  -----------------
3 |   |   | Q |   | 3
  -----------------
2 |   |   |   | Q | 2
  -----------------
1 | Q |   |   |   | 1
  -----------------
    1   2   3   4 
    1   2   3   4 
  -----------------
4 |   | Q |   |   | 4
  -----------------
3 | Q |   | Q |   | 3
  -----------------
2 |   |   |   | Q | 2
  -----------------
1 |   |   |   |   | 1
  -----------------
    1   2   3   4 

Move the queen downside

Move Queen to downside with specific range:

N = 4
nqueens = NQueen(n=N, number_lock=1)
nqueens.show()
nqueens.move_down(queen_pos=2, movement_length=2)
nqueens.show()

output:

    1   2   3   4 
  -----------------
4 |   | Q |   |   | 4
  -----------------
3 |   |   | Q |   | 3
  -----------------
2 |   |   |   | Q | 2
  -----------------
1 | Q |   |   |   | 1
  -----------------
    1   2   3   4 
    1   2   3   4 
  -----------------
4 |   |   |   |   | 4
  -----------------
3 |   |   | Q |   | 3
  -----------------
2 |   | Q |   | Q | 2
  -----------------
1 | Q |   |   |   | 1
  -----------------
    1   2   3   4 

Move the queen on specific neighbor

Move Queen to a specific neighbor by neighbor row position:

N = 4
nqueens = NQueen(n=N, number_lock=1)
nqueens.show()
nqueens.move_to(queen_pos=2, target_pos=1)
nqueens.show()

output:

    1   2   3   4 
  -----------------
4 |   | Q |   |   | 4
  -----------------
3 |   |   | Q |   | 3
  -----------------
2 |   |   |   | Q | 2
  -----------------
1 | Q |   |   |   | 1
  -----------------
    1   2   3   4 
    1   2   3   4 
  -----------------
4 |   |   |   |   | 4
  -----------------
3 |   |   | Q |   | 3
  -----------------
2 |   |   |   | Q | 2
  -----------------
1 | Q | Q |   |   | 1
  -----------------
    1   2   3   4 

Move the queen to a random placement

Move Queen to random place in the column:

N = 4
nqueens = NQueen(n=N, number_lock=1)
nqueens.show()
nqueens.move_random(queen_pos=3)
nqueens.show()

output:

    1   2   3   4 
  -----------------
4 |   | Q |   |   | 4
  -----------------
3 |   |   | Q |   | 3
  -----------------
2 |   |   |   | Q | 2
  -----------------
1 | Q |   |   |   | 1
  -----------------
    1   2   3   4 
    1   2   3   4 
  -----------------
4 |   | Q |   |   | 4
  -----------------
3 |   |   |   |   | 3
  -----------------
2 |   |   | Q | Q | 2
  -----------------
1 | Q |   |   |   | 1
  -----------------
    1   2   3   4 

Free to use! Under MIT License.

Metadata

Release files for nqueenplay 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nqueenplay 0.0.6
File Size Uploaded Provenance Actions
nqueenplay-0.0.6.tar.gz 6.2 kB Without provenance Details for nqueenplay-0.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nqueenplay 0.0.6
File Interpreter ABI Platform Actions
nqueenplay-0.0.6-py3-none-any.whl Python 3 none any Details for nqueenplay-0.0.6-py3-none-any.whl

Total release size: 12.6 kB

Release files / nqueenplay-0.0.6.tar.gz

Download URL nqueenplay-0.0.6.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
94598fe16f0898202a8c89e19391135f251dd918543e9c1fa6fbe6b9c345ea1a
BLAKE2b-256 checksum
How to use checksums
6b75ed21c2cb82e7ce13300834a9e8a9fbbdb4c0733e33a6d9447dbc3959fe3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.9

Release files / nqueenplay-0.0.6-py3-none-any.whl

Download URL nqueenplay-0.0.6-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
61f4b0ebd0b248c76f7065751faa4e74c0b6da50ef830614fd6234de3cfede9c
BLAKE2b-256 checksum
How to use checksums
7f2ba57219ec587c6bcae7f8d7769fdd870af414e216d206c56808dee77e2bb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.9

Security for nqueenplay 0.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page