简体   繁体   中英

User-inputted positions for Maze

I have the program compiling with no errors. It terminates right when I try to run. Can someone help me make this run with user-inputted starting and ending positions? I am a little lost. Here is the assignment:

Modify the maze problem in Chapter 4 so that it can start from a user defined starting postion (other than 0, 0) and search for a user defined ending point.

And the code is basically the maze problem that is mentioned, just with a Position class, and without the user-inputted part.

public class Maze {

    public static void main(String[] args) {
    }

    public interface StackADT<T> {

        public void push(T element);

        public T pop();

        public T peek();

        public boolean isEmpty();

        public int size();

        public String toString();

    }

    class LinkedStack<T> implements StackADT<T> {

        private int count;
        private LinearNode<T> top;

        public LinkedStack() {
            count = 0;
            top = null;
        }

        class LinearNode<T> {

            private LinearNode<T> next;
            private T element;

            public LinearNode() {
                next = null;
                element = null;
            }

            public LinearNode(T elem) {
                next = null;
                element = elem;
            }

            public LinearNode<T> getNext() {
                return next;
            }

            public void setNext(LinearNode<T> node) {
                next = node;
            }

            public T getElement() {
                return element;
            }

            public void setElement(T elem) {
                element = elem;
            }
        }

        class Position {

            private int x;
            private int y;

            Position() {
                x = 0;
                y = 0;
            }

            public int getx() {
                return x;
            }

            public int gety() {
                return y;
            }

            public void setx1(int a) {
                x = a;
            }

            public void sety(int a) {
                y = a;
            }

            public void setx(int x2) {

            }
        }

        private final int TRIED = 3;

        private final int PATH = 7;

        private int[][] grid = {{1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1},
        {1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1},
        {1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0},
        {0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
        {1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
        {1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};

        public StackADT<Position> push_new_pos(int x, int y,
                StackADT<Position> stack) {
            Position npos = new Position();
            npos.setx1(x);
            npos.sety(y);
            if (valid(npos.getx(), npos.gety())) {
                stack.push(npos);
            }
            return stack;
        }

        public boolean traverse() {
            boolean done = false;
            Position pos = new Position();
            Object dispose;
            StackADT<Position> stack = new LinkedStack<Position>();
            stack.push(pos);

            while (!(done)) {
                pos = stack.pop();
                grid[pos.getx()][pos.gety()] = TRIED; // this cell has been tried
                if (pos.getx() == grid.length - 1 && pos.gety() == grid[0].length - 1) {
                    done = true; // the maze is solved
                } else {
                    stack = push_new_pos(pos.getx(), pos.gety() - 1, stack);
                    stack = push_new_pos(pos.getx(), pos.gety() + 1, stack);
                    stack = push_new_pos(pos.getx() - 1, pos.gety(), stack);
                    stack = push_new_pos(pos.getx() + 1, pos.gety(), stack);
                }
            }
            return done;
        }

        private boolean valid(int row, int column) {
            boolean result = false;
            if (row >= 0 && row < grid.length
                    && column >= 0 && column < grid[row].length) {
                if (grid[row][column] == 1) {
                    result = true;
                }
            }

            return result;
        }

        public String toString() {
            String result = "\n";

            for (int row = 0; row < grid.length; row++) {
                for (int column = 0; column < grid[row].length; column++) {
                    result += grid[row][column] + "";
                }

                result += "\n";
            }
            return result;
        }

        @Override
        public void push(T element) {
            // TODO Auto-generated method stub

        }

        @Override
        public T pop() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public T peek() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public int size() {
            // TODO Auto-generated method stub
            return 0;
        }

    }
}

You do nothing in your main method:

public static void main(String[] args){}

Hence the programm does nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM