Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Chess960 Initial Position Generator (Ruby)
#!/usr/bin/ruby
board = Array.new(8)
# Bishops
2.times do |i|
r = rand(4) * 2
if i == 1
r += 1
end
board[r] = "B"
end
# Queen, Knights
%w[Q N N].each do |s|
empty = board.each_index.select { |i| board[i].nil? }
board[empty.sample] = s
end
# Rook, King, Rook
%w[R K R].each do |s|
board[board.index(nil)] = s
end
board.each do |i|
print "#{i} "
end
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment