From: Morton Goldberg Date: 2006-12-22T00:24:37+09:00 Subject: Re: [SUMMARY] Chess960 (#106) On Dec 21, 2006, at 8:37 AM, Ruby Quiz wrote: > Here's the main algorithm from Morton's code (with a minor fix from > me): > > # ... > > def initialize(number) > q, @bishop_index = number.divmod 16 > @knight_index, @queen_index = q.divmod 6 > @white_pieces = BISHOP_TABLE[@bishop_index].split('') > @white_pieces[nth_dash(@queen_index)] = 'Q' > knights = N5N_TABLE[@knight_index] > m = knights.index('N') > n = knights.index('N', m + 1) > m, n = nth_dash(m), nth_dash(n) > @white_pieces[m] = 'N' > @white_pieces[n] = 'N' > @white_pieces[@white_pieces.index('-')] = 'R' > @white_pieces[@white_pieces.index('-')] = 'K' > @white_pieces[@white_pieces.index('-')] = 'R' > end > > def nth_dash(n) > dashes = [] > @white_pieces.each_with_index { |ch, i| dashes << i if ch == > '-' } > dashes[n] > end > If you're going to make the change - q, @bishop_index = (number - 1).divmod 16 + q, @bishop_index = number.divmod 16 then, to maintain consistency, you've got to make the following changes as well: > if __FILE__ == $0 > begin - if ARGV.empty? then n = 1 + rand(960) + if ARGV.empty? then n = rand(960) > else > n = ARGV.first.to_i - raise StandardError unless (1..960).include?(n) + raise StandardError unless (0..959).include?(n) > end > puts "Initial position #{n}" > print Chess960.new(n).to_s > rescue StandardError > puts "Usage: #{$PROGRAM_NAME} []" - puts "where is in 1..960" + puts "where is in 0..959" > puts "Omitting produces a random initial position" > end > end Regards, Morton