Karel The Robot – My robot is not stopping at five beepers as it should

My task is to:

Karel goes forward

  • If karel hits 1 beepers, he picks it up and turns to the north, picks up all the beepers
  • If 2 beepers, turns to the west, picks up all the beepers
  • If 3 beepers, turns to the south, picks up all the beepers
  • If 4 beepers, turns to the east, picks up all the beepers
  • If 5 beepers, stops, picks up all the beepers.

All of those are working in my code, but when it's at five beepers, it picks them up and continues moving. What is the problem in my code?

int main()
{
    turn_on("task_6.kw");
    set_step_delay(100);

    while (!five_beepers())
    {   
        while (no_beepers_present())
        {
            step();     
        }
        check_beepers();
    }

    turn_off();
    return 0;
}

void check_beepers()
{
    
    if (beepers_present())
    {
        pick_beeper();
        if (no_beepers_present())
        {
            turn_north();
        }
        else
        {
            pick_beeper();
            if (no_beepers_present())
            {
                turn_west();

            }
            else
            {
                pick_beeper();
                if (no_beepers_present())
                {
                    turn_south();
                }
                else 
                {
                    pick_beeper();
                    if (no_beepers_present())
                    {
                        turn_east();

                    }
                
                        
                }
            }
        }

    }
}
void turn_north()
{
    while (not_facing_north())
    {
        turn_left();
    }
}

void turn_west()
{
    while (not_facing_west())
    {
        turn_left();
    }
}

void turn_south()
{
    while (not_facing_south())
    {
        turn_left();
    }
}


void turn_east()
{
    while (not_facing_east())
    {
        turn_left();
    }

}

bool one_beeper()
{
    if (beepers_present())
    {
        pick_beeper();
        if (no_beepers_present())
        {
            return true;
        }
    }
    return false;
}

bool two_beepers()
{
    if (beepers_present())
    {
        pick_beeper();
        if (beepers_present())
        {
            pick_beeper();
            {
                if (no_beepers_present())
                {
                    return true;
                }
            }
        }
    }
    return false;
}

bool three_beepers()
{
    if (beepers_present())
    {
        pick_beeper();
        if (beepers_present())
        {
            pick_beeper();
            if (beepers_present())
            {
                pick_beeper();
                if (no_beepers_present())
                {
                    return true;
                }
            }
        }
    }
    return false;
}

bool four_beepers()
{
    if (beepers_present())
    {
        pick_beeper();
        if (beepers_present())
        {
            pick_beeper();
            if (beepers_present())
            {
                pick_beeper();
                if (beepers_present())
                {
                    pick_beeper();
                    if (no_beepers_present())
                    {
                        
                        return true;    
                    }
                }
            }
        }
    }
    return false;
}


bool five_beepers()
{

    if (no_beepers_present())
    {
        return false;
    }
    if (beepers_present())
    {
        pick_beeper();
        if (beepers_present())
        {
            pick_beeper();
            if (beepers_present())
            {
                pick_beeper();
                if (beepers_present())
                {
                    pick_beeper();
                    if (beepers_present())
                    {
                        pick_beeper();
                        if (no_beepers_present())
                        {
                            return true;
                        }return false;

                    }return false;
                }return false;
            }return false;

        }
        return false;
    }
    return false;
}


void turn_right()
{
    turn_left();
    turn_left();
    turn_left();
}


Read more here: https://stackoverflow.com/questions/67920946/karel-the-robot-my-robot-is-not-stopping-at-five-beepers-as-it-should

Content Attribution

This content was originally published by patr9k at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: