Advertisement
yangjason69

ComplexCarpeter

Apr 23rd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1.  
  2. import kareltherobot.*;
  3. import java.awt.Color;
  4.  
  5. /**
  6.  * A robot lays carpet down in complex rooms.
  7.  *
  8.  * @author  Jason Yang
  9.  *  
  10.  * @version 4/22/2017
  11.  */
  12. public class ComplexCarpeter extends Carpeter
  13. {
  14.     /**
  15.      * Creates a ComplexCarpeter object.
  16.      *
  17.      * @param street     specifies the vertical position on the grid
  18.      *                   of this robot.
  19.      * @param avenue     specifies the horizontal position on the grid
  20.      *                   of this robot.
  21.      * @param direction  specifies the direction of this robot.
  22.      * @param beepers    specifies this robot's number of beepers.
  23.      */
  24.     public ComplexCarpeter (int street, int avenue,
  25.     Direction direction, int beepers)
  26.     {
  27.         super(street, avenue, direction, beepers);
  28.     }
  29.     /* ComplexCarpeter methods: */
  30.     /**
  31.      * Looks to see if the room is carpetable.
  32.      *
  33.      * @precondition  This robot is not in the room, is facing
  34.      *                the end of the hallway, and has enough beepers.
  35.      *
  36.      * @postcondition This robot is ready to carpet the room, facing North.
  37.      *
  38.      */
  39.     public void carpetRoom ()
  40.     {        
  41.         enterRoom();
  42.         if (doBothSidesExist())
  43.         {
  44.  
  45.             if (frontIsClear())
  46.             {
  47.                 move();
  48.                 if (doBothSidesExist())
  49.                 {
  50.  
  51.                     if (frontIsClear())
  52.                     {
  53.                         move();
  54.                         if (doBothSidesExist())
  55.                         {
  56.                             if (frontIsClear())
  57.                             {
  58.                                 exitRoomBad();
  59.                             }
  60.                             else
  61.                             {
  62.                                 exitRoomGood();
  63.                             }
  64.                         }
  65.                         else
  66.                         {
  67.                             exitRoomBad();
  68.                         }
  69.                     }
  70.                     else
  71.                     {
  72.                         exitRoomGood();
  73.                     }
  74.                 }
  75.                 else
  76.                 {
  77.                     exitRoomBad();
  78.                 }
  79.  
  80.             }
  81.             else
  82.             {
  83.                 exitRoomGood();
  84.             }
  85.  
  86.         }
  87.         else
  88.         {
  89.             exitRoomBad();
  90.         }
  91.     }
  92.     /**
  93.      * Exits the room and does not plant beepers on the way down.  
  94.      *
  95.      * @precondition   The robot is in a "room" that beepers cannot be planted in.
  96.      *
  97.      * @postcondition  This robot's position is just outside of the
  98.      *                 room's entrance on 1st Street.  The room is to the
  99.      *                 robot's left and this robot is facing the end of
  100.      *                 the row of rooms.
  101.      */    
  102.     public void exitRoomBad()
  103.     {
  104.         faceSouth();
  105.         if (frontIsClear())
  106.         {
  107.             move();
  108.             if (frontIsClear())
  109.             {
  110.                 move();
  111.                 if (frontIsClear())
  112.                 {
  113.                     move();
  114.                 }
  115.             }
  116.         }
  117.         turnLeft();
  118.     }
  119.  
  120.     /**
  121.      * Exits the room and puts beepers down on its way down.  
  122.      *
  123.      * @precondition   This robot's position is at the top of the room, facing north.
  124.      *
  125.      * @postcondition  This robot's position is just outside of the
  126.      *                 room's entrance on 1st Street.  The room is to the
  127.      *                 robot's left and this robot is facing the end of
  128.      *                 the row of rooms. The robot has also placed its beepers down
  129.      *                 in the room.        
  130.      */    
  131.     public void exitRoomGood()
  132.     {
  133.         faceSouth();
  134.         if (frontIsClear())
  135.         {
  136.             putBeeper();
  137.             move();
  138.             if (frontIsClear())
  139.             {
  140.                 putBeeper();
  141.                 move();
  142.                 if (frontIsClear())
  143.                 {
  144.                     putBeeper();
  145.                     move();
  146.                 }
  147.             }
  148.         }
  149.         turnLeft();
  150.     }
  151.  
  152.     /**
  153.      * Determines whether there is a wall to the left and right.
  154.      *
  155.      * @postcondition This robot is facing the same direction as it came in.
  156.      *
  157.      * @return  true if walls exist to this robot's right & left; otherwise,
  158.      *          false.
  159.      */
  160.     public boolean doBothSidesExist()
  161.     {
  162.         if (! leftIsClear() && ! rightIsClear())
  163.         {
  164.             return true;
  165.         }
  166.         return false;
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement