Karel Assignment 1 Problems

On By In 1

Posted 10 November 2012 - 12:12 AM

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.

– 4 –

The problem here (as indicated by the "bug" picture next to the code), is that Karel will

not place a beeper on the last corner he encounters. When he reaches the final corner of

the row he's in, his front will no longer be clear, so the while loop will immediately exit

before a beeper is placed on that final corner. This is perhaps easier to see when looking

at the Karel's world after he executes the code above:

Note that in the picture above, there is no beeper on the corner that Karel is standing on.

To solve this example of a "fence-post" problem (such problems are further discussed in

the Karel course reader), we must make one more putBeeper() method call than calls to

move(), as shown below:

private void createBeeperLine() {

while (frontIsClear()) {

putBeeper();

move();

}

putBeeper();

}

Cleaning up scattered beepers

Unfortunately, sometimes Karel's world gets a little messy, with beepers strewn around at

various corners. We want to help Karel clean up his world by writing a program that has

Karel go through the world and pick up any beepers that may be scattered about. We

assume that:

 Karel starts at corner (1, 1) facing East

 Each corner of Karel's world may either be empty or contain at most one beeper, and when Karel is done there should be no more beepers on any corner

 Karel can finish his task at any location and orientation

If you were to execute your program, you would see something like the following before-

and-after diagram on the next page.

0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *