# \[SUMMARY\] Chess960 (#106)

**URL:** <https://rubytalk.org/t/summary-chess960-106/33943>\
**Category:** ruby-talk\
**Created:** [21 December 2006 13:37 UTC](https://rubytalk.org/t/summary-chess960-106/33943 "2006-12-21T13:37:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [21 December 2006 13:37 UTC](https://rubytalk.org/t/summary-chess960-106/33943/1 "2006-12-21T13:37:02Z")

</div>

There are a surprising number of ways to think about this problem, each leading  
to a different solution. Let's examine several.

First, you can think of this as a constraints problem. We have the rules of a  
board setup which are the constraints and any board meeting those rules is one  
of the 960 positions. Way back when we did the Constraint Processing Ruby Quiz,  
I learned that the Amb library is a fun way to handle such problems. Here's my  
solution, using Amb:

&nbsp;&nbsp;require "amb"  
&nbsp;&nbsp;  
&nbsp;&nbsp;setup = Amb.new  
&nbsp;&nbsp;count = 0  
&nbsp;&nbsp;seen = Hash.new  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;squares = Array.new(8) { setup.choose(\*%w[r n b q k b n r]) }  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;%w[r n b].each do |piece|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setup.assert(squares.select { |s| s == piece }.size == 2)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;%w[k q].each do |piece|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setup.assert(squares.select { |s| s == piece }.size == 1)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;king = squares.index("k")  
&nbsp;&nbsp;&nbsp;&nbsp;setup.assert(squares.index("r") \< king)  
&nbsp;&nbsp;&nbsp;&nbsp;setup.assert(squares.rindex("r") \> king)  
&nbsp;&nbsp;&nbsp;&nbsp;setup.assert((squares.index("b") + squares.rindex("b")) % 2 == 1)  
&nbsp;&nbsp;&nbsp;&nbsp;board = squares.join(' ')  
&nbsp;&nbsp;&nbsp;&nbsp;setup.assert(seen[board].nil?)  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;puts "#{count += 1}: #{board}"  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;seen[board] = true  
&nbsp;&nbsp;&nbsp;&nbsp;setup.failure  
&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;# do nothing, we're done  
&nbsp;&nbsp;end

What I really love above this approach is that it requires almost no thought.  
All I am doing here is translating the rules to code. Ruby and Amb do the rest.

The above code works by building an Amb object that is considering piece  
placement in eight squares. After that, I define the rules for placement.

Since I gave it all eight pieces as possible choices for each square, the first  
two rules have to establish the allowed counts for each piece. This prevents  
Amb from building a position of eight rooks or similar wrong setups.

For the next two rules I locate the king and verify that we have a rook to the  
left of him as well as to the right. The following rule adds the locations of  
the two bishops and verifies that we got an odd number. This ensures the  
bishops are on different colors, since an even plus an odd will be odd but even  
plus even or odd plus odd both yield even numbers. This group of rules covers  
the constraints from the quiz description.

The final rule prevents duplicate positions being found. It's needed because  
positions like the following example seem different to the computer, but not to  
a chess player:

&nbsp;&nbsp;N1 B1 B2 R1 K R2 Q N2  
&nbsp;&nbsp;N2 B2 B1 R2 K R1 Q N1

With the rules in place, Amb will have found a viable solution. I print that  
out, then manually trigger a failure(), to cause it to find another. When it  
fails to find one an Exception will be thrown. I catch that and exit quietly  
since we will have seen all 960 positions at that point.

The downside of this approach is that Amb uses Ruby's continuations to under the  
hood to backtrack and find new solutions. Those are a bit on the slow side and  
this simple script of mine takes about six and a half minutes to complete, on my  
box. One solution to this is just to generate the positions once and cache them  
for future runs but there were other solutions that could think a lot faster.

For a faster solution, let's shift how we are thinking about the problem.  
Another approach is to think of this challenge as a permutations problem. We  
really just need to examine all possible permutations of the eight pieces and  
select those that match the quiz rules. Multiple solutions did this, including  
the following code from David Tran:

&nbsp;&nbsp;def permutation(pieces)  
&nbsp;&nbsp;&nbsp;return [pieces] if pieces.length \<= 1  
&nbsp;&nbsp;&nbsp;result = []  
&nbsp;&nbsp;&nbsp;pieces.uniq.each do |p|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_pieces = pieces.dup  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_pieces.delete\_at(pieces.index(p))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;permutation(\_pieces).each do |perm|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result \<\< (perm \<\< p)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;result  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;results = permutation("RNBKQBNR".split(//)).select do |position|  
&nbsp;&nbsp;&nbsp;r1 = position.index('R')  
&nbsp;&nbsp;&nbsp;r2 = position.rindex('R')  
&nbsp;&nbsp;&nbsp;b1 = position.index('B')  
&nbsp;&nbsp;&nbsp;b2 = position.rindex('B')  
&nbsp;&nbsp;&nbsp;k = position.index('K')  
&nbsp;&nbsp;&nbsp;r1 \< k && k \< r2 && ((b1+b2) % 2 != 0)  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;puts "Total positions = #{results.length}"  
&nbsp;&nbsp;puts results[rand(results.length)].join(' ')

The permutation() method here is a recursive generator of all possible  
permutations. It works by adding each() uniq() piece to all possible smaller  
permutations. Those are found by removing one piece from the set each time and  
recursing.

The rest of the code calls that method and then select()s the positions matching  
the quiz rules. Those rules are almost identical to my implementation of them  
that we examined earlier.

This code does the same thing as mine but runs in under a second on my box.

Shifting the approach again, several methods have been developed to help players  
create positions using these rules as needed, some using dice. Bodlaender's  
dice-rolling method is a pretty easy system to translate into code and Jamie  
Macey did just that:

&nbsp;&nbsp;class Chess960  
&nbsp;&nbsp;&nbsp;attr\_reader :board\_id, :board  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@board = generate\_board(bodlaender\_line)  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;def generate\_board(white)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Black's starting line is mirror of white's  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;black = white.map{|piece| piece.downcase}  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# middle of board is always the same  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;middle = [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%w(p p p p p p p p),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%w(\_ \_ \_ \_ \_ \_ \_ \_),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%w(\_ \_ \_ \_ \_ \_ \_ \_),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%w(\_ \_ \_ \_ \_ \_ \_ \_),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%w(\_ \_ \_ \_ \_ \_ \_ \_),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%w(P P P P P P P P)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# add back rows  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[black] + middle + [white]  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;def bodlaender\_line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free = (0...8).to\_a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white = []  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dark\_bishop = rand(4) \* 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;light\_bishop = rand(4) \* 2 + 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white[dark\_bishop] = 'B'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white[light\_bishop] = 'B'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free.delete(dark\_bishop)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free.delete(light\_bishop)  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queen = rand(6)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white[free[queen]] = 'Q'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free.delete\_at(queen)  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;knight1 = rand(5)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white[free[knight1]] = 'N'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free.delete\_at(knight1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;knight2 = rand(4)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white[free[knight2]] = 'N'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free.delete\_at(knight2)  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white[free[0]] = 'R'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white[free[1]] = 'K'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white[free[2]] = 'R'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

The first two methods are trivial with initialize() kicking off the process and  
generate\_board() building a board representation. The interesting stuff happens  
in bodlaender\_line().

This algorithm works with a collection of free indices and an Array of the final  
piece arrangement. As pieces are placed in the Array, those indices are pulled  
from the free list so they won't be reused.

The first step is to place both bishops. That's done by choosing a random  
number between one and four and placing it on the selected light or dark square.  
After that, a random selection places the queen on one of the six remaining  
squares. The same technique is used to place the knights on the remaining five  
and then four squares. That leaves us three empty squares which must be R, K,  
and R, in that order, to satisfy the rules.

Making one more leap of thought with regard to this problem, there has been an  
effort to enumerate all 960 positions. This has a lot of value to chess players  
since we can record the game using our usual techniques and just add a note  
like, "Started from Chess960 position #351." Even better, once you have a  
system for enumerating the positions, you can use that system to build the  
entire list or select a position. Morton Goldberg gave us the code for that:

&nbsp;&nbsp;class Chess960  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BISHOP\_TABLE = [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"BB------", #0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"B--B----", #1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"B----B--", #2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"B------B", #3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-BB-----", #4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"--BB----", #5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"--B--B--", #6  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"--B----B", #7  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-B--B---", #8  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"---BB---", #9  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"----BB--", #10  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"----B--B", #11  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-B----B-", #12  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"---B--B-", #13  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-----BB-", #14  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"------BB" #15  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N5N\_TABLE = [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"NN---", #0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"N-N--", #1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"N--N-", #2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"N---N", #3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-NN--", #4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-N-N-", #5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-N--N", #6  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"--NN-", #7  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"--N-N", #8  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"---NN" #9  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...

The official numbering scheme, invented by Reinhard Scharnagl, works by using  
simple charts to position the pieces. Above you can see Morton's translation of  
the two charts he will use, giving piece placements and their indices. The  
knight charts are narrower because they are placed after the bishops and queen,  
taking three squares out of consideration.

Here's the main algorithm from Morton's code (with a minor fix from me):

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(number)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q, @bishop\_index = number.divmod 16  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@knight\_index, @queen\_index = q.divmod 6  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces = BISHOP\_TABLE[@bishop\_index].split('')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[nth\_dash(@queen\_index)] = 'Q'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;knights = N5N\_TABLE[@knight\_index]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = knights.index('N')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = knights.index('N', m + 1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m, n = nth\_dash(m), nth\_dash(n)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[m] = 'N'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[n] = 'N'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[@white\_pieces.index('-')] = 'R'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[@white\_pieces.index('-')] = 'K'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[@white\_pieces.index('-')] = 'R'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def nth\_dash(n)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dashes = []  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces.each\_with\_index { |ch, i| dashes \<\< i if ch == '-' }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dashes[n]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...

Most of the clever work is done in initialize(). First, a little math is used  
to find the lookup index on the bishop's chart, an index for the queen, and an  
index on the knight's chart. The row selected from the bishop's chart becomes  
the basis for the final arrangement of pieces and nth\_dash() is used to properly  
slot the queen in that template. The knight's are then pulled from their chart  
by index and nth\_dash() is again used to place them. The three squares left  
then must be a rook, king, and rook in that order.

The rest of Morton's code is just for displaying the results:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def inspect  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces.join  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white\_pieces = @white\_pieces.join + "\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;white\_pawns = 'P' \* 8 + "\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;black\_pieces = white\_pieces.downcase  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;black\_pawns = 'p' \* 8 + "\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;empty = ('.' \* 8 + "\n") \* 4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;black\_pieces + black\_pawns + empty + white\_pawns + white\_pieces  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;if \_\_FILE\_\_ == $0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ARGV.empty? then n = 1 + rand(960)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = ARGV.first.to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise StandardError unless (1..960).include?(n)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Initial position #{n}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print Chess960.new(n).to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue StandardError  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Usage: #{$PROGRAM\_NAME} [\<integer\>]"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "where \<integer\> is in 1..960"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Omitting \<integer\> produces a random initial position"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

If you want to read more about the systems for assigning pieces, check out:

&nbsp;&nbsp;[http://en.wikipedia.org/wiki/Chess960\_starting\_position](http://en.wikipedia.org/wiki/Chess960_starting_position)

and:

&nbsp;&nbsp;[http://en.wikipedia.org/wiki/Chess960\_Enumbering\_Scheme](http://en.wikipedia.org/wiki/Chess960_Enumbering_Scheme)

There were a lot more creative elements in the solutions I didn't cover. Jamie  
Macey even built a complete Camping application to display the positions.  
Definitely take the time to look over them. It's worth it.

My thanks to all for another great quiz. I'm a big chess nut some problems like  
this always thrill me.

There will be no Ruby Quiz tomorrow. I'll be busy having a merry Christmas and  
I wish the same for others. See you in a week!

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [21 December 2006 15:24 UTC](https://rubytalk.org/t/summary-chess960-106/33943/2 "2006-12-21T15:24:37Z")

</div>

> Here's the main algorithm from Morton's code (with a minor fix from me):
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
> &nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(number)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q, @bishop\_index = number.divmod 16  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@knight\_index, @queen\_index = q.divmod 6  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces = BISHOP\_TABLE[@bishop\_index].split('')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[nth\_dash(@queen\_index)] = 'Q'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;knights = N5N\_TABLE[@knight\_index]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = knights.index('N')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = knights.index('N', m + 1)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m, n = nth\_dash(m), nth\_dash(n)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[m] = 'N'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[n] = 'N'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[@white\_pieces.index('-')] = 'R'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[@white\_pieces.index('-')] = 'K'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[@white\_pieces.index('-')] = 'R'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def nth\_dash(n)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dashes =   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces.each\_with\_index { |ch, i| dashes \<\< i if ch == '-' }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dashes[n]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:

> &nbsp;&nbsp;if \_\_FILE\_\_ == $0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin

- if ARGV.empty? then n = 1 + rand(960)  
+ if ARGV.empty? then n = rand(960)

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = ARGV.first.to\_i

- raise StandardError unless (1..960).include?(n)  
+ raise StandardError unless (0..959).include?(n)

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Initial position #{n}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print Chess960.new(n).to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue StandardError  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Usage: #{$PROGRAM\_NAME} [\<integer\>]"

- puts "where \<integer\> is in 1..960"  
+ puts "where \<integer\> is in 0..959"

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Omitting \<integer\> produces a random initial position"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end

Regards, Morton

> **···**
>
> On Dec 21, 2006, at 8:37 AM, Ruby Quiz wrote:

---

<div class="post-metadata">

**Author:** ![rik](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@rik](https://rubytalk.org/u/rik)\
**Post date:** [21 December 2006 17:10 UTC](https://rubytalk.org/t/summary-chess960-106/33943/3 "2006-12-21T17:10:10Z")

</div>

We're currently wondering why none of the published solutions take the  
approach of:

First, place king between b to g, inclusive.  
- Place rook on left of king.  
- Place rook on right of king.  
- Place bishop in empty black sqaure.  
- Place bishop in empty white square.  
- Fill remaining squares with knights and queen.

Note that the number of permutations generated there are VASTLY lowered  
as compared to the "generate everything, then throw away everything  
invalid" approach.

It also seems to be the common sense approach in my office...

(Red rag, have you met Mr Bull?)

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [21 December 2006 18:35 UTC](https://rubytalk.org/t/summary-chess960-106/33943/4 "2006-12-21T18:35:04Z")

</div>

rik wrote:

> We're currently wondering why none of the published solutions take the  
> approach of:
> 
> First, place king between b to g, inclusive.  
> - Place rook on left of king.  
> - Place rook on right of king.  
> - Place bishop in empty black sqaure.  
> - Place bishop in empty white square.  
> - Fill remaining squares with knights and queen.
> 
> Note that the number of permutations generated there are VASTLY lowered  
> as compared to the "generate everything, then throw away everything  
> invalid" approach.
> 
> It also seems to be the common sense approach in my office...
> 
> (Red rag, have you met Mr Bull?)

Did you look at my first solution?

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [21 December 2006 18:37 UTC](https://rubytalk.org/t/summary-chess960-106/33943/5 "2006-12-21T18:37:24Z")

</div>

Well, Jamie Macey's dice rolling solution is fairly similar. The order is different but it works the same.

Jamie's implementation requires even fewer decisions though, since three whole pieces are placed without any random maneuvering. I guess, in that respect, it seems pretty "common sensical" to me.

James Edward Gray II

> **···**
>
> On Dec 21, 2006, at 11:10 AM, rik wrote:
> 
> > We're currently wondering why none of the published solutions take the  
> > approach of:
> > 
> > First, place king between b to g, inclusive.  
> > - Place rook on left of king.  
> > - Place rook on right of king.  
> > - Place bishop in empty black sqaure.  
> > - Place bishop in empty white square.  
> > - Fill remaining squares with knights and queen.
> > 
> > Note that the number of permutations generated there are VASTLY lowered  
> > as compared to the "generate everything, then throw away everything  
> > invalid" approach.
> > 
> > It also seems to be the common sense approach in my office...

---

<div class="post-metadata">

**Author:** ![Pedro\_Fortuny\_Ayuso](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pedro\_Fortuny\_Ayuso](https://rubytalk.org/u/Pedro_Fortuny_Ayuso)\
**Post date:** [21 December 2006 18:40 UTC](https://rubytalk.org/t/summary-chess960-106/33943/6 "2006-12-21T18:40:47Z")

</div>

That's what I did in my first solution (+take account on symmetry and only  
generate half  
of the solutions), but it does not preserve the so-thought-of "official"  
numbering scheme.  
Yes, that is the obvious solution.

\*Heck\* I cannot see if my first solution is posted bc my IP's internet  
filter filters just my  
posts. Did I write something obscene?

Pedro.

> **···**
>
> On 12/21/06, rik \<rikrose@gmail.com\> wrote:
> 
> > We're currently wondering why none of the published solutions take the  
> > approach of:
> > 
> > First, place king between b to g, inclusive.  
> > - Place rook on left of king.  
> > - Place rook on right of king.  
> > - Place bishop in empty black sqaure.  
> > - Place bishop in empty white square.  
> > - Fill remaining squares with knights and queen.
> > 
> > Note that the number of permutations generated there are VASTLY lowered  
> > as compared to the "generate everything, then throw away everything  
> > invalid" approach.
> > 
> > It also seems to be the common sense approach in my office...
> > 
> > (Red rag, have you met Mr Bull?)
> 
> --  
> Pedro Fortuny Ayuso  
> C/Capuchinos 14, 1. 47006 Valladolid. SPAIN
> 
> > **[Pedro Fortuny Ayuso: homepage](http://pfortuny.sdf-eu.org)**
> >
> > Pedro Fortuny, Pedro Fortuny Ayuso homepage. Resume,
> > Curriculum Vitae, Works, Freelance, Networks, Security

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [21 December 2006 19:19 UTC](https://rubytalk.org/t/summary-chess960-106/33943/7 "2006-12-21T19:19:40Z")

</div>

You're absolutely right. Thanks for pointing that out.

James Edward Gray II

> **···**
>
> On Dec 21, 2006, at 9:24 AM, Morton Goldberg wrote:
> 
> > 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):
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
> > > &nbsp;&nbsp;  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(number)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q, @bishop\_index = number.divmod 16  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@knight\_index, @queen\_index = q.divmod 6  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces = BISHOP\_TABLE[@bishop\_index].split('')  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[nth\_dash(@queen\_index)] = 'Q'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;knights = N5N\_TABLE[@knight\_index]  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = knights.index('N')  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = knights.index('N', m + 1)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m, n = nth\_dash(m), nth\_dash(n)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[m] = 'N'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[n] = 'N'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[@white\_pieces.index('-')] = 'R'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[@white\_pieces.index('-')] = 'K'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces[@white\_pieces.index('-')] = 'R'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def nth\_dash(n)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dashes =   
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@white\_pieces.each\_with\_index { |ch, i| dashes \<\< i if ch == '-' }  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dashes[n]  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:
> > 
> > > &nbsp;&nbsp;if \_\_FILE\_\_ == $0  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin
> > 
> > - if ARGV.empty? then n = 1 + rand(960)  
> > + if ARGV.empty? then n = rand(960)
> > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = ARGV.first.to\_i
> > 
> > - raise StandardError unless (1..960).include?(n)  
> > + raise StandardError unless (0..959).include?(n)
> > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Initial position #{n}"  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print Chess960.new(n).to\_s  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue StandardError  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Usage: #{$PROGRAM\_NAME} [\<integer\>]"
> > 
> > - puts "where \<integer\> is in 1..960"  
> > + puts "where \<integer\> is in 0..959"
> > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Omitting \<integer\> produces a random initial position"  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![rik](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@rik](https://rubytalk.org/u/rik)\
**Post date:** [22 December 2006 12:05 UTC](https://rubytalk.org/t/summary-chess960-106/33943/8 "2006-12-22T12:05:04Z")

</div>

> Did you look at my first solution?

Apparently not well enough.
