with TEXT_IO; package INTEGER_IO is new TEXT_IO.INTEGER_IO(INTEGER); with TEXT_IO, INTEGER_IO; use TEXT_IO, INTEGER_IO; procedure Queens is BSize : INTEGER := 0; procedure Title is begin NEW_LINE; PUT_LINE("###### ## ## ###### ###### ## ## ######"); PUT_LINE("###### ## ## ###### ###### ### ## ######"); PUT_LINE("## ## ## ## ## ## #### ## ## "); PUT_LINE("## ## ## ## ###### ###### ##### ## ######"); PUT_LINE("## ## ## ## ## ## ## ##### ##"); PUT_LINE("### ### ###### ###### ###### ## #### ######"); PUT_LINE("#### ## ###### ###### ###### ## ## ######"); NEW_LINE(3); end title; procedure GetBSize(Size : out INTEGER) is Temp : INTEGER := 0; begin While Temp<1 loop PUT("What is the size of the chess board?"); GET(Temp); NEW_LINE; if Temp<1 then PUT_LINE("Positive integers only please."); end if; end loop; Size := Temp; exception when others => PUT_LINE("Erroneous input - Board size will be 8."); Size := 8; end GetBSize; procedure DoQueens(Size : in INTEGER) is subtype BType is INTEGER range 1..Size; type BArray is array (INTEGER range 1..Size) of BType; Board : BArray; Fail : BOOLEAN; procedure DrawBoard(Rows : in INTEGER; Board : in BArray) is R, C : INTEGER; begin for R in 1..Rows loop for C in 1..Rows loop if (Board(R) = C) then PUT("Q "); else PUT("+ "); end if; end loop; NEW_LINE; end loop; end DrawBoard; function Place( Size, Row : INTEGER) return BOOLEAN is IsGood : BOOLEAN := False; Column : INTEGER := 0; Left, Right : INTEGER; Void : BOOLEAN; begin if Row>Size then return TRUE; end if; while not (Row=1 and Column>Size) loop while not IsGood loop IsGood := TRUE; Column := Column + 1; Left := Column; Right := Column; for N in reverse 1..(Row-1) loop Left := Left - 1; Right := Right + 1; if Left>0 then if Board(N)=Left then IsGood := FALSE; end if; end if; if Right<=Size then if Board(N)=Right then IsGood := FALSE; end if; end if; if Board(N)=Column then IsGood := FALSE; end if; end loop; if Column=Size and IsGood=FALSE then return FALSE; end if; end loop; Board(Row) := Column; if place(Size, Row + 1) then return TRUE; end if; if Column