We look ahead.

We look ahead.#

Here, there are no more pebbles. The ant has to travel a long corridor, how to do it?

from laby.global_fr import *
Laby(niveau="2a")

It is of course possible to copy the avance() command several times, but there are

better solutions.

Below, we introduce a new command and show you an example

of its use:

regarde()       # Renvoie Vide, Caillou, PetitCaillou, Mur,
                # Toile, PetiteToile, Sortie, ou Inconnu
                # selon ce qui se trouve sur la case devant la fourmi

from laby.global_fr import *
carte = """
o x o
o ↓ o
o o o
"""
Laby(carte=carte)
regarde()
Tile(name='wall', char='o')

The function regarde() allows you to “look” at what is in front of the ant, it returns a

Python object describing the content of the box in front of the ant. We can then compare

this content with several predefined values:

Mur
Tile(name='wall', char='o')
Vide
Tile(name='void', char='.')
Caillou
Tile(name='rock', char='r')
PetitCaillou
Tile(name='nrock', char='ŕ')
Toile
Tile(name='web', char='w')
PetiteToile
Tile(name='nweb', char='ẃ')
Sortie
Tile(name='exit', char='x')

We will now use a while loop (“Tant que” in French):`

while condition:
    instructions

which means: As long as condition is true, we repeat the instructions.

In the following program, this translates to “While the ant is facing a wall, turn right”:

from laby.global_fr import *
carte = """
o x o
o ↓ o
o o o
"""
Laby(carte=carte)
debut()
while regarde() == Mur:
    droite()
ouvre()

Below is another example

from laby.global_fr import *
carte = """
o x o
o ↓ o
o r o
o o o
"""
Laby(carte=carte)
while regarde() != Sortie:
    droite()
ouvre()

Here, != means “is different from”. The program therefore translates to: “As long as the

ant is not looking at the door, turn right. Then open the door.”

Pay attention to the details:

  • we always put “:” after the condition

  • the instructions (for now only one, but we can put several) are written under the while by “indenting” the beginning of the lines with 4 spaces

  • what is written at the same level as the while (in the example, the ouvre()) is executed when we exit the loop

Inspired by these examples, solve the following problem, be careful the size of the

hallway is not fixed! Your program must work in all cases (run the

cell creating the labyrinth several times, your program must continue to

work.

from laby.global_fr import *
from random import randint
l = randint(2,20)
carte = "o " + "o " * l + "o\n"
carte+= "o " + "→ " + ". " * (l-1) + "x\n"
carte+= "o " + "o " * l + "o\n"
Laby(carte = carte)
### BEGIN SOLUTION
debut()
while( regarde() == Vide ):
    avance()
ouvre()
### END SOLUTION