• Home   /  
  • Archive by category "1"

Assignment 1 Karel Solutions From Science

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.

From the Course Programming Methodology CS 106A Offered at Stanford University

Having spent more hours than I am willing to admit, finally solved Karel the Robot Problem 3 (from Assignment 1).

Please don’t look at the solution until you have solved it yourself. Otherwise you will be depriving yourself of a cool mental accomplishment.

Having said that, here’s my train of thoughts as I was trying to solve the problem:

1. Thought that ignoring the checkerboard might lead to a simpler solution. Think linearly about Karel dropping beepers alternatively on each move “over a single horizontal line” till the end is reached. Later turn the horizontal line into a checkerboard. Made some progress but the solution started getting increasingly complex.

2. Realized that the core of the problem is really the turns Karel needs to make while traversing through the checkerboard. Dropping beepers at alternate locations isn’t the real problem. So I ignored the beeper issue. This lead to a cleaner solution.

3. Karel’s movement itself shouldn’t be in any if/else control statement. It makes a mess of the code because of too many if/else checks. Karel should just move forward. Then figure out which direction it should face. You can see this behavior in lines 26-29 below. Lines 31-49 shows the logic behind setting Karel’s direction.

4. Finally the beeper code is in line 17, 19. Karel moves twice in each iteration of the while loop. Line 19 checks for even-count columns in Karel’s world. Line 21 puts a last beeper for odd-count column worlds.

5. Line 15 checks for a single-count column world.

I Googled for other solutions to compare after solving the problem. Feeling happy that the solution below is 50 lines of code. If you can think of a simpler solution then please let me know.

/* * File: CheckerboardKarel.java * ---------------------------- * When you finish writing it, the CheckerboardKarel class should draw * a checkerboard using beepers, as described in Assignment 1. You * should make sure that your program works for all of the sample * worlds supplied in the starter folder. */ import stanford.karel.*; public class CheckerboardKarel extends SuperKarel { public void run() { if (frontIsBlocked()) turnLeft(); while (frontIsClear()) { if (noBeepersPresent()) putBeeper(); moveKarelForward(); if (frontIsClear()) { moveKarelForward(); if (noBeepersPresent()) putBeeper(); } } } private void moveKarelForward() { move(); setKarelsDirection(); } private void setKarelsDirection() { if (facingEast()) { if (frontIsBlocked()) { turnLeft(); } } else if (facingWest()) { if (frontIsBlocked()) { turnRight(); } } else if (facingNorth()) { if (rightIsBlocked()) { if (leftIsClear()) { turnLeft(); } } else if (leftIsBlocked()) { turnRight(); } } } }

Testing Checkerboard Karel in different worlds:

Like this:

LikeLoading...

Related

One thought on “Assignment 1 Karel Solutions From Science

Leave a comment

L'indirizzo email non verrĂ  pubblicato. I campi obbligatori sono contrassegnati *