Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Carlos Garcia
- Challenge 2
- Maze Runner
- */
- package Challenges;
- import kareltherobot.World;
- public class Challenge2 extends EsperBot2 {
- public Challenge2(int street, int avenue, Direction direction, int beeperCount) {
- super(street, avenue, direction, beeperCount);
- }
- public static void main(String[] args) {
- World.setDelay(1);
- World.setVisible();
- World.setSize(10, 10);
- World.showSpeedControl(true);
- World.readWorld("KarelChallenges2.kwld");
- Challenge2 zgod = new Challenge2(1, 1, West, -1);
- zgod.mazeRun();
- }
- /**
- * Runs the maze
- * @return void
- * */
- public void mazeRun(){
- while (!nextToABeeper()) { // runs if it is not at the end
- if (wallOnRight()) {
- if (frontIsClear()) {
- move(); // moves straight if Karel is next to the wall and it is clear ahead
- }else {
- if (nextToABeeper()) {
- turnOff(); // maze solved
- } else {
- turnLeft(); // turns left until the front is clear
- if (frontIsClear()) {
- move();
- if (nextToABeeper()) {
- turnOff(); // maze solved
- }
- }else {
- turnLeft();
- }
- }
- }
- } else {
- while (!wallOnRight()){ // turns and moves until it finds the wall on the right.
- turnRight();
- move();
- if (nextToABeeper()) {
- turnOff(); // maze solved
- }
- }
- }
- if (nextToABeeper()) {
- turnOff(); // maze solved
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement