Created
October 28, 2011 04:22
-
-
Save NatashaTheRobot/1321614 to your computer and use it in GitHub Desktop.
This is the solution to the StoneMasonKarel problem in the online Stanford CS 106A class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: StoneMasonKarel.java | |
* -------------------------- | |
* The StoneMasonKarel subclass as it appears here does nothing. | |
* When you finish writing it, it should solve the "repair the quad" | |
* problem from Assignment 1. In addition to editing the program, | |
* you should be sure to edit this comment so that it no longer | |
* indicates that the program does nothing. | |
*/ | |
import stanford.karel.*; | |
public class StoneMasonKarel extends SuperKarel { | |
public void run () { | |
while (frontIsClear()) { | |
BeeperUp(); | |
MoveUp(); | |
BeeperDown(); | |
MoveDown(); | |
} | |
} | |
private void BeeperUp() { | |
turnLeft(); | |
while (frontIsClear()) { | |
if (noBeepersPresent()) { | |
putBeeper(); | |
} | |
move(); | |
} | |
} | |
private void BeeperDown() { | |
turnRight(); | |
while (frontIsClear()) { | |
if (noBeepersPresent()) { | |
putBeeper(); | |
} | |
move(); | |
} | |
} | |
private void MoveUp() { | |
turnRight(); | |
for (int i=0; i<4; i++) { | |
move(); | |
} | |
} | |
private void MoveDown() { | |
turnLeft(); | |
if (frontIsClear()) { | |
for (int i=0; i<4; i++) { | |
move(); | |
} | |
} | |
} | |
} |
import stanford.karel.*;
public class StoneMasonKarel extends SuperKarel {
public void run(){
while(frontIsClear()){
repairColumns();
moveToNextColumns();
}
repairColumns();
}
private void repairColumns(){
if(rightIsBlocked()){
turnLeft();
}
else {
turnRight();
}
while (frontIsClear()){
if(noBeepersPresent()){
putBeeper();}
move();
}
}
private void moveToNextColumns(){
if(facingNorth()){
turnRight();
}
else {
turnLeft();}
for (int i = 0; i < 4 ; i++){
move();
}
}
}
/* Hey Check out my solution for Stone Mason Karek the robot problem */
import stanford.karel.*;
public class StoneMasonKarel extends SuperKarel {
public void run() {
turnLeft();
moveForward();
turnRight();
moveIt();
putBeeper();
turnRight();
moveForward();
turnLeft();
moveIt();
putBeeper();
turnLeft();
moveForward();
turnRight();
moveIt();
turnRight();
moveForward();
turnLeft();
}
private void moveForward() {
for (int i = 0; i <= 3; i++) {
if (beepersPresent()) {
move();
} else {
putBeeper();
move();
}
}
}
private void moveIt() {
for (int i = 0; i <= 3; i++) {
move();
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import stanford.karel.*;
public class StoneMasonKarel extends SuperKarel {
public void run() {
}
//while on the button turnLeft ,on the top turnRight
private void findTheWay() {
if(leftIsBlocked()){
turnRight();
}else{
turnLeft();
}
}
//repair one column
private void repaironecolumn() {
while (frontIsClear()){
if (beepersPresent()){
move();
}else{
putBeeper();
move();
}
}
if (beepersPresent()){
}
//while finish one column turn to east
private void findthedirection() {
if(notFacingNorth()){
turnLeft();
}else{
turnRight();
}
}
private void movetonextcolumn(){
for(int i=0;i<4;i++){
move();
}
}
}