1.15.7 Debug Big Tower

Question: 1.7.4: The Two Towers + Comments

Answer: function start(){

// function start(){

runtoPancakes();

putPancakes();

goBack();

putPancakes();

move();

turnRight();

}

/*

  • Precondition:starting point,Karel is facing east

  • * Postcondition: Karel is inline to drop the balls

  • */

  • function runtoPancakes(){

  • move();

  • turnLeft();

  • }

function putPancakes(){

putBall();

move();

putBall();

move();

putBall();

}

function goBack(){

turnRight();

move();

turnRight();

move();

move();

turnLeft();

move();

turnLeft();

}

function turnRight(){

turnLeft();

turnLeft();

turnLeft();

}1.7.4: The Two Towers + Comments

==================================================

Question: 1.8.4: The Two Towers + Super…

Answer: function start(){

// function start(){

runtoPancakes();

putPancakes();

goBack();

putPancakes();

move();

turnRight();

}

/*

  • allows karel to get in line to drop the balls

  • */

  • function runtoPancakes(){

  • move();

  • turnLeft();

  • }

/*

  • Karel drops all 3 balls in the appropriate position

  • */

  • function putPancakes(){

  • putBall();

  • move();

  • putBall();

  • move();

  • putBall();

  • }

function goBack(){

turnRight();

move();

turnRight();

move();

move();

turnLeft();

move();

turnLeft();

}

==================================================

Question: 1.9.5: Take ‘em All

Answer: function start(){

// unction start(){

move();

for(var i = 0; i < 200; i++){

takeBall();

}

move();

}

==================================================

Question: 1.9.6: Dizzy Karel

Answer: function start(){

for(var i = 0; i < 32; i++){

turnLeft();

}

}

==================================================

Question: 1.9.7: For Loop Square

Answer: function start(){

putBall();

turnLeft();

move();

putBall();

turnRight();

move();

putBall();

turnRight();

move();

putBall();

turnRight();

move();

turnRight();

turnRight();

for(var i = 0; i < 4; i++){

takeBall();

putBall();

}

}

==================================================

Question: 1.9.8: Lots of Hurdles

Answer: function start(){

for(var i = 0; i < 5; i++){

move();

move();

jumpHurdle();

}

}

function jumpHurdle(){

turnLeft();

move();

turnRight();

move();

turnRight();

move();

turnLeft();

}

==================================================

Question: 1.10.5: Is There a Ball?

Answer: // Karel should put a ball on the first spot

// if there isn’t one already there and then move.

function start(){

if (ballsPresent()) {

move();

}else{

putBall();

move();

}

}

==================================================

Question: 1.11.5: Right Side Up

Answer: function start(){

if(facingSouth()){

turnLeft();

}else{

turnRight();

turnRight();

}

}

==================================================

Question: 1.12.4: Follow The Yellow Ball..

Answer: // Follow the yellow ball road!

// Karel moves until it’s not on a tennis ball.

function start() {

while (ballsPresent()){

move();

}

}

==================================================

Question: 1.12.5: Lay Row of Tennis Balls

Answer: /* Write a program to lay a row of tennis balls

  • across first street. Make sure to test your

  • program on multiple worlds. /

  • function start(){

  • while (frontIsClear()) {

  • putBall();

  • move();

  • }

  • putBall();

  • }

  • ==================================================

  • Question: 1.12.6: Big Tower

  • Answer: / This program draws a big tower from Karel’s starting spot /

  • function start(){

  • faceNorth();

  • buildTower();

  • putBall();

  • }

//This function make Karel face north is Karel is facing west.

function faceNorth(){

if(facingWest()){

turnRight();

}

if(facingEast()){

turnLeft();

}

if(facingSouth()){

turnLeft();

turnLeft();

}

}

//This function makes Karel put the tennis balls up to the top of the “world”.

function buildTower(){

while(frontIsClear()){

putBall();

move();

}

}

==================================================

Question: 1.13.4: Random Hurdles

Answer: function start() {

for (var i = 0; i < 13; i++) {

if (frontIsBlocked()) {

jumpHurdle();

} else {

move();

}

}

}

function jumpHurdle() {

//This makes Karel jump over the hurdles

turnLeft();

move();

turnRight();

move();

turnRight();

move();

turnLeft();

}

==================================================

Question: 1.15.4: Diagonal

Answer: /* This program has karel lay a diagonal row of tennis balls.

  • However, the indenting is all wrong. Can you properly

  • indent this program? /

  • function start(){

  • while(frontIsClear()){

  • putBall();

  • move();

  • turnLeft();

  • move();

  • for(var i = 0; i < 3; i++){

  • turnLeft();

  • }

  • }

  • putBall();

  • }

  • ==================================================

  • Question: 1.15.5: Staircase

  • Answer: /* This program creates a staircase from the first spot all the

  • * way across the world for any sized world.

  • *

  • * This program works, but its indentation is completely wrong.

  • *

  • * Run the program first, so you know what it does and don’t break it.

  • */

  • function start(){

  • putBall();

  • while(frontIsClear()){

  • turnLeft();

  • while (ballsPresent()) {

  • move();

  • }

turnRight();

move();

createStep();

}

}

function createStep() {

turnRight();

putBall();

while (frontIsClear()) {

move();

putBall();

}

turnLeft();

}

==================================================

Question: 1.16.1: Fetch

Answer: function start() {

turnLeft();

move4();

turnRight();

move2();

takeBall();

move();

turnRight();

move4();

turnLeft();

move3();

gP();

move3();

move3();

gP();

putBall();

}

//allow move karel 4 times

function move4(){

move();

move();

move();

move();

}

function move2(){

move();

move();

}

function gP(){

turnLeft();

turnLeft();

}

function move3(){

move();

move();

move();

}

==================================================

Question: 1.16.2: Racing Karel

Answer: /* This program will have Karel run around the racetrack

8 times. /

function start(){

for(var i = 0; i < 32; i++){

setBall();

runToWall();

turnLeft();

}

}

function setBall(){

putBall();

}

function runToWall(){

while (frontIsClear()){

move();

}

}

//hello

==================================================

Question: 1.16.3: Go Through the Fence

Answer: function start(){

turnLeft();

while (frontIsClear()) {

move();

}

while (frontIsBlocked()) {

check();

}

x();

turnRight();

y();

}

function check(){

turnRight();

move();

turnLeft();

}

function x(){

while (frontIsClear()) {

move();

}

}

function y(){

while (frontIsClear()){

move();

}

}

==================================================

Question: 1.16.4: Escape Karel

Answer: function start(){

move();

for (var i = 0; i < 4; i++) {

while (rightIsBlocked()) {

if (frontIsBlocked()) {

turnLeft();

}else {

}

move();

}

}

}

==================================================