Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package KarelChallenges;
- import kareltherobot.*;
- /**
- * Sebastian Stephens
- * This program is essentially Karel placing a beeper on the opposite end of where there is
- already a beeper.
- **/
- public class Challenge1Main extends Robot{
- /**
- * Constructor for Challenge1Main class
- * @param a
- * @param s
- * @param d
- * @param b
- * @ return - none
- */
- public Challenge1Main (int a, int s, Direction d, int b) {
- super(a, s, d, b);
- }
- public static void main (String[] args) {
- World.setDelay(20);
- World.setVisible();
- World.readWorld("KarelChallenges1.kwld");
- //Creates robot object named Gary
- Challenge1Main gary = new Challenge1Main(1,6,North, -1);
- //While front is clear it performs overridden move method
- while (gary.frontIsClear()){
- gary.move();
- }
- }
- /**
- * Overrides move method to move forward only if the left isnt clear, and to run checkLeftHole if it is not
- * @ param - none
- * @ return - void
- */
- public void move(){
- if (!leftIsClear()){
- super.move();
- }
- else {
- checkLeftHole();
- }
- }
- /**
- * Checks to see if left hole has beeper or not
- * @ param - none
- * @ return - void
- */
- public void checkLeftHole(){
- //Turns left and moves until the front is clear
- turnLeft();
- while (frontIsClear()){
- super.move();
- }
- //Once it gets to a stop, it checks if a beeper is there, if it is it fills the opposite side
- if (nextToABeeper()) {
- fillOppositeSide();
- //Once fill opposite side method ends it ends at the end of the left side hole
- //exit method has to run to get out of hole to reset position
- exit();
- } else {
- //If beeper is not there it exits the hole and resets position
- exit();
- }
- }
- /**
- * Places a beeper on the opposite side of a row with a beeper
- * @ param - none
- * @ return - void
- */
- public void fillOppositeSide(){
- //Turns around and moves until it hits a wall
- turnRight();
- turnRight();
- while (frontIsClear()){
- super.move();
- }
- //Once it hits a wall it checks to see if a beeper is there
- //Only if a beeper is not there it places a beeper
- if (!frontIsClear() && !nextToABeeper()){
- putBeeper();
- }
- //Turns around and moves all the way to the opposite side until it hits a wall
- turnRight();
- turnRight();
- while (frontIsClear()){
- super.move();
- }
- }
- /**
- * Allows one to exit the left hole
- * @ param - none
- * @ return - void
- */
- public void exit(){
- //Turns around
- turnLeft();
- turnLeft();
- //Checks to see if left or right is clear
- //Since there are some holes where one side is clear
- //If one of the sides is not clear it moves and it stops when both sides are clear
- while(!leftIsClear() || !rightIsClear()){
- super.move();
- }
- //Turns left and moves forward a space
- turnLeft();
- super.move();
- }
- /**
- * Checks to see if left is clear
- * @ param - none
- * @return boolean
- */
- public boolean leftIsClear(){
- //Turns left and sees if front is clear
- //If it isnt it turns right and returns false
- //IF it is it turns right and returns true
- turnLeft();
- if (!frontIsClear()){
- turnRight();
- return false;
- }
- else {
- turnRight();
- return true;
- }
- }
- /**
- * Checks to see if right is clear
- * @ param - none
- * @return boolean
- */
- public boolean rightIsClear(){
- //Same concept as leftIsClear but for right side
- turnRight();
- if (!frontIsClear()){
- turnLeft();
- return false;
- }
- else {
- turnLeft();
- return true;
- }
- }
- /**
- * Allows gary to turn right
- * @ param - none
- * @ return - void
- */
- public void turnRight(){
- int delay = World.delay();
- World.setDelay(0);
- turnLeft();
- turnLeft();
- World.setDelay(delay);
- turnLeft();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement