/
eightq.rb
executable file
·55 lines (49 loc) · 911 Bytes
/
eightq.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
52
53
54
#!/usr/bin/ruby
class Eightqueens
@@solutions = 0
def good_addition(board, pos)
col = 0
for row in board do
return false if row == pos
return false if row + (board.size - col) == pos
return false if row - (board.size - col) == pos
col += 1
end
return true
end
def pg(good_sofar)
if good_sofar.count == 8
@@solutions = @@solutions + 1
printf("\n");
print_board(good_sofar)
else
i = 0
while i < 8
if good_addition(good_sofar, i)
a = Array.new
for x in good_sofar do
a << x
end
a << i
pg(a);
end
i += 1
end
end
return @@solutions
end
def print_board(ba)
for row in [0,1,2,3,4,5,6,7] do
for col in 0..7 do
if (ba[col] == row)
printf("[Q]")
else
printf("[ ]")
end
end
printf("\n");
end
end
end
eightqueens = Eightqueens.new
printf("\nSolutions: %s\n", eightqueens.pg(Array.new))