GridWorld Help (Exam Pre-Test)

Discussion in 'Programming General' started by Glyder, Mar 6, 2011.

GridWorld Help (Exam Pre-Test)
  1. Unread #1 - Mar 6, 2011 at 4:27 PM
  2. Glyder
    Joined:
    Sep 6, 2009
    Posts:
    527
    Referrals:
    5
    Sythe Gold:
    0

    Glyder Forum Addict

    GridWorld Help (Exam Pre-Test)

    For my Pre-Test in my CPU Prog class, we had to make a ZigZagBug Class.

    I used a line from a person that I know in the class, but for a comment with
    that line, I don't know how to explain it.

    Here is the code.

    Code:
    import info.gridworld.actor.Bug;
    
    public class ZigZagBug extends Bug {
        int counter = 1;
        
        public ZigZagBug(){
            turn();
        }
        
        public void act(){
            if(canMove()){
                //This gets the adjacent location, of the direction that the bug is facing.
                //IE - Faces Right-North, the next location is the location directly in front of what the bug faces.
                moveTo(getLocation().getAdjacentLocation(getDirection()));
                setDirection(getDirection() + 90 + ((counter++)%2 * 180));
            }else{
                //If the bug cannot move, it turns 180 degrees and then moves again.
                setDirection(getDirection() + 180);
            }
        }
    }
    Now, what is..

    This for?
    Code:
    int counter = 1;
    And this for?
    Code:
    setDirection(getDirection() + 90 + ((counter++)%2 * 180));
     
  3. Unread #2 - Mar 9, 2011 at 3:39 PM
  4. Quadro
    Joined:
    Feb 4, 2011
    Posts:
    869
    Referrals:
    0
    Sythe Gold:
    0

    Quadro Apprentice
    Banned

    GridWorld Help (Exam Pre-Test)

    We can't tell because you haven't shown us the Bug class...
     
  5. Unread #3 - Mar 10, 2011 at 2:16 PM
  6. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

    GridWorld Help (Exam Pre-Test)

    Based on what the name suggests a "ZigZagBug" would be moving in a ZigZag shape.

    If you are using a counter and using the %2 (mod or modulo) operator it would basically give you whether the number was odd or even.

    So the counter determines the "step" of the zigzag that it is at - 0 being even 1 being odd.

    The setDirection uses the counter to draw this change in direction - basically saying in pseudo code - use the current direction, add 90 degrees, then add another 0 or 180 degrees based on which step it is currently at.


    As Quadro states - it would be necessary to see the full implementation of the class to be sure, but this sounds about right. good luck - take a look at my java help link (in sig) if you need more help.
     
  7. Unread #4 - May 4, 2011 at 6:10 AM
  8. clint999
    Joined:
    Jun 4, 2008
    Posts:
    110
    Referrals:
    0
    Sythe Gold:
    0

    clint999 Active Member

    GridWorld Help (Exam Pre-Test)

    We can't tell because you haven't shown us the Bug class...
     
< Buying Java Help | Little Help >

Users viewing this thread
1 guest


 
 
Adblock breaks this site