Ruby Quiz wrote:

> by Kieran Wild
>
> Chess960, is a chess variant produced by Grandmaster Bobby Fischer by
> formalizing the rules of Shuffle Chess. Its goal was to create a chess variant
> in which chess creativity and talent would be more important than memorization
> and analysis of opening moves. His approach was to create a randomized initial
> chess position, which would thus make memorizing chess opening move sequences
> far less helpful. The initial position is set up in a special way and there are
> 960 such positions, thus the name Chess960.
>
> The starting position for Chess960 must meet certain rules. White pawns are
> placed on the second rank as in chess. All remaining white pieces are placed
> randomly on the first rank, but with the following restrictions:
>
> 	* The king is placed somewhere between the two rooks.
> 	* The bishops are placed on opposite-colored squares.
>
> The black pieces are placed equal-and-opposite to the white pieces. For example,
> if the white king is placed on b1, then the black king is placed on b8. Note
> that the king never starts on file a or h, because there would be no room for a
> rook
>
> Can I suggest a nice little ruby program to generates all 960 possible starting
> positions and outputs a random one on request.

which = ( ARGV.first || rand(960) + 1 ).to_i
count = 0

(1..6).each{|k|
  (0...k).each{|r1|
    (k+1..7).each{|r2|
      ((0..7).to_a - [k,r1,r2]).each{|q|
        used = [k,r1,r2,q]
        ((0..7).select{|i| i % 2 == 0} - used).each{|b1|
          ((0..7).select{|i| i % 2 == 1} - used).each{|b2|
            count += 1
            if which == count
              puts "Position #{ count }"
              s = 'N' * 8
              [k,q,r1,r2,b1,b2].zip(%w(K Q R R B B)).each{|i,p|
                s[i] = p }
              puts s.downcase,'p'*8,('.'*8+"\n")*4,'P'*8,s
            end   } } } } } }