Hello Dart: Introduction to Programming

Part 3: Conditionals

In addition to loops there is a second structure, which is very important to control the program flow. With conditional statements we can specify if a block of statements should be executed or not.

if (treeFront()) {  // Condition.
  turnLeft();       // Block 1, executed if the condition is true.
} else {
  move();           // Block 2, executed if the condition is false.
}
Note: The else part (block 2) may be omitted if it is not needed.

TASK 3.01: Conditionals

Describe in words the effect of the following code examples. Then test them in scenario3.01. For the conditional statements to be executed more than once you should put them into the loop that is already in the scenario.

a.
if (onStar()) {
  removeStar();
}
move();
b.
if (onStar()) {
  removeStar();
} else {
  putStar();
}
move();
c.

Conditional statements may be nested:

if (treeLeft()) {
  if (onStar()) {
    removeStar();
  }
}
move();

TASK 3.02: Star Track

Star Track

The player is to move forward and always put a star where there is none. Make sure that he also puts a star on the last field.

Open the scenario3.02 and write the program.

TASK 3.03: Star at Tree

Star at Tree

Let the player go straight ahaid and put a star anywhere where there is a tree on his left or right or on both sides.

Note that you can use the logical operators &&, ||, and ! in the same way we did in loops.

TASK 3.04: Around Tree II

Around Tree A

How to walk around trees we already know from the first part. We had solved it like this:

class MyPlayer extends Player {

  start() {
    move();
    goAroundTree();
    goAroundTree();
    move();
    goAroundTree();
    removeStar();
  }

  goAroundTree() {
    turnLeft();
    move();
    turnRight();
    move();
    move();
    turnRight();
    move();
    turnLeft();
  }
}

This program works very well as long as the world and trees looks exactly the same. Once a tree is moved, like in the following example, the program will fail.

Around Tree B

The scenario3.04 has three variants. Open this scenario and in the main() function change the text scenario-a.txt to scenario-b.txt. Now test what happens when you start the program in the alternate world.

By now you know various ways you can respond to circumstances with the help of sensor methods. Try to change the program so that your player reaches the star in all worlds with the following properties:

  • The star is always right in front of the player. He must walk around the trees to get to the star.
  • There are never two trees standing next to each other.

TASK 3.05: Round Trip

Round Trip

The player makes a round trip in search of a star (and removes it). Each field in the tour has exactly two empty adjacent fields. One is always behind the player which is the field the player came from.

Open the scenario3.05 and write a program for it. Test your program in all three worlds, scenario-a.txt, scenario-b.txt, and scenario-c.txt.

Hint: First write a loop that stops when the player is on a star. Then imagine what must happen after every step through the loop.

TASK 3.06: Around Tree III

Around Tree III

This is a similar exercise as task 3.04: The player has to find the star that lies before him. Now, however, any number of trees may stand next to each other.

Load the scenario3.06 und improve the goAroundTree() method so that the player can go around multiple trees. Test your program in all the available worlds (a, b, c, and d).

TASK 3.07 (difficult): Follow the Trail

Rundgang

The player has to follow a trail of stars and pick them up. Before the trees he should stop.

Important: Write new methods for specific parts of your program. This greatly improves the clarity of your program.

TASK 3.08 (difficult): Guard

Guard

The player is to guard a forest full of stars. He should walk along the outer edge of the forest.

Start with a single walk around the forest. Then you can let the player make multiple rounds with an infinite loop while (true).

Also test your program in the second world that is available.

What’s next?

→ Continue with Part 4: Variables


Credits
Planet Cute images by Daniel Cook (Lostgarden.com), published under CC BY 3.0.
Oleg Yadrov improved the “Planet Cute” images and was so kind to let me use them. The images were optimized with the great TexturePacker.
Some exercises in Hello Dartwere inspired by Kara. Kara was developed by Jürg Nievergelt, Werner Hartmann, Raimond Reichert et. al.