Files
  • main.rb
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
        		q[n] = i
        		if isConsistent(q,n)
        			enumerate(q,n+1)
        		end
        	end
        end

    end

    def isConsistent(q=[],n)
    	for i in 0..q.length
    		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
        	for j in 0..q.length
        		if q[i] == j
        			print "Q"
        		else
        			print "*"
        		end
        	end
          puts ""
	    end
	   puts ""
    end
end

queens = Queen.new(4)
queens.starter
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]