The problem 3 is “In this exercise, your job is to get Karel to create a checkerboard pattern of beepers inside an empty rectangular world, as illustrated in the following before-and-after diagram:”
The way that I viewed the problem is that the robot (Karel) had to move along the either in Up / Down or Left / Right and place a beeper every other step, so in basics if he moves up, he will move up 1 place see if he is able to move any more, if not turn right and if the front is not blocked move forward and right again to go back down the aisle next to the previous one we have just been up and whilst moving up and down the aisle to place a beeper every other step ( so need to keep check of the steps) or you could do the same process but going left and right instead, e.g. move right first to the end of that aisle and then go up if able to and then turn around and move left along the aisle above the previous aisle.
Here is my implementation of both ideas, again the source code for the assignment 1 is attached above in the zip file and also the PDF with the full assignment details.
/* * 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 { private boolean putBeeper = true; private void placeBeeperAndMove() { while (true) { if (putBeeper) { putBeeper(); putBeeper = false; } else putBeeper = true; if (frontIsBlocked()) break; move(); } } private void goUpAndDown() { boolean turnLeft = true; while (true) { if (turnLeft) turnLeft(); else turnRight(); placeBeeperAndMove(); turnLeft = (turnLeft == true ? false : true); if (turnLeft) turnLeft(); else turnRight(); if (frontIsBlocked()) break; move(); } } public void goLeftAndRight() { boolean turnLeft = true; while (true) { placeBeeperAndMove(); if (turnLeft) turnLeft(); else turnRight(); if (frontIsBlocked()) break; move(); if (turnLeft) turnLeft(); else turnRight(); turnLeft = (turnLeft == true ? false : true); } } // You fill in this part public void run() { // go up and down //goUpAndDown(); // go left and right goLeftAndRight(); } } |
Here’s my version … it works but looks nothing like as nice as yours.
hi matdodds, there is no real prefect answer, as long as the program does what is asked then that is prefect answer :)..
the cs106a course is fun 🙂
Another version using help from the lecture files
Subhadeep’s is the best I’ve seen here, and does follow the lecture notes. Remember, we aren’t supposed to use any variables, and it’s generally not good practice to put a “break” in any statement. Break is also not part of Karel’s commands or methods. I’m going to play with this some more and see if I can work out a more efficient algorithm.
Hi Wendy, yeah, Subhadeeps does not use any of the java commands, but the Karel’s ones. Shame on me!!. I consider myself hand slapped !!.
Seriously you are right, in general coding the “break” is not a good way of doing things, while … is better or do … while depending on the setup of the variables that you are wanting to setup.
I shall do a better version without using the java commands.
here you go, funny thing is is that it is better code than before :).
shall also do the part 4 as well, in non java code.
Genux,
Your code (supposed better one) does not work for all worlds!
That is much more efficient, but doesn’t work for odd-sized worlds or 1 column wide rows. That drove me crazy. Here is my program, and instead of putting beepers, I painted them actually red and black.
shall have to correct that then.. not working in funny sized grids, was doing the part 4 and did not check against the different sized grids :(.
This one is abit more long winded, but does all cases that I checked, 1*8, 7*7, 8*1, 8*8, 6*5, 40*40
I know my code has some bad form in it – take for instance:
I’ve tried switching it around so I didn’t have the “if (frontIsBlocked() && facingWest())
{}
”
because that just means “do nothing”…however, it doesn’t seem to work if I put in just the “else” part as the “if”. I’ll have to do a little more debugging.
These exercises are pretty fun, as is watching the videos.
BTW, how do I get that nice, formatted code I see here? Mine doesn’t indent by using just the
tag. Thanks!
Yeah Wendy, the exercises are fun and also watching the video, he is a good bloke to teach IT stuff, I am doing the CS106B course now and also find that interesting as well :). the Lady is very interesting too in the videos :).
I would say for the code, I am using a <pre lang=”java”> … </pre> tags to do the code.
Also what could be a good idea for the code
notice the ! which means not, so if we are not front is blocked and facing west., just a idea.
have fun.. that is what I am doing 🙂
Here is my version (I’ve tried to keep it easy to understand):
Mine:
This was my take on it:
It’s good to have a way to get feedback on these since it’s not exactly like we can turn them in to be graded…
I think from previous posts that if someone does like it, or not, then it kinder gets a mention :). which is very nice since that helps everyone :). keep up the fun of programming everyone 🙂
Another version.
My take
Nice one GrandT
Its stuck in the first loop. What am I missing. thanks in advance for your help.
What do you think of my program? I feel like it’s not very elegant.
HI Frosh, as long as it does it job :). nice sharing 🙂
hi toobrawnlittlebrain, I am guessing it is going around in circles ? you may need to place the second part of the loop under the while loop, so that it can get to the second function call ?
hmmm… here is my code.. I can’t quite figure out how to make it loop until it hits the northernmost wall, so i just used the rowLoop(); a few times until i got just the right number to not go a second time at the top row. If its not in the right code format, im sorry, but i’ve never posted code online before :/
nice post Bryce
This was actually a lot harder than I thought it would be! So here’s the source for anyone else that struggled. This will work in ALL world sizes, and will not produce errors etc. at the end of loops for hitting walls and such…
Whoever moderates, please forgive me for posting in that mess! I did not know of any code tags etc.
nps Jamie Shepherd.. I normally edit the posts to add the tags 🙂
this is my version
For sadistic reasons, I decided to take this iTunesU class which has so far been a lot of fun. After completing my version, I decided to google to see what others came up with as this was more challenging that I had anticipated. Here is the version I created that works in all worlds. Feel free to critique since it’s the only feedback I can get.
It will be much easier and elegent to use the step counts karel have walked at any given position,
if the step count can be divided by 2 (mod 2 = 0) then karel shold put the beeper otherwise go a step furthur.
if karel face the wall when he face east or west he should make a turn until he face the wall at the north.
it cant do the 7 x7 grid ahhhhhhh!!!!! :@
My idea was to start with a base row and build off that. Only being able to use the Karel stuff definitely made this more challenging.
It was hard to me (too).
After solving by myself looked for others’ solutions.
I very like Jamie Shepherd’s (December 14, 2010 at 5:11 pm) and Frosh’s (November 22, 2010 at 4:14 am) solutions.
Here is my version:
My Version 😀
Wow – Well I have to say I am very impressed with Cyvre’s code!
After reading your code Cyvre then looking at mine, I see I have rather a lot to learn and tidy up! Even with the lack of code comments I found it very easy to read and understand.
My efforts appear to be flooded with if statements
Live and Learn I guess!
Oops I seemed to have missed a trick with submitting code too!
Nps, you just need to add in <pre lang=”java”>….</pre> around the code, thanks for sharing.
And here is my version:
Here’s what I did, haven’t written a program for 25 years (FORTRAN!)
/*
* File: CheckerboardKarel.java
* —————————-
* CheckerboardKarel class draws a checkerboard using beepers,
* as described in Assignment 1. Karel starts in the southwest
* corner of the world, facing east. Program works for various worlds.
*/
import stanford.karel.*;
public class CheckerboardKarel extends SuperKarel {
public void run() {
putBeeper();
if (frontIsBlocked()) {
turnLeft();
}
while (frontIsClear()) {
move();
if (frontIsClear()) {
move();
putBeeper();
if (frontIsBlocked()) {
moveToNextStreet();
if (frontIsClear()) {
move();
putBeeper();
}
}
} else {
moveToNextStreet();
if (frontIsClear()) {
putBeeper();
}
}
}
}
/**
* Moves to the next street facing opposite direction
*/
private void moveToNextStreet() {
if (facingEast()) {
turnLeft();
if (frontIsClear()) {
move();
turnLeft();
}
} else {
turnRight();
if (frontIsClear()) {
move();
turnRight();
}
}
}
}