0% found this document useful (0 votes)
851 views3 pages

Zigzag

The document describes a proposed new type of bug called a ZigZagBug for the GridWorld case study. A ZigZagBug moves diagonally in a zigzag pattern, alternating between right and left turns. It tracks its direction using a private turnRight variable and reverses direction if a move is blocked. Methods for move(), canMove(), and turn() need to be implemented to define the ZigZagBug's behavior.

Uploaded by

ZigZagBugCC
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
851 views3 pages

Zigzag

The document describes a proposed new type of bug called a ZigZagBug for the GridWorld case study. A ZigZagBug moves diagonally in a zigzag pattern, alternating between right and left turns. It tracks its direction using a private turnRight variable and reverses direction if a move is blocked. Methods for move(), canMove(), and turn() need to be implemented to define the ZigZagBug's behavior.

Uploaded by

ZigZagBugCC
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ZigZagBug

This question involves reasoning about the code from the GridWorld case study. A copy of the
code is provided as part of the exam.

Consider defining a new type of bug called ZigZagBug, which moves in a zigzag pattern. The
ZigZagBug always faces in a diagonal direction.The first time a ZigZagBug moves, it will move
forward if that location is empty. This is illustrated in the figure below as the move from position 1
to position 2. The bug will then turn 90 degrees to the left. In each subsequent move, the
ZigZagBug will attempt to move forward , and then turn 90 degrees in the opposite direction of
its previous turn (the second move will forward with a right turn, the third move will forward with
a left turn, and so on). If the ZigZagBug is unable to move, it stays in the same location but
reverses its direction. After reversing its direction, the next time the ZigZagBug moves, it will
attempt to turn in the same direction as it tried before reversing. The diagrams below show the path
followed by a single ZigZagBug object as a result of multiple moves.

1 3
Now consider 2 4 what happens when the ZigZagBug attempts to
move forward from position 4. This move is blocked, and
consequently the ZigZagBug stays in the same location but reverses
its direction. This is illustrated as position 5 in the diagram below.
From position 5, the ZigZagBug moves forward and turns right, and from position 6, it moves
forward to position 7 and turns left.

7 5
8 6
The ZigZagBug class is defined by extending the Bug class and overriding the move, and the
turn methods. Because a ZigZagBug alternates its pattern of movement, a private instance
variable turnRight keeps track of the direction of the next movement.

The partial declaration for class ZigZagBug is shown below:

public class ZigZagBug extends Bug


{
private boolean turnRight;
// true indicates the next turn should be to the
// right, false indicates the next turn should be
// to the left.

public ZigZagBug()
{
turnRight=false;
}

//moves this ZigZagBug diagonally, alternating right and


//left diagonals.
public void move()
{ /* to be implemented in part (a) */ }

// turns the bug 180 degrees without changing its location


//
public void turn()
{ /* to be implemented in part (b) */ }
(a) Override the move method for the ZigZagBug class. Assume that the canMove method is
working correctly, and has returned true. The bug should move diagonally to the right or the left
depending on the state of the turnRight boolean variable. You may use any of the accessible
methods of the classes in the case study.

Complete the method move below.

//moves this ZigZagBug diagonally, alternating right and


//left diagonals.
public void move()

(b) Override the canMove method for the ZigZagBug class. This method returns true if the
ZigZagFish can move into an empty location or a location with a flower along the appropriate
diagonal indicated by the turnRight variable, and false when it cannot.

//returns a boolean indicating whether the ZigZagBug can


//move in the appropriate diagonal direction into an empty
//location or location with a flower
public Boolean canMove()

(c) Override the turn method for the ZigZagBug class. This method reverses the direction of
the ZigZagBug without changing its location.

// turns the bug 180 degrees without changing its location


//
public void turn()

You might also like