This is the problem 4 “As an exercise in solving algorithmic problems, program Karel to place a single beeper at the center of 1st Street. For example, if Karel starts in the world”. In solving this problem, you may count on the following facts about the world:
- Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number of beepers in its bag.
- The initial state of the world includes no interior walls or beepers.
- The world need not be square, but you may assume that it is at least as tall as it is wide.
- Your program, moreover, can assume the following simplifications:
- If the width of the world is odd, Karel must put the beeper in the center square.
- If the width is even, Karel may drop the beeper on either of the two center squares.
- It does not matter which direction Karel is facing at the end of the run.
To me the answer is when you move from one side to the other and count the number of steps, and then once you hit the right side wall, go back length /2 steps and then place the beeper, and below is my answer in code, once again the zip file attached is the full assignment 1 with each problem and also the PDF file which includes the assignment in full.
/* * File: MidpointFindingKarel.java * ------------------------------- * When you finish writing it, the MidpointFindingKarel class should * leave a beeper on the corner closest to the center of 1st Street * (or either of the two central corners if 1st Street has an even * number of corners). Karel can put down additional beepers as it * looks for the midpoint, but must pick them up again before it * stops. The world may be of any size, but you are allowed to * assume that it is at least as tall as it is wide. */ import stanford.karel.*; public class MidpointFindingKarel extends SuperKarel { private int middleI =0; // find the middle by move across until hitting the end private void findTheMiddle() { while (!frontIsBlocked()) { move(); middleI++; } } // move half the way back, and place the beeper private void placeBeeperInTheMiddle() { for (int i=0; i < (middleI /2); i++) move(); putBeeper(); } // You fill in this part public void run() { findTheMiddle(); turnAround(); placeBeeperInTheMiddle(); } } |
This is my version, yours is so small and good one 🙂
Here is my version with just using the Karel commands and nothing else, e.g. no for loops etc. basically what I do is place beepers across the bottom and then pick one up at each end until there is no beeper next to the beeper that we are on.. e.g. the centre.
Very nice Safi, just is a very nice solution :).
genux yours doesn’t work if the number of avenues are even instead of odd.
shall have to sort that out then :). was trying to do it so that the Karel robot did some funky moves 🙂
Hopefully this will sort out the issue of working in both even/odds, it basically moves the beepers into the middle of the line.
my mantra should be.. test. test. and test again.
Mine:
nice one Jeromy and Safi and Subhadeep, it is great when there is some many ways to solve a puzzle 🙂
nice guys. i like safi’s solution.
Here’s my version using a little recursion:
I went a completely different way i assumed the hint about as least as tall as it is wide meant to go for the diagonal approach
Nice one Dave, as long as it works that is all that matters 🙂 and of course you are having fun along the way 🙂
My take: a different Algorithm
I don’t know if anyone is still looking at this, but here’s my approach using recursion (basic idea: Karel takes one step back for every two steps forward). I’m sure there’s a way to optimize this even more.
not sure how to fix formatting…
Just need to put around the code <pre lang=”java”> hth </pre>
Mine isn’t as clean as everybody’s
in addition, I had to look at everybody’s code to get it right 🙁
I don’t see many using variables as counters for this program. Is there a ruling we aren’t supposed to use a lot of variables?
Hi Zig, do not think so regarding the counters, you can use as many or little as you want to 🙂
Thanks for all the ideas on this page, I’ve been working through the stanford course on itunesU which is amazing. This is the first code I’ve written since 10 Print “Hello” 20 Goto 10 when I was 6 years old 🙂 I copied your examples for problems 2 and 3 and was then able to work this one out on my own. Cheers 🙂
Hey, mine is slightly different, I filled the whole square round the edges to find the middle, then head to the bottom, pick up the beeper from the middle square, and run the program again to invert the states. Uses only the Karel commands we were taught…
How do I sort the formatting out?
Hi freekeys, get post and I have sorted the formatting for you.
hi there,
here is mine. really simple when you see it…