Posted on

MidPointFindingKarel – My Solution

I had a pretty challenging two weeks of solving this problem. I finally found a solution. I am so relieved.

[code lang=”javafx”]

/*
* 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 {
public void run(){

/*
* Pseudocode
* 1.laybeepers
* 2.pick them up one end to the other
*
* Decomposition
* * solves one problem
* * 1-15 lines
* * good names
* * /postconditions
*
*/
LayBeepers();
BackToStart();

/* FindTheMiddle
* here is the method that
* does the work
*/
FindTheMiddle();
}

/*
* LayBeeper
* put beepers from one end to the other
* pre: unlimited beepers, front is clear
* post: wall in front, beeper where iam standing
*/
private void LayBeepers(){
while(frontIsClear()){
putBeeper();
move();
}
putBeeper();
}

/* BackToStart
* moves Karel to the starting position
* pre: front is blocked
* post:front is clear
*/
private void BackToStart(){
turnAround();
MoveToWall();
turnAround();
}

/* MoveToWall
* keep moving until karel hits a wall
* pre: front clear
* post : front blocked
*/
private void MoveToWall(){
while(frontIsClear()){
move();
}
}

/* FindTheMiddle
* Finds the midpoint of a line
* Pre: beepers present in front
* post: no more beepers in front
*
*/
private void FindTheMiddle(){
if(beepersPresent()){
pickBeeper();
}
while(noBeepersPresent()){
GotoTheOtherEnd();
if(frontIsClear()){
checkabeeper();
}
pickBeeper();
}
}

/*
* Gootheotherend
* traverse the line of beepers
*/
private void GotoTheOtherEnd(){
FirstBeeper();
EndingBeeper();
}

/* FirstBeeper
* go to the first beeper
*/
private void FirstBeeper(){
if(noBeepersPresent()){
move();
}
}

/* endingbeeper
* check the lastbeeper
*
*/
private void EndingBeeper(){
while(frontIsClear()){
move();
}
turnAround();

while(noBeepersPresent()){
move();
}
}

/* checkabeeper
* checks if there is still a beeper for the last
* time
*/
private void checkabeeper(){
if(beepersPresent()){
move();
if(beepersPresent()){
MoveBack();
} else {
MoveBack();
putBeeper();
}
}
}

/* Moveback
*
*/
private void MoveBack(){
turnAround();
move();
turnAround();
}
}

[/code]

Posted on

My MidpointFindingKarel Algorithm

I am trying to solve the MidpointFindingKarel problem in the assignment 1 of Stanford’s CS106a. As of now, It is in my head but it still is not working perfectly.

I can lay down the beepers and have Karel move and pick them up one by one from one end to the other but I can’t have him stop at the middle.

• lay down the beepers
• move to the starting position
• pick the beepers one at a time from one end to the other
1. Lay down the beepers is easy.  It is just a “while” loop with the put beeper command in the body
2. move to the starting position is just a “turnAround” and “movetowall” method
3. This is the tough one which probably needs more decomposition. Still a work in progress.
Posted on

CheckerBoardKarel – CS106a,Assignment 1,Problem 3

Eureka! I solved it! Works in all worlds. I didn’t put any comments because the code is self explanatory.

[code lang=”java”]
/*
* 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 {

public void run(){
turnLeft();
for(int i=0;i<32;i++){
putBeeper();
skip();
}
}

private void skip(){
if(frontIsClear()){
move();
//doubleskip();
}

if(frontIsBlocked()){
if(facingNorth()){
if(beepersPresent()){
skipright();
} else {
movedown();
}

} else {
if(facingSouth()){
if(beepersPresent()){
skipleft();
} else {
moveup();
}
}
}

} else {
move();
}
}

private void moveup(){
turnLeft();
move();
turnLeft();
}

private void movedown(){
turnRight();
move();
turnRight();
}

private void doubleskip(){
move();
move();
}

private void skipright(){
moveright();
moveright();
}

private void skipleft(){
moveleft();
moveleft();
}

private void moveright(){
turnRight();
if(frontIsBlocked()){
moveleft();
} else {
move();
}
}

private void moveleft(){
turnLeft();
if(frontIsBlocked()){
moveright();
} else {
move();
}
}

}
[/code]

Posted on

Karel the Robot Assignment 1 Problem 2

It looks like I got the assignment solved. It looks a bit like spaghetti code but it works on the default world.

[code lang=”java”]/*
* 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(){
//iterate 13 times(checking 13 columns)
for(int i=0;i<13;i++){
check();
fixColumn();
keepmoving();
}
//pseudocode
//go up the column
//check if there is a beeper
//if there is one
//go back to the start and
//fill in the beeper
//go check the next column
//repeat
}

// move to the wall
private void movetowall(){
while(frontIsClear()){
move();
}
}

//keep karel moving through the next column
private void keepmoving(){
if(frontIsBlocked()){
if(facingNorth()){
backtostart();
}
}

if(facingNorth()){
turnRight();
move();
//turnRight();
}
if(facingSouth()){
turnLeft();
move();
turnLeft();
}
if(facingWest()){
turnAround();
move();
}

}

//skip a beeper
private void skipbeeper(){
if(beepersPresent()){
move();
}
}

//fix the broken column
private void fixColumn(){
backtostart();
while(frontIsClear()){
skipbeeper();
while(noBeepersPresent()){

if(frontIsClear()){
while(noBeepersPresent()){
putBeeper();
move();
}
} else {
if(noBeepersPresent()){
putBeeper();
}
turnAround();
movetowall();
}
}
}
}

//move to the next column
private void nextcolumn(){
turnLeft();
move();
turnLeft();

}

//back to starting position
private void backtostart(){
turnAround();
movetowall();
turnAround();
}

//check if this a column that needs fixing
private void check(){
if(facingEast()){
turnLeft();
}
while(noBeepersPresent()){
if(frontIsClear()){
move();
} else {
turnAround();
movetowall();
nextcolumn();
backtostart();
}
}
}

}
[/code]

Posted on

Getting stuck ?!?

stuck!
[code lang=”java”]

* File: StoneMasonKarel.java
* ————————–
* The StoneMasonKarel subclass as it appears here does nothing.
* When you finish writing it, it should solve the &quot;repair the quad&quot;
* 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()){
turnLeft();
movetowall();
keepmoving();
}
}

private void movetowall(){
while(frontIsClear()){
move();
}
}

private void keepmoving(){
if(notFacingEast()){
turnRight();
}
nextcolumn();
}

private void fixColumn(){
while(noBeepersPresent()){
putBeeper();
move();
}
backtostart();
}

private void nextcolumn(){
turnRight();
move();
turnLeft();
// turnLeft();
// move();
}

private void backtostart(){
turnAround();
movetowall();
turnAround();
}

private void check(){
while(noBeepersPresent()){
move();
}
//backtostart();
}
// private void fix(){
// if(beepersPresent()){
// //backtostart();
// fixColumn();

// turnAround();
// movetowall();
// }

// }

}

[/code]
how to iterate??

Posted on

The first assignment – CollectNewsPaperKarel

Posting my solution to the problem number 1 of assignment 1 of CS106a. Hoping for some feedback to this.

[code lang=”java”]
import stanford.karel.*;

public class CollectNewspaperKarel extends SuperKarel {

public void run(){
MoveToNewsPaper();
pickBeeper();
MoveBackToStart();
}

private void MoveToNewsPaper(){
while(frontIsClear()){
move();
}
turnRight();
move();
turnLeft();
move();

}

private void MoveBackToStart(){
turnAround();
move();
turnRight();
move();
turnLeft();
while(frontIsClear()){
move();
}
turnAround();

}
}

[/code]