Skip to content
Fetching contributors… Cannot retrieve contributors at this time
78 lines (68 sloc) 1.63 KB
 # The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens attack each other. # Thus, a solution requires that no two queens share the same row, column, or diagonal. @wins = [] @all_coords = Array(0..7).product(Array(0..7)) def new_board @board = {} @all_coords.each do |coords| @board[coords] = nil end end def safe?(board, cell) checks = [] checks << lambda{|cell, other| cell == other} checks << lambda{|cell, other| cell == other} checks << lambda{|cell, other| (cell - cell) == (other - other)} checks << lambda{|cell, other| (cell + cell) == (other + other)} @all_coords.each do |coord| results = checks.map{|proc| proc.call(cell, coord)} if results.any? && board[coord] return false end end true end def print_board(board) x = 0 y = 0 until x == 8 until y == 8 if board[[x,y]] == true print "Q " else if safe?(board, [x,y]) == true print '. ' else print '. ' end end y+=1 end print "\n" y = 0 x +=1 end end def iter_place_queens(input, queen_number = 0) if queen_number == 8 @wins << input return nil end row = input.keys[queen_number*8...(queen_number+1)*8] row.each do |index| board = input.dup if safe?(board, index) board[index] = true # puts "we have to go deeper" iter_place_queens(board, queen_number + 1) end end end system('clear') new_board iter_place_queens(@board) for board in @wins print_board(board) puts end print "number of wins: ", @wins.length
You can’t perform that action at this time.