Talk:Generate Chess960 starting position: Difference between revisions

m (→‎random starting position: added a separator.)
Line 108:
 
As another aside, the chess symbols have been in Unicode for more than 20 years now. --[[User:TimToady|TimToady]] ([[User talk:TimToady|talk]]) 19:31, 8 May 2014 (UTC)
 
==Python: Correct by construction? ==
Originally wrong but corrected. This is the check:
<lang python>from pprint import pprint as pp
 
def check(start):
'Check that B/b are on different colours'
assert (start.index('b') % 2) != (start.index('B') % 2), ''.join(start)
 
def generate_all_bishpos():
start = list('______') # Should be all-but Bishops but any six chars will do
 
starts = []
for bishpos in range(len(start)+1):
start2 = start[::]
start2.insert(bishpos, 'B')
for bishpos2 in range(bishpos+1, len(start)+2, 2):
start3 = start2[::]
start3.insert(bishpos2, 'b')
check(start3)
starts.append(tuple(start3))
return starts
 
starts = sorted(generate_all_bishpos())
print('Generated all separations of Bishops:')
pp(starts, width=50)
 
import re
from random import choice
 
def random360():
start = ['R', 'K', 'R'] # Subsequent order unchanged by insertions.
#
for piece in ['Q', 'N', 'N']:
start.insert(choice(range(len(start))), 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()
 
def random360():
"Exact copy of the one above except other pieces are all '_'"
start = ['_', '_', '_'] # Subsequent order unchanged by insertions.
#
for piece in ['_', '_', '_']:
start.insert(choice(range(len(start))), 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('\nCheck we can generate all separations of bishops randomly:')
s, i = set(), 0
while len(s) < len(starts) and i < 9999:
start = random360()
check(start)
s.add(tuple(start))
i += 1
 
s = sorted(s)
 
if s == starts:
print(' Generated all the %i separations in %i attempts' % (len(starts), i))
else:
print(' Error! could not do it in %i' % i)</lang>
 
{{out}}
<pre>Generated all separations of Bishops:
[('B', '_', '_', '_', '_', '_', '_', 'b'),
('B', '_', '_', '_', '_', 'b', '_', '_'),
('B', '_', '_', 'b', '_', '_', '_', '_'),
('B', 'b', '_', '_', '_', '_', '_', '_'),
('_', 'B', '_', '_', '_', '_', 'b', '_'),
('_', 'B', '_', '_', 'b', '_', '_', '_'),
('_', 'B', 'b', '_', '_', '_', '_', '_'),
('_', '_', 'B', '_', '_', '_', '_', 'b'),
('_', '_', 'B', '_', '_', 'b', '_', '_'),
('_', '_', 'B', 'b', '_', '_', '_', '_'),
('_', '_', '_', 'B', '_', '_', 'b', '_'),
('_', '_', '_', 'B', 'b', '_', '_', '_'),
('_', '_', '_', '_', 'B', '_', '_', 'b'),
('_', '_', '_', '_', 'B', 'b', '_', '_'),
('_', '_', '_', '_', '_', 'B', 'b', '_'),
('_', '_', '_', '_', '_', '_', 'B', 'b')]
 
Check we can generate all separations of bishops randomly:
Generated all the 16 separations in 55 attempts</pre>
Anonymous user