#move beetle to leaf

1 messages · Page 1 of 1 (latest)

rancid frigateBOT
#

<@&987246399047479336> please have a look, thanks.

rancid frigateBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

thin nebula
#

Why Do you have the inner if two Times?

velvet bay
#

if theres a Tree Right / Tree Left and if theres no Tree Right = no Tree Left

jolly flare
#

like, just look at the picture:

#

and tell me how ur beetle works

#

without going into detail on the code

#

is it trying to hug the right wall?

#

like this?

velvet bay
#

yeah i'm trying to hug the right wall

jolly flare
#

or whats ur "idea"?

#

so like the above pic?

velvet bay
#

yeah

jolly flare
#

okay. then lets break down the logic for that

#

its a priority based system

#

the priority is to go right if possible

#

if thats not possible, u want to go forward

#

if thats not possible, left

#

if thats not possible, back

#

so:

#
  1. right
  2. forward
  3. left
  4. back
#

this "logic" should be visible in ur code exactly like that

#

are u familiar with creating ur own methods already?

#

like creating helper methods

#

are u familiar with enums already?

velvet bay
#

i'm familiar with creating void statements, enums i'm assuming is i < 0 etc ?

#

sorry i'm fairly new

jolly flare
#

no worries. im just asking, otherwise i cant suggest u a solution/approach thats familiar to u

#

enums are this:

enum Weekday {
  MONDAY,
  TUESDAY,
  WEDNESDAY,
  ... // im too lazy, lol
}
#

and then u can use them in ur code

#

cause that way we could create ourselves a neat little helper method that would look like

#
boolean result = tryMove(Direction.LEFT);
#

but if u didnt learn that yet, we can do it differently as well

rancid frigateBOT
#

move beetle to leaf

#

Changed the title to move beetle to leaf.

velvet bay
#

i'm not sure if enum is allowed in the code

jolly flare
#

alright

velvet bay
#

but boolean statements should be legal

jolly flare
#

yeah, no worries

#

lets just write it all in one method for today

#

are u familiar with continue?

velvet bay
#

yeah

jolly flare
#

perfect

#

so the first technique u should know is "early-return". or in this case "early-continue". it will increase readability of ur code

#

instead of writing:

#
while (...) {
  if (...) {
    ...
    if (...) {
      ...
      if (...) {
        ...
        if (...) {
          ...
        }
      }
    }
  }
}
#

u can write:

#
while (...) {
  if (...) {
    ...
    continue;
  }
  if (...) {
    ...
    continue;
  }
  if (...) {
    ...
    continue;
  }
  if (...) {
    ...
    continue;
  }
}
#

that way u reduce the nesting of ur code dramatically

#

which makes it much more readable in general

velvet bay
#

just to clarify the continue statement is similar to a break but i can keep writing functions

jolly flare
#

break ends the loop

#

continue only ends the current iteration

#

and goes into the next iteration of the loop

#

example:

// instead of this
void buySomeGames() {
  if (hasMoney()) {
    buyGame();
    if (hasMoney()) {
      buyAnotherGame();
    }
  }
}

// do this
void buySomeGames() {
  if (!hasMoney()) {
    return;
  }
  buyGame();

  if (!hasMoney()) {
    return;
  }
  buyAnotherGame();
  }
}
#

so this is the early-return idea in a more concrete example

velvet bay
#

so would this be ok?
if (treeFront()) {
if (!treeRight()) {
turnRight();
move();
else {
turnLeft();
continue;
}
if (!treeRight()) {
turnRight();
move();
}
else {
move();
continue;

jolly flare
#

wait on that please

#

ur still nesting too much

#

and the priority is not clearly visible in ur code

#

its hard to understand that logic

#

so ur loop is correct.

while (!onLeaf()) {
  ...
}
#

now lets start with the first priority

#

which was moving to the right

velvet bay
#

yep

jolly flare
#

mh actually

#

now that i think about it

#

i doubt we can get rid of nesting

#

oh wait

#

we can

#

okay

#

so lets move to the right, if possible

velvet bay
#

if we can get the bettle to hug right wall it will reach the end

jolly flare
#
if (!treeRight()) {
  turnRight();
  move();
}
#

this is the move-right logic

#

so far so good?

velvet bay
#

yep following along (thx for the help :D)

jolly flare
#

"if we can move to the right, lets move to the right"

#

now, we realize that if we moved to the right, there is no work left to do anymore

#

we are done

#

with this iteration of the loop

#

so... we continue;

#
while (!onLeaf()) {
  if (!treeRight()) {
    turnRight();
    move();
    continue;
  }

  ...
}
#

and now u can put the next block

#

directly next to it

#

without nesting

#

since u got an implicit else now

#

whatever u write at the ... can only be executed if we didnt move to the right yet

#

cause otherwise the continue would have ended the iteration already

#

so this is like using else, just without using it

#

which improves readability, since we have a level of nesting less

#

still following along, or is it confusing?

#

the main readability issue of ur code is the nesting. so this is a very common technique to improve on that

velvet bay
#

the beetle immediately turns right, I need to find a way to make her move beforehand

jolly flare
#

thats okay

#

its expected

#

the logic is correct like that

#

remember the list of priorities

#
  1. right
  2. forward
  3. left
  4. back
#

theres no point in executing the code yet

#

its not done yet

#

but we cant continue with the logic if its not 100% clear to u now

#

cause the next piece, u have to write urself

velvet bay
#

i'm having a hard time comprehending why right is more important than forward

#

when writing loops isnt thinking abotu the end important?

jolly flare
#

we can also change the priority list. but that was the idea we had initially

#

otherwise the movement is different

jolly flare
#

u said that this is correct:

#

but with "first move forward", u get a different movement

#

i dont mind how we do it

languid lava
#

If you don't look to the right, you won't be able to stick to the right wall

jolly flare
#

if u first move forward, u get this

#

and will be stuck

velvet bay
#

Ah I understand that

jolly flare
#

u can also see nicely here how to approach algorithms like this

#

u have to solve it on paper first

#

without thinking about code

#

exercise it step by step on paper

#

as seen in those pics

#

before attempting to code anything

#

alright. did u get the logic with the continue fully?

#

would u be able to put the next piece at the right place already?

#
while (!onLeaf()) {
  if (!treeRight()) {
    turnRight();
    move();
    continue;
  }

  ...
}
velvet bay
#

I understand that function is over and continues with next

jolly flare
#

so can u put the next piece in the code, or dont u fully understand it yet?

#

is sth unclear?

velvet bay
#

while (treeRight()) {
move();

jolly flare
#

that doesnt look like it makes sense to me

#

whats the next piece on the list, logic-wise?

#

our list was:

  1. move right if possible, if not:
  2. move forward if possible, if not:
  3. move left if possible, if not:
  4. move back
velvet bay
#
  1. is not possible 2. is with what i wrote
jolly flare
#

the task is to move one tile

#

not multiple

#

we are still doing only a single step

#
while (!onLeaf()) {
  if (!treeRight()) {
    turnRight();
    move();
    continue;
  }

  ... // <- we are here
}
#

okay, lets maybe introduce a helper method to clear ur confusion

velvet bay
#

if (!treeFront()) { move(); ?

jolly flare
#

u said u know how to create a simple void method

#

so lets do that

#
public static void moveOneStep() {
  ...
}
#

we will write this method

#

and then our core logic looks like this:

#
while (!onLeaf()) {
  moveOneStep();
}
#

and thats it

#

alright?

velvet bay
#

hows this? void claraMove() {
while (!onLeaf()) {
move();

jolly flare
#

nope

#

then u just move forward

#

and nothing else

#

whats unclear about what i just said?

#

i dont have the feeling that ur really sticking with me for this

velvet bay
#

i'm confused as to what to do after the continue statement

#

sorry

jolly flare
#

but thats not what we are doing now

#

ur rushing ahead

velvet bay
#

we're creating a void statement for that

jolly flare
#

and nothing else

velvet bay
#

oh nothing else

#

my bad i didnt understand that

jolly flare
#

nothing else yet

velvet bay
#

ok same page now

jolly flare
#

is this piece of code fully clear

#

or is it unclear

velvet bay
#

yep

jolly flare
#

okay. then we can now talk about implementing the moveOneStep method

#

can u describe in words what its supposed to do

#

whats the goal for this method

velvet bay
#

the goal of this is to get the bettle to move forward

#

if its not a leaf (which it isnt until the end)

jolly flare
#

well, thats the overall context

#

but not what this particular method would do

#

the method doesnt deal with leaves at all

#

thats done above the method

#

please be a bit more precise

#

try again 🙂

#

lets say we stare at this picture:

#

if we just call moveOneStep(); without anything else, just once

#

where is the beetle supposed to be after that?

velvet bay
jolly flare
#

nope

#

but i feel like we are now getting to ur misunderstanding

#

so thats good

#

look at the name of our method

#

maybe u can deduct it from there

velvet bay
#

i have to use the statements listed in the top left of the original screenshot

#

so i can't make the moveonestep statement

jolly flare
#

its a helper function

#

why cant u introduce another function

velvet bay
#

you can only use a mix of the listed functions

jolly flare
#

or create ur own methods

#

next to it

velvet bay
#

you can create your own method but only using the existing functions

jolly flare
#

yeah, but we will do that

velvet bay
#

the only available one is move();

jolly flare
#

nope

#

moveOneStep() is a method we will write ourselves, which will use move() and other things

#
class MyClara extends Clara {
  void run() {
    while (!onLeaf()) {
      moveOneStep();
    }
  }

  void moveOneStep() {
    ...
  }
}
#

or are u not familiar with writing ur own methods yet?

velvet bay
#

while (!onLeaf())

jolly flare
#

yes, my bad

velvet bay
#

i mean I don't understand what statement would make me only one move if (treeleft && treeright??)

jolly flare
#

what do u mean

#

move() moves a single tile

#

not all the way forward until bonk

#

does it

velvet bay
#

yes move would work

#

i agree

jolly flare
#

but thats not including the turn logic yet

#

ur rushing ahead again

#

the point was that we introduce a helper method where we will move most of our logic into

#
class MyClara extends Clara {
  void run() {
    while (!onLeaf()) {
      moveOneStep();
    }
  }

  void moveOneStep() {
    ...
  }
}
#

so that the core logic is simple to understand

#

especially for u, so that u dont get confused

#

if helper methods confuse u, we can get rid of it again

#

i thought it will simplify it for u. but im not so sure anymore now

velvet bay
#

i'm going to work on it alone, write it down on some paper and break it down - i have to go now - sorry for finding an understanding

#

thanks for your help

#

really appreciate it

jolly flare
#

mh. alright