Results 1 to 19 of 19

Thread: Maze

  1. #1
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Maze

    The goal of this program is to make a maze of nodes where each step gives different options, I have made and organized the maze but I don't know how to go about how to do make different options for the maze, here is what the maze is supposed to look like
    Maze-imgs.jpg

    and heres my code
    Maze.java
    Java Code:
    public class Maze {
        public Maze(){
    
            Node A = new Node('A');
            Node B = new Node('B');
            Node C = new Node('C');
            Node D = new Node('D');
            Node E = new Node('E');
            Node F = new Node('F');
            Node J = new Node('J');
            Node K = new Node('K');
            Node L = new Node('L');
            Node I = new Node('I');
            Node G = new Node('G');
            Node H = new Node('H');
    
            A.setEast(B);
            A.setSouth(E);
            E.setSouth(I);
            I.setEast(J);
            B.setSouth(F);
            F.setEast(G);
            G.setSouth(K);
            G.setNorth(C);
            G.setEast(H);
            H.setSouth(L);
            C.setEast(D);
    
        }
    
    
    
        public static void main(String[] args) {
    
        }
    
    }

    and Node.java
    Java Code:
    public class Node {
        private char Data;
        private Node north;
        private Node south;
        private Node east;
        private Node west;
    
    
    
        public Node(char data) {
            Data = data;
        }
    
        public char getData() {
            return Data;
        }
    
        public void setData(char data) {
            Data = data;
        }
    
        public Node getNorth() {
            return north;
        }
    
        public void setNorth(Node north) {
            this.north = north;
        }
    
        public Node getSouth() {
            return south;
        }
    
        public void setSouth(Node south) {
            this.south = south;
        }
    
        public Node getEast() {
            return east;
        }
    
        public void setEast(Node east) {
            this.east = east;
        }
    
        public Node getWest() {
            return west;
        }
    
        public void setWest(Node west) {
            this.west = west;
        }
    }
    The maze is supposed to start at A and end at L and it's supposed to give output about your choices each time you select a direction to go but I'm not sure how either of these things.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Maze

    For starters you are missing some options.
    E, for example, also has an exit north to A, which you haven't supplied, so there's no way to track back.

    You'll want to map out some simple logic, on a piece of paper, for what to do each turn. That is, each decision point for the user.
    This will form the content of your game loop, which ends when the user reaches L.

    So, what does your user need to do?
    And therefore, what does your code need to supply?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: Maze

    I fixed the options, but I'm stuck on the logic, I know I can use switch statements but how can I implement them into the program to make it work how it's supposed to, can you give me an example

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Maze

    Forget about code.
    Write down what it is you think the flow is for each turn the user takes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: Maze

    I'm kind of confused by what you're asking.
    As in starting at A the user has 2 choices, go east to B or south to E?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Maze

    The traveler will need to know where it is located
    What the choices are from that location will be determined by the maze's nodes.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: Maze

    Now that I have set the nodes in their places how can I move around them

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Maze

    how can I move around them
    Have a current location variable that references a Node. To move, change the value of that variable.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Maze

    OK.

    For each decision point the user has to make the following needs to happen:

    The app prompts the user for a choice of direction, giving the list of options base on their current node.
    The user enters their choice (NSEW).
    The app changes the current node to be the node in that direction.
    Back round the loop.

    So you need to write code for step 1.
    I would recommend doing that in a method of its own so it is easier to test.

    ETA: That is what I mean by not writing code.
    Until you know what the app should do there is no point worrying about switch statements or the like.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: Maze

    Thank you tolls, sorry I didn't understand what you meant earlier but the problem is I know what I need the program to do and how I'm going to go about it, I'm just inexperienced with the coding aspect itself. I'm not sure how to even begin coding that and my lack of knowledge in writing code doesn't let me understand what you guys are trying to tell me.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Maze

    So take step 1.
    Write a method in the Maze class that prompts the user for their next choice based on the current node.

    Now, that will require you to know the current node, which means you will need to add an attribute to the Maze.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: Maze

    I have written the method but still don't understand how to implement currentNode and the code should even look

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Maze

    OK, so show us the Maze code you have.

    I'm off now, so I won't be around to pick it up til tomorrow morning...one of the Americans'll be around, though, I expect.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Maze

    I know what I need the program to do
    Ok, can you write down the steps that the program needs to take? See post#9

    After the steps for the program are written down then work on the code for each step.

    how to implement currentNode
    Java Code:
      Node currentNode;     // define pointer to the current location
    maze is supposed to start at A
    Java Code:
      currentNode = A;  // set the starting location
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: Maze

    The steps are
    Java Code:
    At  you the beginning  of the maze can either choose South to E or East to B
    If you choose south to E then your only option is to go south one more step to I and the east one more step to J and then you're trapped and you get a message saying you're trapped.
    If you choose to go east from the A to B then your only option is to go south to F and then east to G.
    At G you have 3 options, 
    Option 1 is North to C which then leads you east to D which will print out  a message saying you're trapped.
    Option 2 is south to K which then is a dead end with a message saying you're trapped
    and Option 3 leads you east to H then south to L which prints a message saying you made it out the maze
    Maze.Java
    Java Code:
    public class Maze {
        public Maze(){
    
            Node A = new Node('A');
            Node B = new Node('B');
            Node C = new Node('C');
            Node D = new Node('D');
            Node E = new Node('E');
            Node F = new Node('F');
            Node J = new Node('J');
            Node K = new Node('K');
            Node L = new Node('L');
            Node I = new Node('I');
            Node G = new Node('G');
            Node H = new Node('H');
    
            A.setEast(B);
            A.setSouth(E);
            B.setWest(A);
            F.setNorth(B);
            G.setWest(F);
            C.setSouth(G);
            K.setNorth(G);
            D.setWest(C);
            E.setSouth(I);
            E.setNorth(A);
            I.setEast(J);
            I.setNorth(E);
            J.setWest(I);
            B.setSouth(F);
            F.setEast(G);
            G.setSouth(K);
            G.setNorth(C);
            G.setEast(H);
            H.setSouth(L);
            C.setEast(D);
            
    
    
        }
    
        
    
    
        public static void main(String[] args) {
    
    
    
    
    
        }
    
    }


    Node.Java
    Java Code:
    public class Node {
        private char Data;
        private Node north;
        private Node south;
        private Node east;
        private Node west;
        private Node currentNode;
    
    
        public Node(char data) {
            Data = data;
        }
        @Override
        public String toString(){
            return "Node location= " +currentNode;
        }
    
        public char getData() {
            return Data;
        }
    
        public void setData(char data) {
            Data = data;
        }
    
        public Node getNorth() {
            return north;
        }
    
        public void setNorth(Node north) {
            this.north = north;
        }
    
        public Node getSouth() {
            return south;
        }
    
        public void setSouth(Node south) {
            this.south = south;
        }
    
        public Node getEast() {
            return east;
        }
    
        public void setEast(Node east) {
            this.east = east;
        }
    
        public Node getWest() {
            return west;
        }
    
        public void setWest(Node west) {
            this.west = west;
        }
    }

    This is all I have so far, I tried putting the currentNode=A in but kept getting errors, I'm completely confused on what i need to do in this assignment

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Maze

    The list of steps left off setting the starting location to A.
    Another missing step is getting the desired direction of movement from the user.
    The steps have too much knowledge of the maze. They should use only consider what is available from the current location. The steps need to work with any maze. For example:
    the user asks to go south
    check if the current location has a path to a node to the south
    if it does, move the current location to the south
    if it does not, tell user and wait for another direction from user.
    Last edited by Norm; 10-01-2016 at 12:40 PM.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    dmp5658 is offline Member
    Join Date
    Sep 2016
    Posts
    33
    Rep Power
    0

    Default Re: Maze

    I tried setting currentNode to A but I couldn't figure out where it needed to go, how can I even begin to code this

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Maze

    how can I even begin to code this
    Don't start coding until you have a design for how the program will work. See post#9 for a starting list of steps.

    setting currentNode to A
    Yes that was given in the instructions for how the program should work:
    The maze is supposed to start at A and end at L and
    it's supposed to give output about your choices each time you select a direction to go
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Maze

    A Node should not care whether it is current or not.
    The Maze, however, should.
    So it's the Maze class that should have the current node.

    It can then call something on Node to tell it what options to display to the player.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Maze help
    By bap2 in forum Java Software
    Replies: 0
    Last Post: 01-14-2011, 12:31 AM
  2. Solving a Maze
    By bdario1 in forum New To Java
    Replies: 4
    Last Post: 04-14-2010, 12:02 AM
  3. Maze
    By soriano in forum New To Java
    Replies: 1
    Last Post: 12-16-2008, 05:40 PM
  4. 3D Maze
    By EternalSolitude in forum New To Java
    Replies: 5
    Last Post: 11-14-2008, 12:51 AM
  5. Maze Help
    By Soda in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2007, 03:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •