Advertisement
KevinNT03

Karel - Constructing Castles

Dec 12th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. package Activity4;
  2. import kareltherobot.*;
  3.  
  4. /**
  5.  * This program uses robots to construct a house using beepers
  6.  * @param - none
  7.  * @return - none
  8.  */
  9. public class Activity4Main implements Directions{
  10.  
  11.     public static void main(String[] args) {
  12.         World.readWorld("Lesson3World4.kwld");
  13.         World.setVisible();
  14.         World.setDelay(20);
  15.  
  16.         WallBuilder john = new WallBuilder(1, 2, North, -1);
  17.         WallBuilder cal = new WallBuilder(1,10, North, -1);
  18.         RoofBuilder jack = new RoofBuilder(5,1, North, -1);
  19.         WindowBuilder bob = new WindowBuilder(5, 4, North, -1);
  20.         WindowBuilder tom = new WindowBuilder(5, 7, North, -1);
  21.         DoorBuilder michael = new DoorBuilder(1,5, North, -1);
  22.  
  23.         john.build(); //all robots build with respective overriding
  24.         cal.build();
  25.         jack.build();
  26.         bob.build();
  27.         tom.build();
  28.         michael.build();
  29.     }
  30. }
  31. -----------------------------------
  32. Different file
  33. -----------------------------------
  34. package Activity4;
  35. import kareltherobot.*;
  36.  
  37. /**
  38.  * Constructicon - parent class
  39.  * @param - none
  40.  * @return - none
  41.  */
  42. public class Constructicon extends UrRobot {
  43.  
  44.     public Constructicon(int s, int a, Direction d, int b){
  45.         super(s, a, d, b);
  46.     }
  47.  
  48.     public void build(){
  49.         turnOff();  //if the overriding is successful, the robot shouldn't turn off
  50.     }
  51.  
  52.     /**
  53.      * Allows the robot to instantaneously turn right
  54.      * @param - none
  55.      * @return - Void
  56.      */
  57.     public void turnRight(){
  58.         int delay = World.delay();
  59.         World.setDelay(0);
  60.         turnLeft();
  61.         turnLeft();
  62.         World.setDelay(delay);
  63.         turnLeft();
  64.     }
  65. }
  66. -----------------------------------
  67. Different file
  68. -----------------------------------
  69. package Activity4;
  70.  
  71. /**
  72.  * Builds the door of the house
  73.  */
  74. public class DoorBuilder extends Constructicon{
  75.     public DoorBuilder(int s, int a, Direction d, int b){
  76.         super(s, a, d, b);
  77.     }
  78.  
  79.     /**
  80.      * @Override build
  81.      */
  82.     public void build(){
  83.         for (int out = 0; out < 3; out++){
  84.             for (int in = 0; in < 2; in++){
  85.                 putBeeper();
  86.                 move();
  87.             }
  88.             turnRight();
  89.         }
  90.     }
  91. }
  92. -----------------------------------
  93. Different file
  94. -----------------------------------
  95. package Activity4;
  96.  
  97. /**
  98.  * Subclass of Constructicon
  99.  * Builds the roof of the house
  100.  */
  101. public class RoofBuilder extends Constructicon{
  102.     public RoofBuilder(int s, int a, Direction d, int b){
  103.         super(s, a, d, b);
  104.     }
  105.  
  106.     /**
  107.      * @Override build
  108.      */
  109.     public void build(){
  110.         for(int x = 0; x < 5; x++){ //builds the left part of roof
  111.             putBeeper();
  112.             move();
  113.             turnRight();
  114.             move();
  115.             turnLeft();
  116.         }
  117.  
  118.         putBeeper();
  119.         turnRight();
  120.  
  121.         for(int y = 0; y < 5; y++){ //builds the right part of roof
  122.             move();
  123.             turnRight();
  124.             move();
  125.             putBeeper();
  126.             turnLeft();
  127.         }
  128.     }
  129. }
  130. -----------------------------------
  131. Different file
  132. -----------------------------------
  133. package Activity4;
  134.  
  135. /**
  136.  * builds the walls of the house
  137.  */
  138. public class WallBuilder extends Constructicon{
  139.     public WallBuilder(int s, int a, Direction d, int b){
  140.         super(s, a, d, b);
  141.     }
  142.  
  143.     /**
  144.      * @Override build
  145.      */
  146.     public void build(){
  147.         for(int x = 0; x < 5; x++){
  148.             putBeeper();
  149.             move();
  150.         }
  151.     }
  152. }
  153.  
  154. -----------------------------------
  155. Different file
  156. -----------------------------------
  157. package Activity4;
  158.  
  159. /**
  160.  * builds the windows of the house
  161.  */
  162. public class WindowBuilder extends Constructicon{
  163.     public WindowBuilder(int s, int a, Direction d, int b){
  164.         super(s, a, d, b);
  165.     }
  166.  
  167.     /**
  168.      * @Override build
  169.      */
  170.     public void build(){
  171.         for(int i = 0; i < 4; i++){
  172.             putBeeper();
  173.             move();
  174.             turnRight();
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement