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
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,
Code:
for (x = 0, x < 21, y++)
for (y = 0, y < 11, y++)
v = pond[x][y];
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.
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.
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.
Bookmarks