Last active
August 29, 2015 14:07
-
-
Save kaiwren/5a26b24be25d81b82c42 to your computer and use it in GitHub Desktop.
Answering a question asked on the Fb Learn Ruby Programming group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Queen | |
def initialize(arrayLength) | |
@array = Array.new(arrayLength) | |
end | |
def starter | |
enumerate(@array,0) | |
end | |
def enumerate(q,n) | |
if n == q.length | |
printQueen(q) | |
else | |
for i in 0..(q.length-1) | |
q[n] = i | |
if isConsistent(q,n) | |
enumerate(q,n+1) | |
end | |
end | |
end | |
end | |
def isConsistent(q,n) | |
n.times do |i| | |
return false if (q[i] == q[n]) | |
return false if ((q[i] - q[n]) == (n-1) ) | |
return false if ((q[n] - q[i]) == (n-1) ) | |
return true | |
end | |
end | |
def printQueen(q) | |
for i in 0..(q.length-1) | |
for j in 0..(q.length-1) | |
if q[i] == j | |
print "Q" | |
else | |
print "*" | |
end | |
end | |
puts "" | |
end | |
puts "" | |
end | |
end | |
queens = Queen.new(4) | |
queens.starter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment