------ art_2179_14716701.1166383699945
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Here goes my second solution, looks a little bit better, using Jim Weirich's
Amb class
------------------------------------------------------------------
#!/usr/bin/ruby
# This solution uses a cut down version of Jim Weirich's Amb class
# submitted to Ruby Quiz # 70. Hope that's ok?
#
# The purpose of this solution is to show how #generate becomes more
readable
# and there is a fix of the "place 2 Knights instead of 1 Queen" error.
class Amb
class ExhaustedError < RuntimeError; end
def initialize
@fail roc { fail ExhaustedError, "amb tree exhausted" }
end
def choose(*choices)
prev_fail fail
callcc { |sk|
choices.each { |choice|
callcc { |fk|
@fail roc {
@fail rev_fail
fk.call(:fail)
}
sk.call(choice)
}
}
@fail.call
}
end
def assert(cond)
choose unless cond
end
end
class Chess960
N
Queen Q
King K
Rook R
Bishop B
def initialize
@amb mb.new
@solutions ]
init
generate
raise RuntimeError, "Illegal Number of solutions #{@solutions.length}"
unless
@solutions.length 960
end
def [] sol_nb
@solutions[ sol_nb ]
end
private
def init
@sol " * N
end
def generate
@b1 amb.choose( *1..N-1 )
@b2 amb.choose( *@b1.succ..N )
@amb.assert @b1 & 1 ! b2 & 1
@r1 amb.choose( *1..N-2 )
@k amb.choose( *@r1.succ..N-1 )
@r2 amb.choose( *@k.succ..N )
@q amb.choose( *1..N )
# This late check makes the whole thing more readable
# we can easily afford the additional computations
@amb.assert [@b1,@b2,@r1,@k,@r2,@q].uniq.length 6
save_solution
rescue Amb::ExhaustedError
end
def save_solution
@sol[2*(@b1-1)] ishop
@sol[2*(@b2-1)] ishop
@sol[2*(@r1-1)] ook
@sol[2*(@r2-1)] ook
@sol[2*(@q-1)] ueen
@sol[2*(@k-1)] ing
@solutions << @sol
init
@amb.choose
end
end
c hess960.new
puts %<enter a number to show a specific solution (0 based) or
enter r for a random solution or
enter q to go back to work>
until (n ets.strip) /^q/i
i .to_i
i and(960) if n /^r/i
puts "Solution #{i}"
puts c[i]
end
------------------------------------------------------------------
Cheers
Robert
--
"The real romance is out ahead and yet to come. The computer revolution
hasn't started yet. Don't be misled by the enormous flow of money into bad
defacto standards for unsophisticated buyers using poor adaptations of
incomplete ideas."
- Alan Kay
------ art_2179_14716701.1166383699945--