Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Random numberGen = new Random();
- int door = -400869;//(numberGen.nextInt(1000000)) - 500000; //Position door somewhere -500000 to 500000
- int you = 0; //Position
- int counter = 0; //Direction reversals
- int steps = 0;
- boolean doorFound = false;
- while (!doorFound) {
- if (counter % 2 == 1) {
- System.out.println("You are walking " + java.lang.Math.pow(2, counter) + " steps right.");
- for (int i = 0; i < java.lang.Math.pow(2, counter); i++) {
- you++;
- steps++;
- if (you == door) {
- doorFound = true;
- break;
- }
- }
- counter++;
- } else {
- System.out.println("You are walking " + java.lang.Math.pow(2, counter) + " steps left.");
- for (int i = 0; i < java.lang.Math.pow(2, counter); i++) {
- you--;
- steps++;
- if (you == door) {
- doorFound = true;
- break;
- }
- }
- counter++;
- }
- }
- System.out.println("YOU FOUND IT. You are at " + you + " and the door is at " + door);
- System.out.println("You changed direction " + counter + " times. - You took " + steps + " steps.");
- }
Add Comment
Please, Sign In to add comment