-- queens
with Ada.Text_Io;
use Ada.Text_Io;
with Interfaces;
use Interfaces;
procedure Queens is
subtype Index is Natural range 1..8;
Queens : array (Index) of Unsigned_8;
function Row (
Vertex : Unsigned_8;
Diag : Unsigned_8;
The_Row : Index )
return Boolean is
M0 : Unsigned_8;
M1 : Unsigned_8;
C1 : constant Unsigned_8 := 16#10#;
C3 : constant Unsigned_8 := 7;
Urow : Unsigned_8 := Unsigned_8(The_Row);
begin
-- set diagonal initial bits
M1 := (Shift_Left(C1, The_Row) or (Shift_Left(1, Natural((C3 xor Urow)))));
M0 := 1;
while M0 <= 255 loop
-- see if lines not already attacked
if 0 = ((Vertex and M0) or (Diag and M1)) then
-- recurse on next row after attacking these lines
if (The_Row = Index'Last) or else
(The_Row < Index'Last and then
Row((Vertex or M0), (Diag or M1), The_Row + 1)) then
Queens(The_Row) := M0;
return True;
end if;
end if;
M0 := M0 + M0;
M1 := M1 + M1;
end loop;
return False;
end Row;
procedure Display_Solution is
begin
for I in Index'range loop
for J in 0..7 loop
if Queens(I) = Shift_Left(1, J) then
Put("Q");
else
Put("_");
end if;
end loop;
New_Line;
end loop;
end Display_Solution;
begin
if Row(0,0,1) then
Display_Solution;
else
Put_Line("No Solution");
end if;
end Queens;