Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kareltherobot.*;
- import java.awt.Color;
- /**
- * Karel turns off if he is surrounded and cannot move.
- *
- * @author Jason Yang
- *
- * @version 3/11/2017
- */
- public class turnOffIfSurrounded extends Robot
- {
- /**
- * Creates a turnOffIfSurrounded 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 turnOffIfSurrounded (int street, int avenue,
- Direction direction, int beepers)
- {
- super(street, avenue, direction, beepers);
- }
- /* turnOffIfSurrounded methods: */
- public void turnOffIfSurrounded(){
- if (frontIsClear()){
- return;
- }
- turnLeft();
- if (frontIsClear()){
- turnLeft();
- turnLeft();
- turnLeft();
- return;
- }
- turnLeft();
- if (frontIsClear()){
- turnLeft();
- turnLeft();
- return;
- }
- turnLeft();
- if (frontIsClear()){
- turnLeft();
- return;
- }
- turnOff();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement