Part 3: Conditionals

Solutions

SOLUTION TASK 3.01: Conditionals

a.

Removes a star if there is one.

b.

Removes a star if there is one, puts down a star if there is none. This means the star pattern is inverted.

c.

Removes the star if there is a tree on the left.

SOLUTION TASK 3.02: Star Track

class MyPlayer extends Player {

  start() {
    while (canMove()) {
      if (!onStar()) {
        putStar();
      }
      move();
    }
    putStar();
  }
}

SOLUTION TASK 3.03: Star at Tree

class MyPlayer extends Player {

  start() {
    while (canMove()) {
      if (treeLeft() || treeRight()) {
        putStar();
      }
      move();
    }
  }
}

SOLUTION TASK 3.04: Around Tree II

class MyPlayer extends Player {

  start() {
    while (!onStar()) {
      if (treeFront()) {
        goAroundTree();
      } else {
        move();
      }
    }

    removeStar();
  }

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

SOLUTION TASK 3.05: Round Trip

class MyPlayer extends Player {

  start() {
    while (!onStar()) {
      if (!treeFront()) {
        move();
      } else {
        if (!treeLeft()) {
          turnLeft();
          move();
        } else {
          turnRight();
          move();
        }
      }
    }

    removeStar();
  }
}

SOLUTION TASK 3.06: Around Tree III

class MyPlayer extends Player {

  start() {
    while (!onStar()) {
      if (treeFront()) {
        goAroundTree();
      } else {
        move();
      }
    }

    removeStar();
  }

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

    while (treeRight()) {
      move();
    }

    turnRight();
    move();
    turnLeft();
  }
}

SOLUTION TASK 3.07: Follow the Trail

class MyPlayer extends Player {

  start() {
    while (!treeFront()) {
      removeStar();
      findNextStar();
    }

    removeStar();
  }

  /// Function to find the next star.
  findNextStar() {
    // First look in the front.
    move();
    if (!onStar()) {

      // No star in front, get back and try on the left.
      turnAndGoBack();
      turnRight();
      move();
      if (!onStar()) {

        // No star on the left, it must be on the right.
        turnAndGoBack();
        move();
      }
    }
  }

  /// Function to go back one step.
  turnAndGoBack() {
    turnLeft();
    turnLeft();
    move();
  }
}

SOLUTION TASK 3.08: Guard

class MyPlayer extends Player {

  start() {
    while (true) {
      makeOneStep();
    }
  }

  makeOneStep() {
    if (!treeRight()) {
      // No tree right --> go to the right.
      turnRight();
      move();
    } else {
      // There is a tree on the right.
      if (!treeFront()) {
        // No tree in front --> make step forward.
        move();
      } else {
        // Bäume rechts und vorne.
        if (!treeLeft()) {
          // No tree left --> go to the left.
          turnLeft();
          move();
        } else {
          // Trees right, in front, and left --> dead end.
          turnLeft();
          turnLeft();
          move();
        }
      }
    }
  }
}

Comments