How to resolve the algorithm Generate Chess960 starting position step by step in the Python programming language
How to resolve the algorithm Generate Chess960 starting position step by step in the Python programming language
Table of Contents
Problem Statement
Chess960 is a variant of chess created by world champion Bobby Fischer. Unlike other variants of the game, Chess960 does not require a different material, but instead relies on a random initial position, with a few constraints:
With those constraints there are 960 possible starting positions, thus the name of the variant.
The purpose of this task is to write a program that can randomly generate any one of the 960 Chess960 initial positions. You will show the result as the first rank displayed using either the chess symbols in Unicode (♔♕♖♗♘), the letters King Queen Rook Bishop kNight, or the corresponding letters in a language other than English.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Generate Chess960 starting position step by step in the Python programming language
itertools.permutations
: Generates all possible permutations of a given iterable.
Bishop constraint
: Ensures that bishops are placed on opposite colors (e.g., one on a white square, the other on a black square).
King constraint
: Enforces that the king is between the rooks, either in the middle or on the flanks.
re.compile
: Creates a regular expression object to search for patterns in a string.
random960
: Generates a random starting position for 960 chess.
generate960
: Generates all 960 possible starting positions for 960 chess.
Here's a step-by-step breakdown of the code:
-
Generating Possible Starting Positions:
- Using
permutations
and constraints, both snippets generate a set of all possible starting positions for 960 chess. These positions satisfy the bishop and king placement rules.
- Using
-
Random Position Generator (
random960
):- This function takes a random approach to generating a single starting position. It randomly inserts the QNN pieces and bishops into the initial RKR configuration.
-
Comprehensive Position Generator (
generate960
):- This function systematically generates all 960 starting positions. It starts with RKR and iteratively inserts the QNN pieces. Then, it inserts bishops in all possible ways while adhering to the bishop and king constraints. The result is a list containing all 960 possible starting positions.
-
Printing Random Position:
- The code prints a random starting position by selecting one randomly from the list generated by
generate960
.
- The code prints a random starting position by selecting one randomly from the list generated by
In summary, this code generates starting positions for 960 chess, a variant that eliminates the opening advantage of white by randomizing the starting arrangement of pieces on the board. It has a systematic approach (generate960
) and a random approach (random960
), both resulting in the same set of 960 possible starting positions.
Source code in the python programming language
>>> from itertools import permutations
>>> pieces = 'KQRrBbNN'
>>> starts = {''.join(p).upper() for p in permutations(pieces)
if p.index('B') % 2 != p.index('b') % 2 # Bishop constraint
and ( p.index('r') < p.index('K') < p.index('R') # King constraint
or p.index('R') < p.index('K') < p.index('r') ) }
>>> len(starts)
960
>>> starts.pop()
'QNBRNKRB'
>>>
>>> import re
>>> pieces = 'KQRRBBNN'
>>> bish = re.compile(r'B(|..|....|......)B').search
>>> king = re.compile(r'R.*K.*R').search
>>> starts3 = {p for p in (''.join(q) for q in permutations(pieces))
if bish(p) and king(p)}
>>> len(starts3)
960
>>> starts3.pop()
'QRNKBNRB'
>>>
from random import choice
def random960():
start = ['R', 'K', 'R'] # Subsequent order unchanged by insertions.
#
for piece in ['Q', 'N', 'N']:
start.insert(choice(range(len(start)+1)), piece)
#
bishpos = choice(range(len(start)+1))
start.insert(bishpos, 'B')
start.insert(choice(range(bishpos + 1, len(start) + 1, 2)), 'B')
return start
return ''.join(start).upper()
print(random960())
from random import choice
def generate960():
start = ('R', 'K', 'R') # Subsequent order unchanged by insertions.
# Insert QNN in all combinations of places
starts = {start}
for piece in ['Q', 'N', 'N']:
starts2 = set()
for s in starts:
for pos in range(len(s)+1):
s2 = list(s)
s2.insert(pos, piece)
starts2.add(tuple(s2))
starts = starts2
# For each of the previous starting positions insert the bishops in their 16 positions
starts2 = set()
for s in starts:
for bishpos in range(len(s)+1):
s2 = list(s)
s2.insert(bishpos, 'B')
for bishpos2 in range(bishpos+1, len(s)+2, 2):
s3 = s2[::]
s3.insert(bishpos2, 'B')
starts2.add(tuple(s3))
return list(starts2)
gen = generate960()
print(''.join(choice(gen)))
You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the C# programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the Racket programming language
You may also check:How to resolve the algorithm Extend your language step by step in the Idris programming language
You may also check:How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the C++ programming language
You may also check:How to resolve the algorithm Assertions step by step in the 11l programming language