JumpingBug

A GridWorld Problem

Page 1 of 1

9 Replies - 7899 Views - Last Post: 24 March 2010 - 04:46 PM Rate Topic: -----

#1 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

JumpingBug

Posted 23 March 2010 - 10:36 PM

So here is my assignment:

Create a JumpingBug that extends the Bug class and jumps over Rocks.  Details:
-it is only strong enough to jump one Rock at a time
-if it encounters 2 consecutive Rocks in its path, it should turn like a Bug
-in all other respects, it should have the same behavior as a Bug
Things to watch for: 
-make sure it does not
-jump off the grid or 'disappear' or
-'eat' another Bug (or Actor other than a Flower)

I've been trying to figure this out, but I am stuck. I hit a wall.

Here's my code:
import info.gridworld.actor.*;
import info.gridworld.grid.*;

/**
 * A <code>BoxBug</code> traces out a square "box" of a given size. <br />
 * The implementation of this class is testable on the AP CS A and AB exams.
 */
public class JumpingBug extends Bug
{
    private int steps;
    private int sideLength;

    /**
     * Constructs a box bug that traces a square of a given side length
     * @param length the side length
     */
    public JumpingBug(int length)
    {
        steps = 0;
        sideLength = length;
    }

    /**
     * Moves to the next location of the square.
     */
    public void act()
    {
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
            turn();
            turn();
            steps = 0;
        }
    }
    
    // modify this method....
    public boolean canMove()
    {
        Grid<Actor> gr = getGrid();
        if (gr == null)
            return false;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        
        if (!gr.isValid(next))
            return false;
            
        Actor neighbor = gr.get(next);
       
        return (neighbor == null) || (neighbor instanceof Rock) || (neighbor instanceof Flower);
        // ok to move into empty location or onto flower
        // not ok to move onto any other actor
    }
}


import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Location;

import java.awt.Color;

/**
 * This class runs a world that contains box bugs. <br />
 * This class is not tested on the AP CS A and AB exams.
 */
public class JumpingBugRunner
{
    public static void main(String[] args)
    {
        ActorWorld world = new ActorWorld();
        
        JumpingBug bob = new JumpingBug(3);
        world.add(new Location(5, 5), bob);
        
        world.add(new Location(4, 5), new Rock());
        world.add(new Location(3, 5), new Rock());
        
        world.add(new Location(5, 4), new Rock());
        world.add(new Location(5, 6), new Rock());
        world.add(new Location(6, 5), new Rock());
        
        
        world.show();
    }
}



And the relevant code that was given to me:

BUG CLASS
/* 
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * @author Cay Horstmann
 */

package info.gridworld.actor;

import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

import java.awt.Color;

/**
 * A <code>Bug</code> is an actor that can move and turn. It drops flowers as
 * it moves. <br />
 * The implementation of this class is testable on the AP CS A and AB exams.
 */
public class Bug extends Actor
{
    /**
     * Constructs a red bug.
     */
    public Bug()
    {
        setColor(Color.RED);
    }

    /**
     * Constructs a bug of a given color.
     * @param bugColor the color for this bug
     */
    public Bug(Color bugColor)
    {
        setColor(bugColor);
    }

    /**
     * Moves if it can move, turns otherwise.
     */
    public void act()
    {
        if (canMove())
            move();
        else
            turn();
    }

    /**
     * Turns the bug 45 degrees to the right without changing its location.
     */
    public void turn()
    {
        setDirection(getDirection() + Location.HALF_RIGHT);
    }

    /**
     * Moves the bug forward, putting a flower into the location it previously
     * occupied.
     */
    public void move()
    {
        Grid<Actor> gr = getGrid();
        if (gr == null)
            return;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (gr.isValid(next))
            moveTo(next);
        else
            removeSelfFromGrid();
        Flower flower = new Flower(getColor());
        flower.putSelfInGrid(gr, loc);
    }

    /**
     * Tests whether this bug can move forward into a location that is empty or
     * contains a flower.
     * @return true if this bug can move.
     */
    public boolean canMove()
    {
        Grid<Actor> gr = getGrid();
        if (gr == null)
            return false;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (!gr.isValid(next))
            return false;
        Actor neighbor = gr.get(next);
        return (neighbor == null) || (neighbor instanceof Flower);
        // ok to move into empty location or onto flower
        // not ok to move onto any other actor
    }
}


ACTOR CLASS
package info.gridworld.actor;

import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

import java.awt.Color;

/**
 * An <code>Actor</code> is an entity with a color and direction that can act.
 * <br />
 * The API of this class is testable on the AP CS A and AB exams.
 */
public class Actor
{
    private Grid<Actor> grid;
    private Location location;
    private int direction;
    private Color color;

    /**
     * Constructs a blue actor that is facing north.
     */
    public Actor()
    {
        color = Color.BLUE;
        direction = Location.NORTH;
        grid = null;
        location = null;
    }

    /**
     * Gets the color of this actor.
     * @return the color of this actor
     */
    public Color getColor()
    {
        return color;
    }

    /**
     * Sets the color of this actor.
     * @param newColor the new color
     */
    public void setColor(Color newColor)
    {
        color = newColor;
    }

    /**
     * Gets the current direction of this actor.
     * @return the direction of this actor, an angle between 0 and 359 degrees
     */
    public int getDirection()
    {
        return direction;
    }

    /**
     * Sets the current direction of this actor.
     * @param newDirection the new direction. The direction of this actor is set
     * to the angle between 0 and 359 degrees that is equivalent to
     * <code>newDirection</code>.
     */
    public void setDirection(int newDirection)
    {
        direction = newDirection % Location.FULL_CIRCLE;
        if (direction < 0)
            direction += Location.FULL_CIRCLE;
    }

    /**
     * Gets the grid in which this actor is located.
     * @return the grid of this actor, or <code>null</code> if this actor is
     * not contained in a grid
     */
    public Grid<Actor> getGrid()
    {
        return grid;
    }

    /**
     * Gets the location of this actor.
     * @return the location of this actor, or <code>null</code> if this actor is
     * not contained in a grid
     */
    public Location getLocation()
    {
        return location;
    }

    /**
     * Puts this actor into a grid. If there is another actor at the given
     * location, it is removed. <br />
     * Precondition: (1) This actor is not contained in a grid (2)
     * <code>loc</code> is valid in <code>gr</code>
     * @param gr the grid into which this actor should be placed
     * @param loc the location into which the actor should be placed
     */
    public void putSelfInGrid(Grid<Actor> gr, Location loc)
    {
        if (grid != null)
            throw new IllegalStateException(
                    "This actor is already contained in a grid.");

        Actor actor = gr.get(loc);
        if (actor != null)
            actor.removeSelfFromGrid();
        gr.put(loc, this);
        grid = gr;
        location = loc;
    }

    /**
     * Removes this actor from its grid. <br />
     * Precondition: This actor is contained in a grid
     */
    public void removeSelfFromGrid()
    {
        if (grid == null)
            throw new IllegalStateException(
                    "This actor is not contained in a grid.");
        if (grid.get(location) != this)
            throw new IllegalStateException(
                    "The grid contains a different actor at location "
                            + location + ".");

        grid.remove(location);
        grid = null;
        location = null;
    }

    /**
     * Moves this actor to a new location. If there is another actor at the
     * given location, it is removed. <br />
     * Precondition: (1) This actor is contained in a grid (2)
     * <code>newLocation</code> is valid in the grid of this actor
     * @param newLocation the new location
     */
    public void moveTo(Location newLocation)
    {
        if (grid == null)
            throw new IllegalStateException("This actor is not in a grid.");
        if (grid.get(location) != this)
            throw new IllegalStateException(
                    "The grid contains a different actor at location "
                            + location + ".");
        if (!grid.isValid(newLocation))
            throw new IllegalArgumentException("Location " + newLocation
                    + " is not valid.");

        if (newLocation.equals(location))
            return;
        grid.remove(location);
        Actor other = grid.get(newLocation);
        if (other != null)
            other.removeSelfFromGrid();
        location = newLocation;
        grid.put(location, this);
    }

    /**
     * Reverses the direction of this actor. Override this method in subclasses
     * of <code>Actor</code> to define types of actors with different behavior
     * 
     */
    public void act()
    {
        setDirection(getDirection() + Location.HALF_CIRCLE);
    }

    /**
     * Creates a string that describes this actor.
     * @return a string with the location, direction, and color of this actor
     */
    public String toString()
    {
        return getClass().getName() + "[location=" + location + ",direction="
                + direction + ",color=" + color + "]";
    }
}



My only problem is in the JumpingBug class on how to actually make the Bug simply jump over the rock without deleting it and only jumping on the conditions listed.

Is This A Good Question/Topic? 0
  • +

Replies To: JumpingBug

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: JumpingBug

Posted 24 March 2010 - 03:31 AM

Ok, so here are the steps.

In the current direction, so if there is a blank spot exactly two grid locations in front of you, if there is, the continue moving forward, even though there's a rock. If there is not an open space to move, then you should turn.
Was This Post Helpful? 1
  • +
  • -

#3 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: JumpingBug

Posted 24 March 2010 - 07:33 AM

View PostDogstopper, on 24 March 2010 - 01:31 AM, said:

Ok, so here are the steps.

In the current direction, so if there is a blank spot exactly two grid locations in front of you, if there is, the continue moving forward, even though there's a rock. If there is not an open space to move, then you should turn.

Not exactly...here I have a picture attached.

Moving NORTH is invalid because there are 2 rocks. Moving all other directions are valid because there is only 1 rock. My problem is how to differentiate between the 2 cases and have the bug "jump" without deleting the rock.
My link
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,684
  • Joined: 27-December 08

Re: JumpingBug

Posted 24 March 2010 - 07:58 AM

When I did this assignment, I added a canJump() method instead of overriding the canMove() method. In canJump(), check two spaces in front of you for valid and unoccupied. It should look fairly similar to canMove().
Was This Post Helpful? 1
  • +
  • -

#5 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: JumpingBug

Posted 24 March 2010 - 10:48 AM

View Postmacosxnerd101, on 24 March 2010 - 05:58 AM, said:

When I did this assignment, I added a canJump() method instead of overriding the canMove() method. In canJump(), check two spaces in front of you for valid and unoccupied. It should look fairly similar to canMove().

The said, how do you make it so it doesn't lay down a flower when it jumps?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,684
  • Joined: 27-December 08

Re: JumpingBug

Posted 24 March 2010 - 11:31 AM

Take a look at the Bug class, specifically the move() method. Think about how you could override move() to move, jump or turn accordingly, and not lay a Flower down.
    public void move()
    {
        Grid<Actor> gr = getGrid();
        if (gr == null)
            return;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (gr.isValid(next))
            moveTo(next);
        else
            removeSelfFromGrid();
        Flower flower = new Flower(getColor());
        flower.putSelfInGrid(gr, loc);
    }


Was This Post Helpful? 1
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: JumpingBug

Posted 24 March 2010 - 01:33 PM

View Postzim1985, on 24 March 2010 - 09:33 AM, said:

View PostDogstopper, on 24 March 2010 - 01:31 AM, said:

Ok, so here are the steps.

In the current direction, so if there is a blank spot exactly two grid locations in front of you, if there is, the continue moving forward, even though there's a rock. If there is not an open space to move, then you should turn.

Not exactly...here I have a picture attached.

Moving NORTH is invalid because there are 2 rocks. Moving all other directions are valid because there is only 1 rock. My problem is how to differentiate between the 2 cases and have the bug "jump" without deleting the rock.
My link

That's what I said...

View Postmacosxnerd101, on 24 March 2010 - 09:58 AM, said:

When I did this assignment, I added a canJump() method instead of overriding the canMove() method. In canJump(), check two spaces in front of you for valid and unoccupied. It should look fairly similar to canMove().


That's what I said...
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,684
  • Joined: 27-December 08

Re: JumpingBug

Posted 24 March 2010 - 01:37 PM

Quote

Quote

When I did this assignment, I added a canJump() method instead of overriding the canMove() method. In canJump(), check two spaces in front of you for valid and unoccupied. It should look fairly similar to canMove().


That's what I said...


Great minds think alike. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: JumpingBug

Posted 24 March 2010 - 01:47 PM

True dat! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#10 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: JumpingBug

Posted 24 March 2010 - 04:46 PM

View Postmacosxnerd101, on 24 March 2010 - 09:31 AM, said:

Take a look at the Bug class, specifically the move() method. Think about how you could override move() to move, jump or turn accordingly, and not lay a Flower down.
    public void move()
    {
        Grid<Actor> gr = getGrid();
        if (gr == null)
            return;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (gr.isValid(next))
            moveTo(next);
        else
            removeSelfFromGrid();
        Flower flower = new Flower(getColor());
        flower.putSelfInGrid(gr, loc);
    }


Oh I see. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1