Jump to content
Sign in to follow this  
TheSingularity

Help, Having Difficulties With the Stanford CS106A Assignments

Recommended Posts

Hey all,

 

So I've been doing the Stanford CS106A course. The only problem is the assignments aren't terribly hard but still hard enough for me to get stuck in certain parts. I will copy paste (and edit it appropriately) some of what I posted in another forum which was for getting some help but so far haven't even gotten a reply (haven't given it long but I'm being impatient).

 

Another query before I get into it is do you have Steam if so and you can help add me so we can talk via Steam's instant messaging service which makes things easier in my opinion. My username for Steam is Powermaniac.

 

A link to the site where I'm learning this is here: http://see.stanford.edu/see/courseinfo.asp...aa-866adcae1111 So far I'm stuck on 2 separate assignments that you can read here: http://see.stanford.edu/materials/icspmcs1...ent-1-karel.pdf on pages 3 and 4. Basically I've finished the first assignment on page 3 but I get a bug at the end of it every time, it completes the job but it encounters a bug. Which is it walks into the wall and 'dies' as it doesn't stop and keeps running regardless of the fact there is no more world to run in. The code for that can be read here: http://pastebin.com/JxDUsUNP. The second one works in the original world but doesn't work in a 1x8 world or 7x7 world and 'dies' at the end as it either makes columns instead of a checkerboard (7x7 world) or turns and walks into a wall and thus 'dies' (1x8 world). Code for that can be read here:http://pastebin.com/ZtRDywmC.

 

Anyway I've had Cyb3rGlitch helping me at times but I don't think he has the time nor wants to be answering my every beck and call. Cyb3rGlitch also pointed me at the Stanford course which has been great, least convoluted course I've tried yet, first one where I've actually gotten anywhere and stuck with it. As for example I own Java A Beginner's Guide Fifth Edition which seems to be missing things, I've tried CodeCademy, Udacity's CS101 course, among other courses I've somehow forgotten at this point in time. Anyway Cyb3rGlitch was also helping me out but tried not to give me any of the answers and had me think the problem through, so I did and do but sometimes no matter how much thinking I still can't work it out and changing snippets of code to see if it fixes the problem. Such as at the moment the fact on certain maps (worlds) it doesn't make a checkerboard it makes columns or it walks into the wall and 'dies'. So I appreciate your help Cyb3rGlitch it's just I have a feeling you don't want to be helping me constantly and thus I'm trying multiple avenues so I have multiple sources of help and thus can progress faster and not fill 1 persons time too much.

 

So yes any help is appreciated and well thanks for reading this far.

Edited by TheSingularity

Share this post


Link to post
Share on other sites

Hey Singularity, I've added you as a friend on Steam (berrigan) .. so if this doesn't help, we can try to find some time to chat. ;-)

 

---

 

It looks to me like you're having a problem splitting the problem into repeatable bites.

 

I should likely ask first, have you been taught about recursion, and are you intending on using it?

I'm 50/50 on this, since these problems CAN lend themselves well to such, BUT most programming courses I've seen don't approach it how you are .. it seems more accidental from your code listings.

 

To explain, you have a two functions (problem 4), called makeRowEast & makeRowWest.

 

- Each of these will make a row across the world, dropping blocks along the way, then when it can't move forward, calls moveUpAndTurnAround.

- moveUpAndTurnAround does what it says, and then calls the other row making function, makeRowWest/makeRowEast.

- After THIS call to makeRowWest is run, it also calls moveUpAndTurnAround.

- moveUpAndTurn around runs and calls the first makeRow.

- this calls moveUpAndTurn .. which then calls a Row, which then calls MoveUp, which then calls a makeRow ... etc,etc,etc.

 

Each iteration, you are going further down the rabbit hole.

I use iteration so you can see the loop... I'm assuming you've done loops.

 

As a general rule, a lot of problems can be done with loops or recursion.

Recursion is usually similar to continued iteration loops with a slightly different loop state .. the hard part is making sure you catch the state where the recursion should stop, and you should stop going further down the rabbit hole.

 

It doesn't look like you've made this with this in mind though... I'm not seeing an intentional escape clause.

BUT, that could also be because your moveUpAndTurn around function has a logic error .. it makes an assumption which won't always be true.

 

---

 

A small tip as well, sometimes it helps to program for some special cases straight from the bat.

In this case, image a 1x8, and 8x1 world ... they are very similar .. just depends which way you're facing! ;-)

 

---

 

For problem 3 ... I'll go over the start of your program.

 

Start program -> columnBuilding -> placeColumn ->

{ (in function)

 

IF no beeper put beeper

ELSE move 4 and put beeper if no beeper (side note: you're assuming there will be more than one column)

while not blocked, move 4 and if no beeper, put beeper

 

....

}

 

 

- It appears you're trying to organise your iterations/looping sideways ...

- It says columns, which usually implies that it's up/down, and I'm guessing that's the case here, given fact 4 : "The top of the column is marked by a wall, but Karel cannot assume that columns are

always five units high, or even that all columns are the same height." & looking at the map a column would then be the 5 stones which are lined up/down the world @ 1,5,9,13.

 

Sometimes, re-assessing the problem can be helpful instead of pondering more over the not working current state of the solution.

 

- Given the above, you may have better luck making a function which can generate a column, and leave you back at the start facing east again.

Share this post


Link to post
Share on other sites

Well a list of all the commands I've been taught for Karel (Karel is a minimised version of Java to teach some basics to start off with and style and then move into Java itself). So I do not believe I know recursives or loops yet.

 

http://www.stanford.edu/class/cs106a/book/...learns-java.pdf page 39 has a list of all the commands I know among other things.

 

Now I think about it your solution to problem 3 (which is actually problem 2 =P) would probably work better and I know just how to do it so =D.

 

Problem 2 (which is problem 3) is the problem one though...

 

Otherwise I'm currently on Steam so talking about the rest of it now would be great, if possible.

Edited by TheSingularity

Share this post


Link to post
Share on other sites

I don't have time to read the spec but in the first link to your code you are calling columnBuilding() from within itself. That's recursion. (Similarly, in the second link, you are calling a function, which calls the calling function, and so on, which is also recursion.)

 

Without a condition such that subsequent calls will eventually stop, a recursive function will just continue calling itself.

 

So, you need to put in a stopping condition basically such that subsequent calls will converge to it.

 

From your OP, they should probably be stopping just before there is "no more world".

 

Also, not sure if you meant it, but columnBuilding() will be called twice when facingEast() and frontIsClear() are true.

Share this post


Link to post
Share on other sites

Yeah I haven't actually been taught that, that is recursion yet.

 

I also know there is no 'stop' function and thus the only stop function is to have either Karel 'die' or to somehow get Karel to do an action that just occurs and then ends there.

 

The code has also changed quite a bit again already, as I continued to go over it afterwards. StoneMasonKarel. CheckerboardKarel

Share this post


Link to post
Share on other sites

Now that you've got your own code done, thought you might like this ...

http://pastebin.com/wgkR8ekt

 

:-)

-___-, very neat version of StoneMasonKarel...

 

I've been working on CheckerboardKarel while I wait for you to come online and I fixed it basically but it just dies when it walks into a wall. Quite a lot of code though so it looks messy. Anyway looking forward to working together on CheckerboardKarel next and then hopefully MidpointKarel.

 

CheckerboardKarel.

 

My final StoneMasonKarel.

Share this post


Link to post
Share on other sites

Yeah, sorry I haven't really been on.

 

I think you have enough idea to do the other two, with a little playing around, so just a couple of ideas/suggestions.

 

- I think you have too much going on in your checkerboard one ... feels like you need to start over, and figure out and algorithm in your head and on paper first.

 

- Your observation about the diagonals for the midpoint one is very good... I was even wondering how I would do that one before I saw that!!

Share this post


Link to post
Share on other sites

The CheckerboardKarel took a fair bit of trial and error, it now works in a 7x7 and 8x1 world. Although I just had idea on how to make it shorter so I shall go write that down.

 

Before that though the MidpointKarel I was having a hard time thinking even how to do it. I first thojght 1 diagonal line but then wasn't sure where to go with it and decided it didn't work. I then got fed up thinking about how to do it so I looked at the Stanford solution on Github. This morning I finally thought I might have a soljtion using the diagonal idea then realised if the shape was anything but square it probably wouldn't work...Then I read the pdf again and discovered the shape may not always be a square but you can assume it will always be as tall as wide thus a square...So I tried writing the code but encountered a couple problems. Particularly checking each row for 2 beepers and finding and stopping at the row with 1 beeper. I will solve cleaning up the beepers afterwards and placing the midpoint beeper at the base. So that is where I need some help.

Share this post


Link to post
Share on other sites

Are you actually doing the course? Or just following the course? ... if you know what I mean.

 

Also, I'll say this, with the algorithm I've got anyway, there's only 1 diagonal line of beepers .. just need to hit that line again! ;-)

 

Of course, you don't have to do it one way or another, a working solution is merely one that works in this case.

Share this post


Link to post
Share on other sites

Well I'm not submitting work because I can't as I'm not actually going to Stanford whether in-person or online. And I don't think the free online students ever could. So I guess you could say I'm following it but in my opinion I am taking the course just the free online one.

 

1 diagonal but you have to change directions. So 1 diagonal code wouldn't work unless I'm missing something. Maybe facingEast, facingWest otherwise...

Share this post


Link to post
Share on other sites

Think about a square. If you want to find the exact middle of a square without measuring, how would you do it?

 

Also, I said 1 diagonal line of beepers ...

Share this post


Link to post
Share on other sites

I'm a bit all over the place at the moment, so sorry about that.

 

Yeah, you should look at the commands you have available to you in regards to the detecting diagonal line.

Share this post


Link to post
Share on other sites

My current code is a pain I'm so close I can feel it but I just can't think how to fix it or re-write it. Link to current code: http://pastebin.com/YDvzdPpF.

 

Basically I'm stuck with the code checkForLoneBeeper and checkForSecondBeeper. I want Karel to detect the first beeper in the column and keep walking until Karel walks into the wall. Unless Karel detects a second beeper and then Karel should go to the next column and start again. If Karel detects only 1 beeper and walks into the wall I want Karel to turn around pickup the beeper and place it at the bottom of the world. Then go and clean up the rest of the beepers in the world. So far it isn't working though and no matter how I adjust the code Karel keeps walking even though Karel has detected only 1 beeper which is the midpoint I'm trying to find.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×