Hey I'm using jgrasp and the objective is to get the robot out of a maze and I have done just that but after the robot makes it out of the maze I need to have it print out how many spaces it has moved in each direction such as "I have moved + int + times SOUTH" and so on.

Was wondering what the instance var would look like if I wanted to make a print so it would respond at the end of the program for the directions. Having problems finding anything on the net that could possible help with this or maybe I'm just missing something here.

Any help would be appreciated. This below is the program minus the stuff I did already. Just need an idea of how to go about making the directional instance var.

import becker.robots.*;

class MazeBot extends RobotSE
{
    public MazeBot(City theCity, int str, int ave, Direction dir, int numThings)
    {  
        super(theCity, str, ave, dir, numThings);
    }

    public void printTotalNumberOfSpacesMoved()
    {
		
    }

	// isAtEndSpot is what's called a 'helper method' - it exists just to make
	// another command (in this case, NavigateMaze) easier to understand.
	// It does this by replacing some code that otherwise would be in NavigateMaze
	// with it's name, and doing that work here, instead.
	// "private" means that only the MazeBot is allowed to call it.
	private boolean isAtEndSpot()
	{
		// If we're at avenue 9, AND we're at street 10, then we must
		// be at the intersection (9,10), which is the "way out" of the maze
		// You can easily change this to tell the robot to stop at a different
		// intersection.
		return (this.getAvenue() == 9 && this.getStreet() == 10);	
	}

    public void NavigateMaze()
    {
        // While your robot hasn't reached the 
        // 'ending spot' in the maze, take
        // another step
        while( !this.isAtEndSpot() )
        {
            // What will you have the robot do at each step?
            
        }
    }
	
	
}
public class Maze extends Object
{
    private static void MakeMaze(City theCity)
    {
        for(int i = 1; i < 11; i++)
        {
            // north wall
            new Wall(theCity, 1, i, Direction.NORTH);
            
            // Second to north wall
            if (i <= 9)
                new Wall(theCity, 1, i, Direction.SOUTH);
            
            // Third to north wall
            if (i >= 4)
                new Wall(theCity, 4, i, Direction.SOUTH);
            
            // south wall
            if (i != 9) // (9, 10, SOUTH), is where the 'exit' is
                new Wall(theCity, 10, i, Direction.SOUTH);
            
            // west wall
            if( i != 1) // (1,1, WEST) is where the 'entrance' is
                new Wall(theCity, i, 1, Direction.WEST);
            
            // second to west-most wall
            if (i >= 3 && i < 6)
                new Wall(theCity, i, 6, Direction.WEST);
                
            // east wall
            new Wall(theCity, i, 10, Direction.EAST);
        }
        
        // cul-de-sac
        new Wall(theCity, 3, 10, Direction.WEST);
        new Wall(theCity, 3, 10, Direction.SOUTH);
        
        new Wall(theCity, 2, 8, Direction.WEST);
        new Wall(theCity, 2, 8, Direction.SOUTH);

        new Wall(theCity, 10, 8, Direction.NORTH);
        new Wall(theCity, 10, 9, Direction.EAST);
        new Wall(theCity, 10, 9, Direction.NORTH);
        MakeSpiral(theCity, 8, 9, 3);
        new Wall(theCity, 8, 10, Direction.SOUTH);
        
        MakeSpiral(theCity, 10, 5, 4);
    }

    public static void MakeSpiral(City theCity, int st, int ave, int size)
    {
        // We start out building the wall northward
        // the walls will be built on the east face of the current
        // intersection
        Direction facing = Direction.EAST;
        
        while( size > 0)
        {
            int spacesLeft = size;
            int aveChange = 0;
            int stChange = 0;
            switch(facing)
            {
                case EAST:
                    stChange = -1;
                    break;
                case NORTH:
                    aveChange = -1;
                    break;
                case WEST:
                    stChange = 1;
                    break;
                case SOUTH:
                    aveChange = 1;
                    break;
            }

            while(spacesLeft > 0)
            {
                new Wall(theCity, st, ave, facing);
                ave = ave + aveChange;
                st = st + stChange;
                spacesLeft--;
            }
            // back up one space
            ave = ave - aveChange;
            st = st - stChange;
            
            switch(facing)
            {
                case EAST:
                    facing = Direction.NORTH;
                    break;
                case NORTH:
                    facing = Direction.WEST;
                    size--;
                    break;
                case WEST:
                    facing = Direction.SOUTH;
                    break;
                case SOUTH:
                    facing = Direction.EAST;
                    size--;
                    break;
            }
        }
    }


    public static void main(String[] args)
    {  
        City calgary = new City(12, 12);
        MazeBot don = new MazeBot(calgary, 1, 1, Direction.EAST, 0);
        
        Maze.MakeMaze(calgary);
     
        don.NavigateMaze();
        
    }
}

Recommended Answers

All 3 Replies

What you may need is to add class's variables for each of the direction inside your existing class (MazeBot). Then increment the direction value each time your robot has made a decision to move to in your while( !this.isAtEndSpot() ) loop. Then you can print out the result at the end of the walk...

Another option would be to use a Map, such as HashMap, with the direction as the key and the count as the value.
Or an array could be used very easily if the Direction constants are int values 0-4.

K thx ill give those a try. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.