slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
GridWorld PowerPoint Presentation
Download Presentation
GridWorld

Loading in 2 Seconds...

play fullscreen
1 / 33

GridWorld - PowerPoint PPT Presentation


  • 158 Views
  • Uploaded on

GridWorld. GridWorld - Part 2 Bug Variations. Bug. Bug. Bug extends Actor but differs from Actor in that a Bug actually moves from cell to cell.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'GridWorld' - neal


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide2

GridWorld - Part 2

Bug Variations

slide4

Bug

Bug extends Actor but differs from Actor in that a Bug actually moves from cell to cell.

A Bug moves to the cell immediately in front if possible. If a move is not possible, the bug turns in 45 degree increments until it finds a spot to which it can move.

slide7

Bug

ActorWorld world = new ActorWorld();

Bug dude = new Bug();

world.add(new Location(3,3), dude);

Bug sally = new Bug(Color.GREEN);

sally.setDirection(Location.SOUTHEAST);

world.add(new Location(2,2), sally);

Bug ali = new Bug(Color.ORANGE);

ali.setDirection(Location.NORTHEAST);

world.add(new Location(1,1), ali);

world.show();

slide9

open

bugone.java

slide10

demo

boxBug.java

slide11

Set 2 Questions

1. What is the role of the instance variable sideLength?

The sideLength instance variable defines the number of steps a BoxBug moves on each side of its box.

2. What is the role of the instance variable steps?

The steps instance variable keeps track of how many steps a BoxBug has moved on the current side of its box.

3. Why is the turn method called twice when steps becomes equal to sideLength?

When a BoxBug travels sideLength steps, it has to turn 90 degrees to travel along the next side of its box. The turn method only executes a 45 degree turn; therefore it takes two turn method calls to turn 90 degrees.

4. Why can the move method be called in the BoxBug class when there is no move method in the BoxBug code?

The BoxBug class extends the Bug class, and the Bug class has a public move method. Since the BoxBug class is a subclass of the Bug class, it inherits the move() method from the Bug class.

slide12

Set 2 Questions

5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not?

Yes. When a BoxBug is constructed, the side length is determined and cannot be changed by client code.

6. Can the path a BoxBug travels ever change? Why or why not?

Yes. If another Actor, like a Rock or Bug, is in front of a BoxBug when it tries to move, the BoxBug will turn and start a new box path.

7. When will the value of steps be zero?

Initially, the value of steps is set to zero when a BoxBug is constructed. After that, the value of steps will be set to zero when steps is equal to sideLength—meaning the BoxBug has completed one side of its box path, or when the BoxBug cannot move and turns instead to start a new box path.

slide13

Bug Methods

in more detail

slide14

Bug

What does a Bug do when its act() method is called ?

What methods does the act() method appear to call?

slide15

Bug

public void act()

{

if (canMove())

move();

else

turn();

}

Let’s look at the Bug class

slide17

canMove

The bug act method looks to see if a move is possible by calling canMove.

canMove looks at the location in front of this bug to see if it is empty or

if it contains a flower.

canMove returns true or false.

slide18

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

}

instanceof keyword checks to see if object is a certain type

slide19

move

The bug act method calls move if canMove returns true.

move calls moveTo to move the bug to the location in front of this bug. move leaves a flower in the old location.

slide20

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);

}

slide21

turn

The bug act method calls turn if canMove returns false.

turn changes the direction of the bug by 45 degrees to the right.

slide22

public void turn()

{

setDirection(getDirection() +

Location.HALF_RIGHT);

}

slide23

Bug

ActorWorld world = new ActorWorld();

Bug dude = new Bug(Color.GREEN);

dude.setDirection(Location.EAST);

Location loc = new Location(5,5);

world.add(loc , new Rock());

loc = new Location(2,5);

world.add(loc, new Flower());

loc = new Location(2,7);

world.add(loc, dude);

world.show();

slide24

open

bugtwo.java

slide26

Extending Bug

Questions you need to ask

  • How will the new bug differ from the original bug?
  • Can the new behavior be created using existing methods?
  • Which of the methods will be overridden?
  • Will new methods need to be added?
slide27

Tips for Extending Bug

  • Override Bug's act method
  • Each call to the move() method should be guarded by a call to the canMove() method
  • Add additional instance fields if needed
  • Add new methods to the subclass if needed
  • Constructors for the subclass call super() or super(someColor)
slide28

Extending Bug

What has to change if you want the bug to go backwards instead of forwards?

Use the GW quick reference!

slide29

Students partner to

create BackwardBug

class (5 minutes)

slide30

Extending Bug

public class BackwardBug extends Bug

{

//constructor

public void act()

{

}

//other methods

}

Is this the only way to write this class?

What methods could be changed?

slide31

discuss

BackwardBug options

slide32

What have we learned about Bugs?

  • Bug class: - constructor method - act() - canMove() - move() - turn()
  • Grid class: - isValid() - get() - getNumRows() - getNumCols()
  • Location class: - getRow() - getCol() - getAdjacentLocation()
  • Actor class: - getGrid() - putSelfInGrid() - removeSelfFromGrid() - moveTo()
  • BoxBug class: - overriding act() - using instance variables to control movement of a bug
slide33

Start work

on MultiSpiralBug

class