The #1 programming site for children

2. Build a House

House

Since it might rain soon, and computers don’t like water very much, let’s build a shelter for ourselves and our bot. You already know how to build a wall, so how about building 4 walls by repeating the program we already made four times?

with Adabots;

procedure House is
   Bot : constant Adabots.Turtle := Adabots.Create_Turtle;
begin
   -- wall 1
   for I in 1 .. 4 loop
      Bot.Place_Up;
      Bot.Place_Down;
      Bot.Back;
      Bot.Place;
   end loop;
   Bot.Turn_Left;

   -- wall 2
   for I in 1 .. 4 loop
      Bot.Place_Up;
      Bot.Place_Down;
      Bot.Back;
      Bot.Place;
   end loop;
   Bot.Turn_Left;

   -- wall 3
   for I in 1 .. 4 loop
      Bot.Place_Up;
      Bot.Place_Down;
      Bot.Back;
      Bot.Place;
   end loop;
   Bot.Turn_Left;

   -- wall 4 needs to be a little shorter to leave
   -- space for the door!
   for I in 1 .. 3 loop
      Bot.Place_Up;
      Bot.Place_Down;
      Bot.Back;
      Bot.Place;
   end loop;

   -- at this point we are standing in the doorway
end House;

Since the first 3 walls are completely the same, can you use a loop to reduce the amount of code we need? You will need to use a different variable name in the outer loop than we have been using, since the inner and outer loop can’t use the same one. Here’s an example of a nested loop:

for N in 1 .. 3 loop
   for I in 1 .. 5 loop
      Bot.Something; -- replace this with a real instruction
   end loop;
   Bot.Something;
end loop;

To make a roof, we should imagine how the bot needs to move to get up to the top (remember, bots can fly!), and then think of a way to move over all the tiles in sequence while placing blocks below.