Chess960 position calc and enumerate

Post Reply
User avatar
MarkP
Posts: 71
Joined: Tue Apr 07, 2015 9:32 pm

Chess960 position calc and enumerate

Post by MarkP »

This is a quick and dirty little program that calculates and lists the valid opening positions for the game of “chess 960”.

There are 960 different opening positions for the variant of chess called "Fischer Random Chess", "Chess960" or "Chess9LX".

Because this list of 960 variations is longer than the SmartBasic output line limit of 250, a technique is used to circumvent the sB line limit.

Normally, PRINT statements without a trailing ";" cause a LF character to terminate the line, which sB uses to count lines to the print buffer. If this is exceeded, older lines are discarded.

Instead of printing lines with a LF or CR/LF (hex 0D/0A, decimal 13,10) terminator, which would result in the sB line limit being exceeded, all lines are terminated with only a chr$(13), which circumvents the line limit.

This method should be considered a feature of SmartBasic, to be used sparingly by programmers who know what they are doing and are aware of the risks of causing a runaway print buffer.

Note that the standard chess position is listed as #314.

The "collisions" value is a measure of how efficient this algorithm is - the smaller the better.

Code: Select all

rem enumerate all opening positions for chess960

option base 1
dim pos$(8)

for r1=1 to 6 ' LEFT ROOK
' dont check for already occupied
pos$(r1)="R"
for r2=r1+2 to 8 ' RIGHT ROOK
' dont check for already occupied
pos$(r2)="R"
for k=r1+1 to r2-1 ' KING
' dont check for already occupied
pos$(k)="K"
for b1=1 to 7 step 2 ' ODD BISHOP
if pos$(b1)<>"" then 
coll+=1
continue
endif
pos$(b1)="B"
for b2=2 to 8 step 2 'EVEN BISHOP
if pos$(b2)<>"" then 
coll+=1
continue
endif
pos$(b2)="B"
for q=1 to 8 ' QUEEN
if pos$(q)<>"" then 
coll+=1
continue
endif
pos$(q)="Q"
' now fill 2 empty squares with N
for i=1 to 8 ' KNIGHTS
if pos$(i)="" then pos$(i)="N"
next i
count+=1 
print str$(count);" ";
layout$=""

for i=1 to 8
layout$&=pos$(i)
next i

print layout$;
if layout$="RNBQKBNR" then print " standard pos";
print chr$(13);

for i=1 to 8
if pos$(i)="N" then pos$(i)=""
next i

pos$(q)=""
next q

pos$(b2)=""
next b2

pos$(b1)=""
next b1

pos$(k)=""
next k

pos$(r2)=""
next r2

pos$(r1)=""
next r1
 
print "position count:";count
print "collisions:";coll
end


User avatar
motoom
Posts: 1
Joined: Sat Jul 06, 2013 3:00 pm

Re: Chess960 position calc and enumerate

Post by motoom »

MarkP wrote:
Thu Sep 29, 2022 10:56 pm
Note that the standard chess position is listed as #314.
What a coincidence that π=3.14

Greetings,

Post Reply