www.webdeveloper.com
Results 1 to 6 of 6

Thread: Two dimensional Array Problem

  1. #1
    Join Date
    Nov 2004
    Posts
    3

    Question 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} };

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    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];

  3. #3
    Join Date
    Nov 2004
    Posts
    3

    Two dimesional Array Problem

    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.

  4. #4
    Join Date
    Oct 2004
    Location
    Poland
    Posts
    3
    I'd use three step randomization.

    1 step - choose direction
    2 step - choose length of the movement
    3 step - choose spped of the movement

  5. #5
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    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.

  6. #6
    Join Date
    Nov 2004
    Posts
    3
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles