Advertisement
bf17

chess960 starting positions

Mar 19th, 2017 (edited)
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. # python 3.4.3
  2. # c960 - print out all 960 starting positions for Bobby Fischer's chess variant called Chess960.
  3.  
  4. from itertools import permutations
  5.  
  6. counter = 0
  7. board = "BBNNQRKR"  #first valid position starting from BBKNNQRR
  8. possible_board = ""
  9.  
  10. ##########################################################################
  11. # test if king is between the rooks
  12. def rkr_test(pb):
  13.     pb = pb.replace("B", "")
  14.     pb = pb.replace("N", "")
  15.     pb = pb.replace("Q", "")
  16.     if(pb == "RKR"):
  17.         return(True)
  18.     else:
  19.         return(False)
  20. ##########################################################################
  21. # test if bishops are on opposite colors
  22. def bishop_test(pb):
  23.     first_bishop =  pb.find("B")
  24.     second_bishop = pb.find("B", first_bishop + 1)
  25.     if ((second_bishop - first_bishop) % 2 == 1):
  26.         return(True)
  27.     else:
  28.         return(False)
  29. ##########################################################################
  30.  
  31. perms = [''.join(i) for i in permutations(board)]
  32. sorted_unique_perms = sorted(set(perms))
  33.  
  34. print()
  35.  
  36. for i in range(0, len(sorted_unique_perms)):
  37.     possible_board = sorted_unique_perms[i]
  38.     if(rkr_test(possible_board)):
  39.         if(bishop_test(possible_board)):
  40.             counter += 1
  41.             print(counter, "\t", possible_board)
  42. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement