Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kareltherobot.*;
- import java.awt.Color;
- /**
- * A robot lays carpet down in complex rooms.
- *
- * @author Jason Yang
- *
- * @version 4/22/2017
- */
- public class ComplexCarpeter extends Carpeter
- {
- /**
- * Creates a ComplexCarpeter object.
- *
- * @param street specifies the vertical position on the grid
- * of this robot.
- * @param avenue specifies the horizontal position on the grid
- * of this robot.
- * @param direction specifies the direction of this robot.
- * @param beepers specifies this robot's number of beepers.
- */
- public ComplexCarpeter (int street, int avenue,
- Direction direction, int beepers)
- {
- super(street, avenue, direction, beepers);
- }
- /* ComplexCarpeter methods: */
- /**
- * Looks to see if the room is carpetable.
- *
- * @precondition This robot is not in the room, is facing
- * the end of the hallway, and has enough beepers.
- *
- * @postcondition This robot is ready to carpet the room, facing North.
- *
- */
- public void carpetRoom ()
- {
- enterRoom();
- if (doBothSidesExist())
- {
- if (frontIsClear())
- {
- move();
- if (doBothSidesExist())
- {
- if (frontIsClear())
- {
- move();
- if (doBothSidesExist())
- {
- if (frontIsClear())
- {
- exitRoomBad();
- }
- else
- {
- exitRoomGood();
- }
- }
- else
- {
- exitRoomBad();
- }
- }
- else
- {
- exitRoomGood();
- }
- }
- else
- {
- exitRoomBad();
- }
- }
- else
- {
- exitRoomGood();
- }
- }
- else
- {
- exitRoomBad();
- }
- }
- /**
- * Exits the room and does not plant beepers on the way down.
- *
- * @precondition The robot is in a "room" that beepers cannot be planted in.
- *
- * @postcondition This robot's position is just outside of the
- * room's entrance on 1st Street. The room is to the
- * robot's left and this robot is facing the end of
- * the row of rooms.
- */
- public void exitRoomBad()
- {
- faceSouth();
- if (frontIsClear())
- {
- move();
- if (frontIsClear())
- {
- move();
- if (frontIsClear())
- {
- move();
- }
- }
- }
- turnLeft();
- }
- /**
- * Exits the room and puts beepers down on its way down.
- *
- * @precondition This robot's position is at the top of the room, facing north.
- *
- * @postcondition This robot's position is just outside of the
- * room's entrance on 1st Street. The room is to the
- * robot's left and this robot is facing the end of
- * the row of rooms. The robot has also placed its beepers down
- * in the room.
- */
- public void exitRoomGood()
- {
- faceSouth();
- if (frontIsClear())
- {
- putBeeper();
- move();
- if (frontIsClear())
- {
- putBeeper();
- move();
- if (frontIsClear())
- {
- putBeeper();
- move();
- }
- }
- }
- turnLeft();
- }
- /**
- * Determines whether there is a wall to the left and right.
- *
- * @postcondition This robot is facing the same direction as it came in.
- *
- * @return true if walls exist to this robot's right & left; otherwise,
- * false.
- */
- public boolean doBothSidesExist()
- {
- if (! leftIsClear() && ! rightIsClear())
- {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement