bf17 icon

chess960 starting positions

bf17 | PRO | 07/22/23 02:53:28 PM UTC (Edited) | 0 ⭐ | 380 👁️ | Never ⏰ | []
Python |

1.3 KB

|

None

|

0 👍

/

0 👎

# python 3.4.3
# c960 - print out all 960 starting positions for Bobby Fischer's chess variant called Chess960.
 
from itertools import permutations
 
counter = 0
board = "BBNNQRKR"  #first valid position starting from BBKNNQRR
possible_board = ""
 
##########################################################################
# test if king is between the rooks
def rkr_test(pb):
    pb = pb.replace("B", "")
    pb = pb.replace("N", "")
    pb = pb.replace("Q", "")
    if(pb == "RKR"):
        return(True)
    else:
        return(False)
##########################################################################
# test if bishops are on opposite colors
def bishop_test(pb):
    first_bishop =  pb.find("B")
    second_bishop = pb.find("B", first_bishop + 1)
    if ((second_bishop - first_bishop) % 2 == 1):
        return(True)
    else:
        return(False)
##########################################################################
 
perms = [''.join(i) for i in permutations(board)]
sorted_unique_perms = sorted(set(perms))
 
print()
 
for i in range(0, len(sorted_unique_perms)):
    possible_board = sorted_unique_perms[i]
    if(rkr_test(possible_board)):
        if(bishop_test(possible_board)):
            counter += 1
            print(counter, "\t", possible_board)
print()

Comments