Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package CS210;
- import java.util.Scanner;
- public class FindWayHome {
- private int top = -1;
- private String[] data;
- private int maxSize;
- public FindWayHome(int size) {
- this.top = -1;
- this.maxSize = size;
- this.data = new String[maxSize];
- }
- public void push(String input) {
- this.data[++top] = input;
- }
- public String pop() {
- return this.data[top--];
- }
- public String peek() {
- return this.data[top];
- }
- public boolean isEmpty() {
- return (this.top == -1);
- }
- public boolean isFull() {
- return (this.top == maxSize - 1);
- }
- public static void main(String[] args) {
- getDirections();
- }
- public static void getDirections() {
- Scanner sc = new Scanner(System.in);
- FindWayHome stack = new FindWayHome(10000);
- String input;
- boolean stop = false;
- System.out.println("Enter inputs");
- while (!stack.isFull() & !stop) {
- String tmp = sc.nextLine();
- input = tmp.toLowerCase();
- switch (input) {
- case "go north":
- input = "go south";
- break;
- case "go south":
- input = "go north";
- break;
- case "go west":
- input = "go east";
- break;
- case "go east":
- input = "go west";
- break;
- case "arrived":
- stop = !stop;
- break;
- case "go back":
- stack.pop();
- break;
- default:
- System.out.println("Invalid input");
- }
- if (!stop)
- stack.push(input);
- // if (input.equalsIgnoreCase("Go North"))
- // input = "Go South";
- // else if (input.equalsIgnoreCase("Go South"))
- // input = "Go North";
- // else if (input.equalsIgnoreCase("Go West"))
- // input = "Go East";
- // else if (input.equalsIgnoreCase("Go East"))
- // input = "Go North";
- // else if (input.equalsIgnoreCase("arrived"))
- // stop = !stop;
- // else if (input.equalsIgnoreCase("go back"))
- // stack.pop();
- // else if (!stop)
- // stack.push(input);
- // if (!stop)
- // stack.push(input);
- }
- while (!stack.isEmpty())
- System.out.println(stack.pop());
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement