|
BK, Khan Academy is the one that has Python. If you can, you should do the Karel assignments, they're super fun and teaches you more about programming than Java.
and I've just finished the StoneMasonKarel code. I'd love feedback, corrections, improvements, or anything!
/*
* File: StoneMasonKarel.java
* --------------------------
* The StoneMasonKarel subclass fully repairs damaged pillars in Karel's world. It does not * place more than one stone on any corner.
*/
import stanford.karel.*;
public class StoneMasonKarel extends SuperKarel {
public void run() {
turnLeft();
moveToTop();
turnRight();
while (frontIsClear()) {
buildPillar();
turnRight();
moveToNextPillar();
}
buildPillar();
}
/* Pre-condition: Karel is at the bottom of a pillar facing north.
* Post-condition: Karel is at the top of a pillar facing north.
*/
private void moveToTop() {
while (frontIsClear()){
move();
}
}
/* Pre-condition: Karel is at the top of a pillar facing north.
* Post-condition: Karel has returned to the top of the pillar, replacing all missing stones, and facing north.
*/
private void buildPillar() {
turnRight();
while (frontIsClear()) {
if (noBeepersPresent()){
putBeeper();
move();
} else {
move();
}
}
turnAround();
if (noBeepersPresent()){
putBeeper();
}
moveToTop();
}
/* Pre-condition: Karel is at the top of a pillar facing East.
* Post-condition: Karel is at the top of the next pillar facing East.
*/
private void moveToNextPillar() {
move();
while (leftIsClear()){
move();
}
}
}
|