Permalink
Browse files

Works for 4 to 18 queens but broken when n > 18. Lots of cleanup to do

  • Loading branch information...
1 parent 74362db commit cf25377512e0fb351a5f6783ed45b292ac92404c @mikeebert committed Apr 17, 2012
Showing with 47 additions and 27 deletions.
  1. +27 −14 lib/queensolver.rb
  2. +20 −13 spec/queensolver_spec.rb
View
@@ -31,32 +31,46 @@ def place_next_queen
place_positions unless @positions.empty?
@row += 1
@column = 0
- print "#{@positions} \n"
coordinates_for_next_move unless @positions.count == @size
end
def coordinates_for_next_move
- while column_is_occupied(@column) == true || diagonal_is_occupied(@row,@column) == true
- @column += 1
+ while column_is_occupied(@column) || diagonal_is_occupied(@row,@column)
+ @column += 1 if @column < @size
if @column == @size
- if @positions.last[1] == @size - 1
+ if @positions.map{|coordinates| coordinates[1] == (@size - 1)}.include?(true)
+ move_at_end_of_row = @positions.index{|column| column[1] == (@size-1)}
+ puts "The move at the end of the row is #{move_at_end_of_row}"
+ positions_to_delete = (move_at_end_of_row..(@positions.count-1)).to_a
+ puts "The array of indexes to delete are #{positions_to_delete}"
+ positions_to_delete.reverse.each {|index| @positions.delete_at(index)}
+ puts "The remaining positions are #{@positions}"
+ @row = @positions.last[0]
+ @column = @positions.last[1] + 1
@positions.delete_at(@positions.count - 1)
- @positions.delete_at(@positions.count - 1)
+ puts "Positions now equals #{@positions} and starting coordinates are #{@row}, #{@column}"
+ place_positions
+ elsif @row == @size - 1 && @column == @size
+ puts "it hit the elsif statement"
+ @row = 0
+ @column = @positions.last[1] + 1
+ @positions.delete(@positions.count - 1)
+ place_positions
else
+ puts "it hit the else statement"
@row = @positions.last[0]
@column = @positions.last[1] + 1
@positions.delete_at(@positions.count - 1)
- end
- if @positions.count == 0
- @row = 0
- @column += 1
+
end
end
end
place_next_queen
end
def column_is_occupied(column_index)
+ # coordinates = @positions.map{|position| position[1] == column_index}
+ # return true if coordinates.include?(true)
column = @board.map {|row| row[column_index]}
return true if column.compact.count > 0
end
@@ -81,15 +95,15 @@ def diagonal_is_occupied(row_index, column_index)
#check up and forward
row = row_index - 1
column = column_index + 1
- while row > 0 && column < @size
+ while row >= 0 && column < @size
return true if @board[row][column] == "Q"
row -= 1;column +=1
end
#check down and back
row = row_index + 1
column = column_index - 1
- while row < @size && column > 0
+ while row < @size && column >= 0
return true if @board[row][column] == "Q"
row += 1;column -= 1
end
@@ -194,11 +208,10 @@ def set_board
def place_positions
set_board
- display_board
- print "The positions are: #{positions} \n"
+ # print "The positions are: #{positions} \n"
@positions.each do |coordinates|
@board[coordinates[0]][coordinates[1]] = "Q"
- end
+ end
end
def display_board
View
@@ -135,27 +135,34 @@
@queen_solver.diagonal_is_occupied(2,0).should == true
end
- it "should move a queen to the first available space" do
- @queen_solver = QueenSolver.new(4)
- @queen_solver.board[0][0] = "Q"
- @queen_solver.positions << [0,0]
- @queen_solver.move_to_next_available_space
- @queen_solver.board.should == [["Q",nil,nil,nil],
- [nil,nil,"Q",nil],
- [nil,nil,nil,nil],
- [nil,nil,nil,nil]]
- end
-
it "should place 1 Queen on a 1 x 1 Board" do
@queen_solver = QueenSolver.new(1)
@queen_solver.place_queens
@queen_solver.solved.should == true
end
-
+
it "should place 4 Queens on a 4 x 4 Board" do
@queen_solver = QueenSolver.new(4)
@queen_solver.place_queens
- # @queen_solver.solved.should == true
+ end
+
+ it "should place 6 Queens on a 6 x 6 Board" do
+ @queen_solver = QueenSolver.new(6)
+ @queen_solver.place_queens
+ end
+
+ it "should place 8 Queens on a 8 x 8 Board" do
+ @queen_solver = QueenSolver.new(8)
+ @queen_solver.place_queens
+ end
+ end
+
+ describe "Placing N Queens on an N X N Board" do
+ [1,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20].each do |n|
+ it "should place #{n} queens on an #{n} x #{n} board" do
+ @queen_solver = QueenSolver.new(n)
+ @queen_solver.place_queens
+ end
end
end

0 comments on commit cf25377

Please sign in to comment.