Guest User

Untitled

a guest
Jan 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public static void main(String[] args) {
  2. Random numberGen = new Random();
  3.  
  4. int door = -400869;//(numberGen.nextInt(1000000)) - 500000; //Position door somewhere -500000 to 500000
  5.  
  6. int you = 0; //Position
  7. int counter = 0; //Direction reversals
  8. int steps = 0;
  9.  
  10. boolean doorFound = false;
  11.  
  12. while (!doorFound) {
  13. if (counter % 2 == 1) {
  14. System.out.println("You are walking " + java.lang.Math.pow(2, counter) + " steps right.");
  15. for (int i = 0; i < java.lang.Math.pow(2, counter); i++) {
  16. if (!doorFound) { // avoid adding steps since we arne't breaking.
  17. you++;
  18. steps++;
  19. if (you == door) {
  20. doorFound = true;
  21. break;
  22. }
  23. }
  24. }
  25. counter++;
  26. } else {
  27. System.out.println("You are walking " + java.lang.Math.pow(2, counter) + " steps left.");
  28. for (int i = 0; i < java.lang.Math.pow(2, counter); i++) {
  29. if (!doorFound) { // avoid adding steps since we arne't breaking.
  30. you--;
  31. steps++;
  32. if (you == door) {
  33. doorFound = true;
  34. break;
  35. }
  36. }
  37. }
  38. counter++;
  39. }
  40. }
  41. System.out.println("YOU FOUND IT. You are at " + you + " and the door is at " + door);
  42. System.out.println("You changed direction " + counter + " times. - You took " + steps + " steps.");
  43. }
Add Comment
Please, Sign In to add comment