Results 1 to 8 of 8
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Turning a Fish CODE

    I am lost on how to approach this method. My attempt is below.

    isAlive is a boolean method that returns true if fish size > 0
    fishIsSurroundedByRocks is a boolean method that returns true if the fish's position is surrounded by rocks

    Java Code:
    /**
    	 * Iterate through list of Fish.  For each fish that isAlive, do the following:
    	 * <p>
    	 * 1. If this fishIsSurroundedByRocks, DO NOTHING, and move on to the next fish.
    	 *     (This fish will not turn.)
    	 * <p>
    	 * 2.  If this fish's direction is not equal to one of the codes UP, DOWN, LEFT, or
    	 *     RIGHT, then throw an IllegalFishDirectionException, passing this fish's 
    	 *     direction to the constructor.
    	 * <p>    
    	 * 3.  Check whether or not this fish is about to hit a rock if it moves in it's 
    	 *     current direction.  If it is about to hit a rock, call the fish's 
    	 *     setRandomDirection method.  Repeat this step until the fish is no longer
    	 *     about to hit a rock.  Do not make any EXTRA calls to setRandomDirection or 
    	 *     you will fail our tests!
    	 */
    	public void turnFish() {
    
    		Iterator<Fish> it = fish.iterator();
    		Fish f;
    		f = it.next();
    		while (f.isAlive()) {
    
    			if (this.fishIsSurroundedByRocks(f)){
    				f = it.next();
    			}
    			else if ((f.getDirection() != 38) 
    					&&
    					(f.getDirection() != 39)
    					&&
    					(f.getDirection() != 36)
    					&&
    					(f.getDirection() != 37)){
    				throw new IllegalFishDirectionException(f.getDirection());
    			}
    			else{
    				if (f.getDirection() == ROCK);
    				f.setRandomDirection();
    			}
    
    		}
    	}

  2. #2
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Turning a Fish CODE

    What I would do to iterate through all of fish is from lines 20-22 I would replace it with
    Java Code:
    Fish f= null;
    while(it.hasNext()){
         f= it.next();
    or you can leave your code along and inbetween line 38 and 39 add
    f=it.next();

    This would fix the iteration. And then you can test the rest of your logic. If you have more questions let me know.
    Last edited by jbarke12; 05-07-2013 at 04:25 PM.

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Turning a Fish CODE

    I decided to use the for each iteration

    I have a method called move in my Fish class. It says that if my fish's Poisition is UP, then row decrements 1. If fish position = Right, then col increments. etc.

    how to I say or check. if after f.move() is implemented that it doesnt hit a rock?

    Java Code:
    	for (Fish f: fish){
    			while (f.isAlive() && this.fishIsSurroundedByRocks(f)){
    
    				if ((f.getDirection() != 38) 
    						&&
    						(f.getDirection() != 39)
    						&&
    						(f.getDirection() != 36)
    						&&
    						(f.getDirection() != 37)){
    					throw new IllegalFishDirectionException(f.getDirection());
    				}
    				else{
    					
    				
    					if (fish. == ROCK);
    					f.setRandomDirection();
    				}
    
    			}

  4. #4
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Turning a Fish CODE

    I would go to your method setRandomDirection and test for a rock before setting its position.

  5. #5
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Turning a Fish CODE

    Im not having trouble with iteration. I am having trouble putting this":
    * 3. Check whether or not this fish is about to hit a rock if it moves in it's
    * current direction. If it is about to hit a rock, call the fish's
    * setRandomDirection method. Repeat this step until the fish is no longer
    * about to hit a rock. Do not make any EXTRA calls to setRandomDirection or
    * you will fail our tests!



    into code...

    i dont know how to check if fish is going to hit a rock

    i have a getter for direction
    i have a move method that increments one unit depending on direction

    i dont have anyways of saying...if the fish is going to hit a rock..(this is the code part im struggling on)

    ----

    Any help explaining a code would be great. I know i have to call on the getter for direction, the move the fish. And if that move causes to hit a rock, then I have to set a random number...so its going to be some sort of while loop.. but im just lost.

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

    Default Re: Turning a Fish CODE

    Something like this, using if statements? obviously edit to include your actual methods, this is just an outline

    so you could:

    Java Code:
           for( Fish f: fish) {
           if(f.getDirection() != rock){
              f.move();
             
             }
            else {
            f.randomDirection();
            }
         }
    Last edited by monkeyjr97; 05-08-2013 at 03:25 AM.

  7. #7
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Turning a Fish CODE

    Thanks alot. I get it. Ill try it out.

  8. #8
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Turning a Fish CODE

    I have a question on copying... arraylist...same project
    should I start a new thread or ask here?

Similar Threads

  1. Replies: 2
    Last Post: 01-16-2013, 06:54 AM
  2. fish constructor
    By aizen92 in forum New To Java
    Replies: 27
    Last Post: 01-25-2011, 04:04 PM
  3. Fish animation
    By SwEeTAcTioN in forum AWT / Swing
    Replies: 3
    Last Post: 11-30-2009, 11:56 AM
  4. Fish animation
    By SwEeTAcTioN in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 07:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •