# Karel Assignment 1 Problem 4-4

By:
posted 5 years ago

Hi everyone,

I've done quite a bit of searching to find a forum that might actually help me out and have thus come here.

Some explanations first, this is not homework, well not in the traditional sense. This is a free online course, I have no way to submit my work, get feedback, or ask questions (if I could I probably wouldn't be here). From what I understand I actually missed the proper free online course and am just following the course material but as it is I don't think the people following it properly were able to submit there work or ask questions etc.

Anyway to the problem at hand, I will post some links they are not entirely necessary just there if you want to read them and make sure I know what I'm talking about. I've had a lot of problems posting links and then people not helping because they aren't willing to read that much so...

http://see.stanford.edu/materials/icspmcs106a/07-assignment-1-karel.pdf This links you to the assignment document, page 5 is the problem I'm on that I'm having problems with.

The problem given is you need to find the midpoint of a world. So after thinking over it for ages and not knowing how to do it, the idea of a diagonal was constantly stuck in my head, then it came to me. I could make a huge X, but then I realised what if the world is rectangle (this I realised at midnight). So the next day I read over the assignment again to discover one of the conditions says the shape may not always be square, but I read on to find another condition which was the world will always be as tall as it is wide thus a square. So I coded the X. This was all good and well as I continued to code to find the midpoint which was problematic enough as it is. Now though I've discovered wait this doesn't work for 6x6 or 8x8 worlds as there isn't a centre column.

So here we are I'm trying to code a way to find the centre in an even number world. Now before you start suggesting easier ways to start again I'm going to post a link to all the command I know as this is the Karel programming language which is technically Java but dumbed down basically. http://www.stanford.edu/class/cs106a/book/karel-the-robot-learns-java.pdf The last page has what code I know and am allowed to use.

Now my code:

That's the problem code, if you want to read all of my code here is a pastebin link to it: http://pastebin.com/GbQdj2qC.

Basically my idea to solve my problem was whenever it detects a second beeper in a column it would turn left if facing north or turn right if facing south and move forward one and then check if there is a beeper there. If there wasn't it would turnaround move back into position and continue on checking. If there was a beeper that was then the centre in a even world as there would be a group of four beeper at the centre. Instead though it goes to the second beeper in the second column checks and straight thinks there is a second beeper there when there isn't...I have no idea why or what is going wrong and would really appreciate any help I can get.

I've asked for help at Java Forums, OCAU, AtomicMPC, StackOverflow, Binary Revolution and have gotten a couple nice replies but not really any actual help, besides 1 guy from Atomic who I haven't heard from since.

So any help is REALLY appreciated.

Thanks Jack.

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.