Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package KarelChallenges;
- import kareltherobot.*;
- /**
- * Sebastian Stephens
- * This program has Karel move down a path of beepers by checking each side for one.
- */
- public class Challenge3Main extends Robot{
- /**
- * Constructor for Challenge3Main Class
- * @param a
- * @param s
- * @param d
- * @param b
- * @ return - none
- */
- public Challenge3Main (int a, int s, Direction d, int b) {
- super(a, s, d, b);
- }
- public static void main(String[] args){
- World.setDelay(10);
- World.setVisible();
- World.readWorld("KarelChallenges3.kwld");
- //Creates new Challenge3Main object
- Challenge3Main gary = new Challenge3Main(1,1,North, -1);
- //Has gary run masterCheckForBeep method
- gary.masterCheckForBeep();
- }
- /**
- * Checks to see if there is a beeper on each side
- * @ param - none
- * @ return - none
- */
- public void masterCheckForBeep(){
- //Sets a boolean variable to true and has a while toop that runs while it is true
- boolean x = true;
- while(x = true){
- //If one of these conditions is true this code rune
- if(lookUp() || lookLeft() || lookDown() || lookRight() ){
- //Picks beeper robot is on so path doesnt mess up
- pickBeeper();
- //If there is still a beeper the loop ends and so does the program
- //This is because there are two beepers on the last space
- if(nextToABeeper()){
- pickBeeper();
- turnOff();
- x = false;
- }
- }
- }
- }
- /**
- * Checks to see if there is a beeper above the space where robot is located
- * @ param - none
- * @return
- */
- public boolean lookUp() {
- //Checks to see if there is wall in front of it
- if(frontIsClear()) {
- //Moves north and then checks for beeper
- move();
- //If it is next to beeper it returns true and robot stays there facing north
- if (nextToABeeper()) {
- return true;
- }
- //If it is not next to beeper it moves back to the previous space and resets position by facing north
- //Then returns false
- else {
- turnLeft();
- turnLeft();
- move();
- turnLeft();
- turnLeft();
- return false;
- }
- }
- //Nothing runs if there is a wall
- else{
- return false;
- }
- }
- /**
- * Checks to see if there is a beeper in the space below where the robot is located
- * @ param - none
- * @return
- */
- public boolean lookDown(){
- //Turns around and checks to see if front is clear to make sure there is no wall
- turnLeft();
- turnLeft();
- if(frontIsClear()) {
- //If there is no wall it moves and checks for a beeper
- move();
- //If there is beeper it resets position by facing north
- //Returns true
- if (nextToABeeper()) {
- turnLeft();
- turnLeft();
- return true;
- }
- //If there is no beeper then robot turns around and moves up to previous position
- //Returns false
- else {
- turnLeft();
- turnLeft();
- move();
- return false;
- }
- }
- //Does not run if there is wall
- else{
- return false;
- }
- }
- /**
- * Checks to see if there is a beeper to the left of the robot
- * @ param - none
- * @return
- */
- public boolean lookLeft(){
- //Faces left and checks if there is a wall
- turnLeft();
- if(frontIsClear()) {
- //Moves forward and checks if there is a beeper
- move();
- //If there it is a beeper robot turns right and method returns true
- if (nextToABeeper()) {
- turnRight();
- return true;
- }
- //If there is no beeper it turns around moves and resets position in previous spot
- //Then returns false
- else {
- turnRight();
- turnRight();
- move();
- turnLeft();
- return false;
- }
- }
- //Does not run if there is a wall
- else{
- return false;
- }
- }
- /**
- * Checks to see if right has beeper
- * @ param - none
- * @return
- */
- public boolean lookRight(){
- //Robot faces right
- turnRight();
- //Checks to see if the front is clear
- if(frontIsClear()) {
- //If front is clear robot moves forward and checks for beeper
- move();
- //If there is a beeper it turns left to reset position north
- //Returns true
- if (nextToABeeper()) {
- turnLeft();
- return true;
- }
- //If there is no beeper robot turns around a moves back and faces north
- //Returns false
- else {
- turnLeft();
- turnLeft();
- move();
- turnRight();
- return false;
- }
- }
- //If there is wall then nothing runs
- else{
- return false;
- }
- }
- /**
- * 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