Search code examples
javakarel

Becker Robots: Getting them to move


So I have to use the becker.robots package to move forward and pick up a certain amount of flashers and then turn around and place them. However, I am not sure how to invoke the robot.move() method. Everytime I try to make it move forward I get a compiler error saying :

Error: method move in class becker.robots.Robot cannot be applied to given types; required: no arguments found: int reason: actual and formal argument lists differ in length

Could someone please help me :)


Solution

  • Moving a robot in Karel/Becker can only move a single step at a time. By design, of course.

    From the documentation.

    If you wish to move 6 spaces forward, you will need to do a for loop:

    for(int i = 0; i < 6; i++) {
        robot.move();
    }
    

    Or call robot.move() 6 times:

        robot.move();
        robot.move();
        robot.move();
        robot.move();
        robot.move();
        robot.move();
    

    Alternatively, can create a method to move it multiple times.

    void customMove(int move) {
        for(int i = 0; i < move; i++) {
            robot.move();
        }
    }
    

    Then a call of customMove(6); will move Karel forwards 6 times.

    Obviously, to avoid breaking Karel, you should check if it's clear before moving, but this is a conceptual design for movement.