Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. package CS210;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class FindWayHome {
  6.  
  7. private int top = -1;
  8. private String[] data;
  9. private int maxSize;
  10.  
  11. public FindWayHome(int size) {
  12. this.top = -1;
  13. this.maxSize = size;
  14. this.data = new String[maxSize];
  15. }
  16.  
  17. public void push(String input) {
  18. this.data[++top] = input;
  19. }
  20.  
  21. public String pop() {
  22. return this.data[top--];
  23. }
  24.  
  25. public String peek() {
  26. return this.data[top];
  27. }
  28.  
  29. public boolean isEmpty() {
  30. return (this.top == -1);
  31. }
  32.  
  33. public boolean isFull() {
  34. return (this.top == maxSize - 1);
  35. }
  36.  
  37. public static void main(String[] args) {
  38. getDirections();
  39. }
  40.  
  41. public static void getDirections() {
  42. Scanner sc = new Scanner(System.in);
  43. FindWayHome stack = new FindWayHome(10000);
  44. String input;
  45. boolean stop = false;
  46.  
  47. System.out.println("Enter inputs");
  48. while (!stack.isFull() & !stop) {
  49. String tmp = sc.nextLine();
  50. input = tmp.toLowerCase();
  51.  
  52. switch (input) {
  53. case "go north":
  54. input = "go south";
  55. break;
  56. case "go south":
  57. input = "go north";
  58. break;
  59. case "go west":
  60. input = "go east";
  61. break;
  62. case "go east":
  63. input = "go west";
  64. break;
  65. case "arrived":
  66. stop = !stop;
  67. break;
  68. case "go back":
  69. stack.pop();
  70. break;
  71. default:
  72. System.out.println("Invalid input");
  73. }
  74. if (!stop)
  75. stack.push(input);
  76.  
  77. // if (input.equalsIgnoreCase("Go North"))
  78. // input = "Go South";
  79. // else if (input.equalsIgnoreCase("Go South"))
  80. // input = "Go North";
  81. // else if (input.equalsIgnoreCase("Go West"))
  82. // input = "Go East";
  83. // else if (input.equalsIgnoreCase("Go East"))
  84. // input = "Go North";
  85. // else if (input.equalsIgnoreCase("arrived"))
  86. // stop = !stop;
  87. // else if (input.equalsIgnoreCase("go back"))
  88. // stack.pop();
  89. // else if (!stop)
  90. // stack.push(input);
  91. // if (!stop)
  92. // stack.push(input);
  93. }
  94.  
  95. while (!stack.isEmpty())
  96. System.out.println(stack.pop());
  97.  
  98. sc.close();
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement