-
-
Save NatashaTheRobot/1321614 to your computer and use it in GitHub Desktop.
/* | |
* 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()) {
findTheWay();
repaironecolumn();
findthedirection();
movetonextcolumn();
}
findTheWay();
repaironecolumn();
}
//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()){
}else{
putBeeper();
}
}
//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();
}
}
}
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();
}
}
}
I created my own version of this before looking at too much detail here. I think Michael's solution might be more elegant, because everything is separated more logically...