function [newX,newY] = fishSwim(x,y,m,n,world);
% Given matrix coordinates (x,y), returns random coordinates adjacent to
% (x,y).
% All adjacent points have equal probability of occuring.
% (newX,newY) will be coordinates of a zero entry in world: so the new
% fish coordinates are in a water cell.
% Uses the function move.
% INPUTS:   (x,y) are matrix coordinates <= (m,n), respectively
%           m,n are positive integers (matrix dimensions)
%           world is a mxn matrix of zeros and ones: ones represent land, zeros
%           represent water.
% OUTPUTS:  [newX, NewY] are matrix coordinates adjacent to the point (x,y)

currentPosition = [x,y];
onLand = 1;

while onLand
    % Set movement parameter
    % Corner cases
    if x == 1 & y == 1                              % top left corner
        moveParam = floor(2.99*rand)+4;                % param between 4 and 6
    elseif x == 1 & y == n                          % top right corner
        moveParam = floor(2.99*rand)+6;                % param between 6 and 8
    elseif x == m & y == 1                          % bottom left corner
        moveParam = floor(2.99*rand)+2;                % param between 2 and 4
    elseif x == m & y == n                          % bottom right corner
        moveParam = floor(2.99*rand);                  % param = 1,2, or 8
        if moveParam == 0
            moveParam = 8;
        end
 
    % Border, non-corner cases
    elseif x == 1                                   % top border
        moveParam = floor(4.99*rand)+4;                % moveParam between 4 & 8
    elseif x == m                                   % bottom border
        moveParam = floor(4.99*rand)+1;                % moveParam between 1 & 4, or =8
           if moveParam == 5
            moveParam = 8;
        end
    elseif y == 1                                   % left border
        moveParam = floor(4.99*rand)+2;                % moveParam between 2 & 6
    elseif y == m                                   % right border
        moveParam = floor(4.99*rand)+1;                % moveParam = 1,2,6,7, or 8
        if moveParam == 3
            moveParam = 6;
        elseif moveParam == 4
            moveParam = 7;
        elseif moveParam == 5
            moveParam = 8;
        end

    % Interior case    
    else    
        moveParam = floor(7.99*rand)+1;               % moveParam between 1 & 8
    end


    % Get new coordinates
    [xMove,yMove] = move(moveParam);
    newX = xMove + x;
    newY = yMove + y;
    if world(newX,newY)==0          % check that the fish's position is on water
        onLand=0;
    end
end % while loop
