Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cupcakes2000

macrumors 68040
Original poster
Apr 13, 2010
3,889
5,307
I’m really stuck on the 3 Gems, 4 Switches part of Learn To Code 2.

Why, in my code below, does the little man turn on four switches and stop (the correct behaviour) but wont stop collecting the gems at 3, and instead just collects them all?

Would love a pointer.

Swift:
var gemCounter = 0
var switchCounter = 0


func gemGem() {
    if isOnGem {
        collectGem()
        gemCounter = gemCounter + 1
    }
    
}
func switchSwitch() {
    if isOnClosedSwitch {
        toggleSwitch()
        switchCounter = switchCounter + 1
    }
    
}




while gemCounter < 3 || switchCounter < 4{
    moveForward()
    gemGem()
    switchSwitch()
    
    if isBlocked && isBlockedLeft {
        turnRight()
        
    }
    if isBlocked && isBlockedRight {
        turnLeft()
        
    }
    
    
        
        
    }
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
Would you like me to more or less say exactly what's wrong or just give some hints? I'll start just offering some hints for you to think about and then we can take it from there if it doesn't help :)

Your while condition says "Everything inside the following block should be executed if there's less than 3 gems collected OR less than four switches activated."

Look at the layout of the level and ask yourself when that condition holds and what will then happen if the condition is true versus if it is false.
 

cupcakes2000

macrumors 68040
Original poster
Apr 13, 2010
3,889
5,307
Would you like me to more or less say exactly what's wrong or just give some hints? I'll start just offering some hints for you to think about and then we can take it from there if it doesn't help :)

Your while condition says "Everything inside the following block should be executed if there's less than 3 gems collected OR less than four switches activated."

Look at the layout of the level and ask yourself when that condition holds and what will then happen if the condition is true versus if it is false.
I’d rather try and work it out! Thanks for the assistance by the way.

My big thing is that I seem to be getting confused with all the true/not true things in this particular example. I can’t see why what looks like two pieces of nearly idenetal code (aside the switch and gen diff) operate so differently. So I came to the conclusion it can’t be that part of the code so it was to be the implementation I have used.

grasping at straws as I can’t work it out, I have swapped around && || In my while condition, tried to remove the moveFoward() from it and put it in a separate for condition.

Im thinking the issue lies within the while condition as you have indicated. But I haven’t fully grasped the concept of it all to be able to solve it.

do you have something even more obvious to help tip me into solving it?
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
I’d rather try and work it out! Thanks for the assistance by the way.

My big thing is that I seem to be getting confused with all the true/not true things in this particular example. I can’t see why what looks like two pieces of nearly idenetal code (aside the switch and gen diff) operate so differently. So I came to the conclusion it can’t be that part of the code so it was to be the implementation I have used.

grasping at straws as I can’t work it out, I have swapped around && || In my while condition, tried to remove the moveFoward() from it and put it in a separate for condition.

Im thinking the issue lies within the while condition as you have indicated. But I haven’t fully grasped the concept of it all to be able to solve it.

do you have something even more obvious to help tip me into solving it?

OK, so the problem is not in and of itself the while condition. The original you have posted isn’t necessarily wrong in that regard, depending how the rest of the code is designed. But you should still be thinking through the condition to understand the problem.
The ENTIRE while block is executed as one unit. You call both gemGem and switchSwitch inside the while block. The while block is executed if you still haven’t flipped 4 switches or collected 3 gems. If either of those conditions aren’t met, BOTH actions are performed if available
 

cupcakes2000

macrumors 68040
Original poster
Apr 13, 2010
3,889
5,307
I can’t get my head around why the switchSwitch function seems to work, even the the switches occur after the gems, and the egmGem function just collects all the gems regardless.

You mention its within the while loop where the problem lies. The one and only hint in the app for this puzzle suggests the || function in the while loop is correct as well as the <3 and <4.

So I must assume its the layout of the code within the loop, but this is a confusing aspect as I’m so new at this. Do I need two while blocks? Thanks again, sorry for my lack of knowledge here! This lack of how it should conceptually work is what’s holding me back I think, I’m finding it hard to even know how to think of the solutions!
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
I can’t get my head around why the switchSwitch function seems to work, even the the switches occur after the gems, and the egmGem function just collects all the gems regardless.

You mention its within the while loop where the problem lies. The one and only hint in the app for this puzzle suggests the || function in the while loop is correct as well as the <3 and <4.

So I must assume its the layout of the code within the loop, but this is a confusing aspect as I’m so new at this. Do I need two while blocks? Thanks again, sorry for my lack of knowledge here! This lack of how it should conceptually work is what’s holding me back I think, I’m finding it hard to even know how to think of the solutions!
So I’m about to leave the house, but I tell you what. I’ll make you a video when I get back where I walk you through the mindset of not just fixing this problem here, but generally how to work your way through code that doesn’t behave how you think it should.

To give you something to think about in the interim, both the switchSwitch and gemGem functions ONLY say “pick up a gem“ and “Toggle Switch”.
The while loop says “Do all this stuff if while either of these conditions is true”
So as it is, you’ll keep trying to pick up gems AND beep trying to turn on a switch, every step you take until you have more than 3 gems AND more than 4 switches toggled.

Where exactly the problem is depends on how you decide to solve it. The problem can be inside the while loop itself, it can also be in both the gemGem and switchSwitch functions; That’s entirely dependent on how you fix it :)

Hope that helps, but as I said, I’ll do you a video when I get home
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
OK quick extra comment before I leave; If the layout of the level had been different, it would act reversed and stop at three gems, but keep turning switches.

Maybe that’ll clear up something too
 

cupcakes2000

macrumors 68040
Original poster
Apr 13, 2010
3,889
5,307
I walk you through the mindset of not just fixing this problem here, but generally how to work your way through code that doesn’t behave how you think it should.
Wow, really? Thanks!

OK quick extra comment before I leave; If the layout of the level had been different, it would act reversed and stop at three gems, but keep turning switches.

Maybe that’ll clear up something too
Ok, so yes then I understand. I was trying to change the essence of the function by adding an else if in to it, but I’m not sure how it write the code.

I tried:

Code:
}else if isOnGem{
gemCounter == 3
moveForward()
}

But it didn’t work, and it actually affected the while loop, stopping the turn left/right if blocked commands.

I’m lost I think. I kind of know what to do to solve it, but I have nearly no idea on how to implement it, code wise.

Move forward and collect gems, when 3 are collected move forwards and without collecting gems.
Move forward and turn on switches, when 4 are switched on stop.

The obvious difficulty I’m having is making the programme realise to stop collecting gems but continue until the rest is finished.
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
Wow, really? Thanks!
Yep :) - Link to video uploaded to my Raspberry Pi web server. Let me know if it doesn't load properly for you :)


Think I cover everything you ask in that post as well; I sort of do just show the solution, but I believe I do it in a manner that explains how to reason about code more generally so you can apply the thought pattern to later exercises and whatnot as well. - Video is hosted off of a home network on a Raspberry Pi. Depending on where you are in the world relative to me, it might take it a sec to load, but if it does not at all let me know and I can try popping it on YT unlisted or something

I will also offer you, like I have many others starting out with code on here, that you can at any time reach out to me through Discord or even iMessage if you want more quick responses on something or even for me to give a bit of a code review or something, pending available time on my end of course. My information is available at (iMessage through the same email as is on there):

 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
OK just a sec if you're reading this immediately, only got a partial video upload there because my Ramdisk wasn't big enough on the Pi, haha. Fixing real quick, link will work in a bit, but right now it stops a bit abruptly before the actual end of the video

UPDATE: All fixed
 
Last edited:

cupcakes2000

macrumors 68040
Original poster
Apr 13, 2010
3,889
5,307
@casperes1996 wow this is amazing of you. I’m very impressed with your dedication to help! I’ve seen it before from you on here with others and you just be commended for it.
I haven’t looked yet because time has got the better of me this weekend and I’m otherwise occupied! I’ll let you know when I get the chance.
 
  • Like
Reactions: casperes1996

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
@casperes1996 wow this is amazing of you. I’m very impressed with your dedication to help! I’ve seen it before from you on here with others and you just be commended for it.
I haven’t looked yet because time has got the better of me this weekend and I’m otherwise occupied! I’ll let you know when I get the chance.
Sure no rush from my end. Happy to help wherever I can and spread the fun of programming :)
 
  • Like
Reactions: cupcakes2000

cupcakes2000

macrumors 68040
Original poster
Apr 13, 2010
3,889
5,307
Sure no rush from my end. Happy to help wherever I can and spread the fun of programming :)
It doesnt seem to want to stream.. I'm in France, so could be the other side of the world, and also away from my place whilst its been renovated and don't have the best internet connection here. Perhaps you could Dropbox it or something? Or youtube as you mentioned, I dont have any trouble streaming from them. What do you think?
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
It doesnt seem to want to stream.. I'm in France, so could be the other side of the world, and also away from my place whilst its been renovated and don't have the best internet connection here. Perhaps you could Dropbox it or something? Or youtube as you mentioned, I dont have any trouble streaming from them. What do you think?

Eh, fair enough. I'm from Denmark so it's not even really that far away, but it can be a bit iffy running a web server from a home network. Sometimes even folk all the way from America can stream my files fine, but closer folk can't and stuff so it's not always just distance - my ISP can also be a bit fluctuating on upload stability. - I'll pop it on YT
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
The video will go live on this link after YouTube processing. Should be available in up to 4K/5K
https://youtu.be/SD-PybrQOXg

They're not super polished but there are some other programming tutorials on that channel as well but they're using Java though talking about generally applicable concepts to varying degrees
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
Hey, just wanted to check up on you and see if you got your head around the problem :) - If you haven’t had time to look more at it, don’t feel rushed or anything. This isn’t meant to stress about it, more just because I’m curious and want to be motivating about it and want you to feel success with it all :)
 

cupcakes2000

macrumors 68040
Original poster
Apr 13, 2010
3,889
5,307
Hey, just wanted to check up on you and see if you got your head around the problem :) - If you haven’t had time to look more at it, don’t feel rushed or anything. This isn’t meant to stress about it, more just because I’m curious and want to be motivating about it and want you to feel success with it all :)
Yes thanks! I watched it last night, very very well explained and I feel like I understand a bit more about it. I haven’t implemented it yet in playgrounds, as I havent had any time to play with it. I’m going to watch the other stuff on that channel also. I’m very inspired to do this, and you have helped with the motivation of it. The problem with me and problems like these types of problems is I always feel theyre out of reach if I can’t solve them immediately. Even though I may not become an ace programmer, learning it will be a great achievement.
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
Yes thanks! I watched it last night, very very well explained and I feel like I understand a bit more about it. I haven’t implemented it yet in playgrounds, as I havent had any time to play with it. I’m going to watch the other stuff on that channel also. I’m very inspired to do this, and you have helped with the motivation of it. The problem with me and problems like these types of problems is I always feel theyre out of reach if I can’t solve them immediately. Even though I may not become an ace programmer, learning it will be a great achievement.

Good to hear :) - And as I said I don't want to rush you either, take it in a pace where it's fun for you and all. I just feel a bit of a vested interest in helping out all the way :)

I'm sure you can solve all the challenges with time and practice - nobody starts out an expert :)
 
  • Like
Reactions: cupcakes2000
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.