Vybrané riešenia úloh

First Steps with Karel the Robot

Krok č. 1

#include <karel.h>

int main(){
   turnOn("stairs.kw");

   movek();
   
   turnLeft();
   movek();
   turnLeft();
   turnLeft();
   turnLeft();
   movek();
   pickBeeper();

   turnLeft();
   movek();
   turnLeft();
   turnLeft();
   turnLeft();
   movek();
   pickBeeper();

   turnLeft();
   movek();
   turnLeft();
   turnLeft();
   turnLeft();
   movek();
   pickBeeper();

   turnLeft();
   movek();
   turnLeft();
   turnLeft();
   turnLeft();
   movek();

   putBeeper();
   putBeeper();
   putBeeper();

   turnOff();

   return 0;
}

Krok č. 2

#include <karel.h>

void turnRight(){
   turnLeft();
   turnLeft();
   turnLeft();
}

void climbStair(){
   turnLeft();
   movek();
   turnRight();
   movek();
}

int main(){
   turnOn("stairs.kw");

   movek();
   
   climbStair();
   pickBeeper();

   climbStair();
   pickBeeper();

   climbStair();
   pickBeeper();

   climbStair();

   putBeeper();
   putBeeper();
   putBeeper();

   turnOff();

   return 0;
}

Training

#include <karel.h>

turnRight(){
   turnLeft();
   turnLeft();
   turnLeft();
}

jumpOver(){
   turnLeft();
   movek();
   turnRight();
   movek();
   turnRight();
   movek();
   turnLeft();
}

int main(){
   turnOn("training.kw");

   jumpOver();
   jumpOver();
   jumpOver();
   jumpOver();

   turnOff();

   return 0;
}

Road

#include <karel.h>

// otocenie Karla smerom doprava
void turnRight(){
   turnLeft();
   turnLeft();
   turnLeft();
}


// otocenie Karla o 180 stupnov
void turnAround(){
   turnLeft();
   turnLeft();
}


// vyplnenie diery na ceste a navrat na povodnu poziciu
void fillPothole(){
   turnRight();
   movek();
   putBeeper();
   turnAround();
   movek();
   turnRight();
}

int main(){
   turnOn("road.kw");

   movek();
   fillPothole();
   movek();

   movek();
   fillPothole();
   movek();

   turnOff();

   return 0;
}

Karel and his Sensors

Krok č. 1

#include <karel.h>

// otocenie Karla smerom doprava
void turnRight(){
   turnLeft();
   turnLeft();
   turnLeft();
}

// otocenie Karla o 180 stupnov
void turnAround(){
   turnLeft();
   turnLeft();
}

// vyplnenie diery na ceste a navrat na povodnu poziciu
void fillPothole(){
   turnRight();
   movek();
   if(noBeepersPresent())
      putBeeper();
   turnAround();
   movek();
   turnRight();
}

// hlavna funkcia programu
int main(){
   turnOn("road1.kw");

   if( rightIsClear() )
      fillPothole();

   while( frontIsClear() ){
      movek();
      if( rightIsClear() )
         fillPothole();
   }

   turnOff();

   return 0;
}

Krok č. 2

#include <karel.h>

// otocenie Karla smerom doprava
void turnRight(){
    turnLeft();
    turnLeft();
    turnLeft();
}

// preskocenie prekazky
void jumpOver(){
    turnLeft();
    movek();
    turnRight();
    movek();
    turnRight();
    movek();
    turnLeft();
}

// hlavna funkcia programu
int main(){
    turnOn("olympics.kw");

    while( noBeepersPresent() ){
       while( frontIsClear() )
          movek();
       jumpOver();
    }

    turnOff();

    return 0;
}

Chessboard

#include <karel.h>

// otocenie Karla smerom doprava
void turnRight(){
    turnLeft();
    turnLeft();
    turnLeft();
}

// prechod na dalsi riadok
void nextLine(){
   if( facingEast() )
      turnLeft();
   else
      turnRight();

   if( frontIsClear() )
      movek();
   else
      return;

   if( rightIsBlocked() )
      turnLeft();
   else
      turnRight();
}

// vyplnenie 1 riadku
void fillLine(){
   movek();
   putBeeper();

   while( frontIsClear() ){
      movek();
      movek();
      putBeeper();
   }

   nextLine();
}

int main(){
   turnOn("empty1.kw");

   while( frontIsClear() )
      fillLine();

   turnOff();

   return 0;
}

Collector

#include <karel.h>

// otocenie Karla smerom doprava
void turnRight(){
    turnLeft();
    turnLeft();
    turnLeft();
}

// otocenie Karla o 180 stupnov
void turnAround(){
   turnLeft();

   turnLeft();
}

// prechod na dalsi riadok
void nextLine(){
   if( facingEast() )
      turnLeft();
   else
      turnRight();

   if( frontIsClear() )
      movek();
   else
      return;

   if( rightIsBlocked() )
      turnLeft();
   else
      turnRight();
}

// pozbieranie 1 riadku
void pickLine(){
   while( beepersPresent() )
      pickBeeper();

   while( frontIsClear() ){
      movek();
      while( beepersPresent() )
         pickBeeper();
   }

   nextLine();
}

int main(){
   turnOn("empty1.kw");

   while( frontIsClear() )
      pickLine();

   turnAround();
   if( leftIsClear() ){
      turnLeft();
      while( frontIsClear() )
         movek();
      turnRight();
   }
   while( frontIsClear() )
      movek();

   while( beepersInBag() )
      putBeeper();

   turnRight();
   while( frontIsClear() )
      movek();

   turnOff();

   return 0;
}

Karel Solves Daily Problems

Karel Becomes SuperKarel

Karel Says Goodbye, NetBeans Says Hello