0% found this document useful (0 votes)
34 views6 pages

Assignment 1 Karel Macs

This document contains 4 tasks (assignments) involving Karel the robot. Each task contains multiple private methods to help Karel complete various steps like moving, turning, picking up and putting down beepers. The tasks involve Karel reaching and picking up paper, creating a checkerboard pattern, filling rows and changing positions, and finding the midpoint of the world.

Uploaded by

Elene Mikadze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

Assignment 1 Karel Macs

This document contains 4 tasks (assignments) involving Karel the robot. Each task contains multiple private methods to help Karel complete various steps like moving, turning, picking up and putting down beepers. The tasks involve Karel reaching and picking up paper, creating a checkerboard pattern, filling rows and changing positions, and finding the midpoint of the world.

Uploaded by

Elene Mikadze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

დავალება #1

ამოცანა 1
public void run() {

reachPaper();
pickPaper();
goBack();

}
private void reachPaper() {
turnRight();
move();
turnLeft();
for ( int i = 0; i < 3; i++ ) {
move();
}
}

private void pickPaper() {


pickBeeper();
turnAround();
}

private void goBack() {


for ( int i = 0; i < 3; i++ ) {
move();
}
turnRight();
move();
turnRight();
}

ამოცანა 2
public void run () {
fixColumn();
reset();
while (frontIsClear()) {
moveToNextColumn();
fixColumn();
reset();
}
}
private void fixColumn() {
turnLeft();
completeColumn();
checkLastBrick();
}
private void completeColumn() {
while (frontIsClear()) {
if (beepersPresent()) {
move();
} else {
putBeeper();
move();
}
}
}
private void checkLastBrick() {
if (beepersPresent()) {
turnAround();
} else {
putBeeper();
turnAround();
}
}
private void reset() {
while (frontIsClear()) {
move();
}
turnLeft();
}

private void moveToNextColumn() {


move();
move();
move();
move();
}

ამოცანა 3
public void run() {
createCheckerBoard();
}
/*karel is on 1x1 and facing east. After this method he will make a checkerboard and will be in the
right or left highest corner*/
private void createCheckerBoard() {

while (frontIsClear()) {
putBeeper();
fillRow();
changePosition();
fillRow();
reset();
}
fillOneLineVerticalWorld();
}

/*When this method, Karel is at the beginning (or at the ending) of an empty row to fill it from left
to right (or from right to left).
* After this method, it is facing a blocked front, facing east or west.*/
private void fillRow() {
while (frontIsClear()) {
move();
if (frontIsClear()) {
move();
putBeeper();
}
}
}

/*Karel is on the last cell of the row, facing east. After this method, she will be ready to start filling
a next row from right to left */
private void changePosition() {
if (leftIsClear()) {
checkIfOdd();
}
}

/* with this method, Karel finds out if a number of cells in a row is odd and decides whether to put a
Beeper or not on the last
cell of the next row. If it's odd, there will be a beeper on a last cell of a previously filled row, so
Karel will not put a beeper on a next
cell she will move to. She will put a beeper two cells after so that "fillRow" method can work since
"fillRow" begins with 2 moves */
private void checkIfOdd() {
if (noBeepersPresent()) {
turnLeft();
move();
putBeeper();
turnLeft();
} else {
turnLeft();
move();
turnLeft();
move();
putBeeper();
}
}

/*(with this method, Karel moves to next row to fill it from left to right)
at this point Karel has filled a row from right to left and is facing west wall.
She will end up on the last cell of next (yet empty) row, facing east */
private void reset() {
if (leftIsClear()) {
turnRight();
}
if (frontIsClear()) {
move();
turnRight();
}
}

/*karel standing 1x1, facing east. If front is blocked, the first "While" method won't work because
the world is just a vertical line. So Karel fills it with this method. She ends up in the highest
corner, facing left*/
private void fillOneLineVerticalWorld() {
if (leftIsClear()) {
turnLeft();
putBeeper();
fillRow();
}
}

ამოცანა 4
public void run() {
midPointFinding();
}
/*Karel is on 1x1 cell and facing east. After this method, she will be standing on one
beeper in the middle of the world.
If world is vertical line, then she is standing on the middle point already, so all she has
to do is to put beeper.
In other worlds she needs to find a middle point*/
private void midPointFinding() {
if (frontIsBlocked()) {
putBeeper();
} else {
findMidPoint();
}
}
private void findMidPoint() {
putBeepersOnEdges();
shortenDistanceBetweenBeepers();
CompleteAction();
}
/* before this method, she is on 1x1, facing east. After this method, she has put one
beeper on the first and the last cell of the line,
standing at the end of the line, facing west*/
private void putBeepersOnEdges() {
putBeeper();
while (frontIsClear()) {
move();
}
putBeeper();
turnAround();
}
/*When this method, Karel shortens the distance between those two beepers by
tranfering each of them one cell closer to the middle.
After this method, she has transfered two beepers in the middle and she is standing on
one additional temporal beeper at the end of the line,
facing wall */
private void shortenDistanceBetweenBeepers() {
while (frontIsClear()) {
shiftBeeper();
getToOtherBeeper();
turnAround();
}
}
/*Karel is standing on a beeper, After method is done, she has transfered the beeper
one cell closer to the middle */
private void shiftBeeper() {
if (beepersPresent()) {
pickBeeper();
move();
putBeeper();
}
}
/* this method helps Karel reach the second beeper to transfer it. "Else" method is used after
she has already put both beepers in center
and because there's no more beeper to transfer, she will face a wall. To exit from "while"
method, she needs to put additional beeper.*/
private void getToOtherBeeper() {
move();
while (noBeepersPresent()) {
if (frontIsClear()) {
move();
} else {
if (frontIsBlocked()) {
putBeeper();
turnAround();
}
}
}
}
/*precondition: she is standing on the last cell of the line, facing west. Postcondition: she is
standing on one beeper, on the middle cell.
* After all the transition, she has to remove the last additional beeper, go to center and
remove one of the two beepers on the middle cell */
private void CompleteAction() {
turnAround();
pickBeeper();
while (noBeepersPresent()) {
move();
}
pickBeeper();
turnAround();
}
}

You might also like