/    Sign up×
Community /Pin to ProfileBookmark

Two dimensional Array Problem

I am working on a java program that is an optimization problem. The problem is i have this man who wants to go fishing and i have to represent this pond by a 2D array. Also i have to show fish movement during the iterations. I can create the 2D array manually using numbers to represent land=0, water=1, and where fish are present=2. Here is the array at the bottom. I have to optimize the number of fish caught by going to certain locations where fish are present, and this is the part where i get confused. I dont know how to write code for the movement of the fish and how the fisherman gets to those locations, etc. If i could be provided with some syntax for writing 2D arrays it would be helpful

int[][] pond = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,0},
{0,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,0},
{0,1,1,1,1,1,1,1,1,1,1,2,2,2,1,2,1,1,1,1,0},
{0,2,2,1,1,1,1,1,2,1,1,1,1,1,1,1,1,2,1,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0},
{0,1,1,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,2,1,1,1,2,1,1,2,1,1,1,1,1,1,1,1,1,2,0},
{0,2,2,2,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };

to post a comment
Java

5 Comments(s)

Copy linkTweet thisAlerts:
@ray326Nov 02.2004 — First, think of them as (x,y) coordinates. Looks like x runs from 0 to 20 and y runs from 0 to 10. Now to access a point on the pond and get the entity there you get pond[x][y].

To scan the whole pond,
<i>
</i>for (x = 0, x &lt; 21, y++)
for (y = 0, y &lt; 11, y++)
v = pond[x][y];
Copy linkTweet thisAlerts:
@dantepauthorNov 02.2004 — Those for loops help me out to scan the pond. Now my next challenge is to make some fish behavior so that after each iteration the 2's move around. I was suggested using something like using a random number generator and use the number in an equation like x%4 and by using the mod fuction i could associate values for each direction like 0=right 1= left, 2 = up, 3= down. Since i have never coded something like this i am very unsure how to code a random generator and then use in the sense that was suggested to me by my professor.
Copy linkTweet thisAlerts:
@stahuNov 03.2004 — I'd use three step randomization.

1 step - choose direction

2 step - choose length of the movement

3 step - choose spped of the movement
Copy linkTweet thisAlerts:
@ray326Nov 04.2004 — Check java.lang.Math.random() for a "normal" acting random function and keep in mind that once you choose a direction, the fish may not be able to move there because it's blocked.
Copy linkTweet thisAlerts:
@dantepauthorNov 04.2004 — i did use the java class Random and i created a fish behavior using the random number with it mod by 4 and then used different numbers to delcare all directions.
×

Success!

Help @dantep spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...