#move beetle to leaf
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
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.
Why Do you have the inner if two Times?
if theres a Tree Right / Tree Left and if theres no Tree Right = no Tree Left
i dont quite follow ur logic. could u explain it in simple words quickly?
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?
yeah i'm trying to hug the right wall
yeah
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:
- right
- forward
- left
- 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?
i'm familiar with creating void statements, enums i'm assuming is i < 0 etc ?
sorry i'm fairly new
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
i'm not sure if enum is allowed in the code
alright
but boolean statements should be legal
yeah, no worries
lets just write it all in one method for today
are u familiar with continue?
yeah
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
just to clarify the continue statement is similar to a break but i can keep writing functions
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
so would this be ok?
if (treeFront()) {
if (!treeRight()) {
turnRight();
move();
else {
turnLeft();
continue;
}
if (!treeRight()) {
turnRight();
move();
}
else {
move();
continue;
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
yep
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
if we can get the bettle to hug right wall it will reach the end
if (!treeRight()) {
turnRight();
move();
}
this is the move-right logic
so far so good?
yep following along (thx for the help :D)
"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
the beetle immediately turns right, I need to find a way to make her move beforehand
thats okay
its expected
the logic is correct like that
remember the list of priorities
- right
- forward
- left
- 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
i'm having a hard time comprehending why right is more important than forward
when writing loops isnt thinking abotu the end important?
we can also change the priority list. but that was the idea we had initially
otherwise the movement is different
the end is "when we reached the leaf"
u said that this is correct:
but with "first move forward", u get a different movement
i dont mind how we do it
It's a primitive maze strategy "stick to the wall"
If you don't look to the right, you won't be able to stick to the right wall
Ah I understand that
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;
}
...
}
I understand that function is over and continues with next
so can u put the next piece in the code, or dont u fully understand it yet?
is sth unclear?
while (treeRight()) {
move();
that doesnt look like it makes sense to me
whats the next piece on the list, logic-wise?
our list was:
- move right if possible, if not:
- move forward if possible, if not:
- move left if possible, if not:
- move back
- is not possible 2. is with what i wrote
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
if (!treeFront()) { move(); ?
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?
hows this? void claraMove() {
while (!onLeaf()) {
move();
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
we're creating a void statement for that
nothing else yet
ok same page now
yep
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
the goal of this is to get the bettle to move forward
if its not a leaf (which it isnt until the end)
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?
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
i have to use the statements listed in the top left of the original screenshot
so i can't make the moveonestep statement
you can only use a mix of the listed functions
you can create your own method but only using the existing functions
yeah, but we will do that
the only available one is move();
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?
while (!onLeaf())
yes, my bad
i mean I don't understand what statement would make me only one move if (treeleft && treeright??)
what do u mean
move() moves a single tile
not all the way forward until bonk
does it
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
