aspose file tools*
The moose likes Beginning Java and the fly likes Predator prey sim?  2D array of Lists?  Frustrated and stumped Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Predator prey sim?  2D array of Lists?  Frustrated and stumped" Watch "Predator prey sim?  2D array of Lists?  Frustrated and stumped" New topic
Author

Predator prey sim? 2D array of Lists? Frustrated and stumped

Ish Del
Greenhorn

Joined: Dec 05, 2012
Posts: 18
Hello everyone, i am trying to figure out a problem and just am stumped. I have tried to solve this for a couple of days and just am getting nowhere and getting really frustrated. My problem is I have to create a predator prey sim of sorts. Lets say I have grass and rabbits. A field that is a 10x10 grid. I can have multiple instances of grass and rabbits in each individual square grid[i][j]

So my thinking is make a 2d Array of type Object. Then loop through and assign each grid[i][j] space with a new ArrayList of superClass grassrabbit. But i need to be able to add and remove grass and rabbits from the list. But to do that dont i need to have a variable name for each arraylist for each grid space[i][j] to be able to use the add and remove methods? There must be a better way than having 100 different variable arraylists like arraylist1 in grid[0][0] . arraylist2 in grid[0][1] etc...

Im just so incredibly confused as to the best way to do something like this? Am i way off on the approach? How would you do it? I honestly just dont get what to do.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Welcome to the Ranch

I am afraid I am finding it difficult to understand your problem. You are not supposed to create arrays of Lists in the first place, because of shortcomings in the generics (to do with erasure). And is the grass a predator or the rabbit? I thought you would want foxes and rabbits.
Why don’t you create a Square class, probably thinking of a better name for it than Square. Each Square object can be linked to any number of Fox objects or Rabbit objects. Then you can have a Square[][] array.
By the way: that isn’t a 2D array, because Java doesn’t support 2D arrays. It is an array of arrays, which is actually subtly better than a 2D array.
Ish Del
Greenhorn

Joined: Dec 05, 2012
Posts: 18
Thank you for responding. The real issue is I dont know what i need to use or the easiest way to go about it. This piece I asked about is part of a larger problem but I will tackle that myself after this. Maybe i can clarify lets say we have grass and rabbits. Forget that grass is eaten by rabbits im only interested in the placement of things for now.

There is grass
There is rabbits
There is some maximum total amount of grass+rabbits allowed in a single gridspace[10][10] lets say 5. So for each grid[0][0] or grid[0][1] etc... there can be up to 5 rabbits+grass combination in each space of which there are 100 spaces in this 10x10 example.
The grid or map or field whatever you want to call it should be a 10x10 space.

Since its not just 1 thing per space i thought i needed some sort of list or array that could hold up to 5 or 10(whatever that max # i decide is) individual grass+rabbit combinations. That is where the thought of a list within each square[#][#] space of the map.

This is also where i though about a super class of which rabbit and grass extend. THat way i could assign that superclass list to the object square space and that list contained both objects of subclass rabbits and sub class grass. This of course could be a totally wrong way to think abuot it.

Does that clear up your confusion with my question? Thanks for the help it is appreciated. I know it must be hard to decipher what I want since I myself am confused when wording it.

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
If you want a common superclass it would have to be something like Player. There is a great difference between grass and rabbits, even if you have grass which runs around the field
You can have a Square class with grass and rabbit fields, which you can change to grasses and rabbits (Lists) if you wish.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

i'd probably create a class called "PlotOfLand" or some such. It could contain zero or more Grass objects and zero or more Rabbit objects.

It wouldn't be too hard to put in logic such that when you add a new Grass/Rabbit objects, it checks to make sure you don't put in a 6th one.

Then I'd create a Field or Landscape class that holds the 100 PlotOfLand objects.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2377
    
  28

I would imagine that if you wanted to make this extensible, you would want to have interfaces that denote things that are either stationary, movable or self locomotive (can move themselves) . SO, I would create a Thing interface that has the functions for common behaviors among all things. Then have a Stationary, Movable and SelfLocomotive interfaces that contain the contract for objects that are stationary, movable and self locomotive respectively. Grass implements Thing and Stationary. Rabbit implements SelfLocomotive, Movable and Thing.

On second thought maybe stationary doesn't make sense. A force strong enough should be able to move any thing.

Then you can have a class called Square or Plot(I like Plot) that holds a List of Things. As you add behaviors you can keep adding interfaces (for example, Eater and Eatable).
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Fred is correct! +1

A "plot" can contain a maximum of 5 Objects, Rabbits or Grass, so as an "attribute", what type of collection do you think would work?

The "field" contains 10x10 "plots". Easy.


Aside, for interview questions I ask people about a deck of cards, 52 cards (no Jokers), 4 suits, 13 ranks, and how they would design that.

It makes them/you think and is very appropriate to find out what someone actually knows beyond the theoretical.

WP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
You can have static fields in the Rabbit and Grass classes which count when instances are created. Easy. But it is much harder to count when instances are un‑created.
Ish Del
Greenhorn

Joined: Dec 05, 2012
Posts: 18
William P O'Sullivan wrote:Fred is correct! +1

A "plot" can contain a maximum of 5 Objects, Rabbits or Grass, so as an "attribute", what type of collection do you think would work?

The "field" contains 10x10 "plots". Easy.


Aside, for interview questions I ask people about a deck of cards, 52 cards (no Jokers), 4 suits, 13 ranks, and how they would design that.

It makes them/you think and is very appropriate to find out what someone actually knows beyond the theoretical.

WP



Having trouble conceptualizing this. Can someone pseudocode it for me?
Class PlotOfLand (Plot of land represents each square correct?)

List<SuperClass> and add new rabbits and grass to it.

So each square holds collections of rabbits and collection of grass in a single collection?

Then we have a the main class which creates an Object[10[10] array ? We loop through each element and assign object[i][j] = new PlotOfLand?

Maybe i need to see simple code to figure this out? I am still struggling with the concept. When you said above you would create a field or landscape class that holds the 100 plot objectts. Dont all those plots need a distinct variable name? plot1.add(new Grass) plot1.add(new Rabbit) etc... then plot2.add all the way to plot 100? Am i missing something are we really supposed to have 100 individual plot objects with their own variable names to add remove to? Or would it be 100 individual lists of collections with their own variable names? 1 in each plot?

LOL i just confuse myself more and more.







fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Actually, what I think you need is LESS code. Have you read our page titled StopCoding?
Ish Del
Greenhorn

Joined: Dec 05, 2012
Posts: 18
fred rosenberger wrote:Actually, what I think you need is LESS code. Have you read our page titled StopCoding?


No I haven't but i will definitely read it shortly during lunch. Thank You.
Ish Del
Greenhorn

Joined: Dec 05, 2012
Posts: 18
Is something like this acceptable?

Grass Class
fields and constructor like alive =true;

PlotOfLand Class
fields: Grass[] grass;

grass = new Grass[10];


EcoSim Class
PlotOfLand[][] field = new PlotOfLand[10][10];


I could use logic to make sure .length of grass + .length of rabbits was never over 10.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Maybe. But my gut reaction is it's more complicated than you need. Do you need to keep track of live grass and dead grass? Does the position in the array of grass mean anything? Or would it be sufficient to just keep track of how much (live) grass was in each plot of land? That is, you could just use a single number to represent this, with no Grass objects. Optionally you could use another number to track the amount fo dead grass as well, if you need that.
Ish Del
Greenhorn

Joined: Dec 05, 2012
Posts: 18
Mike Simmons wrote:Maybe. But my gut reaction is it's more complicated than you need. Do you need to keep track of live grass and dead grass? Does the position in the array of grass mean anything? Or would it be sufficient to just keep track of how much (live) grass was in each plot of land? That is, you could just use a single number to represent this, with no Grass objects. Optionally you could use another number to track the amount fo dead grass as well, if you need that.


Hello Mike, no I dont need to keep track of dead grass or live grass other than the maximum amount allowed in the plot. The position in the grass array in the plot means nothing. However the position of plot[i][j] vs other plots is important for the grow method as it can only grow in adjacent plots.

I was thinking about just using integers to represent the objects (THe term is encoding i believe?) but for some reason i thought it wasnt good design or somehow I was cheating.



Question when working in levels like above is it standard to step back and forth to create and pass objects?
Main creates plot[][]. plot creates grass[]. grass is individual object.
So to populate do the reverse grass function creates grass[] and returns it to plot. plot returns itself to main and main stores that plot[][] in an indexed location.
Is that normal programming practice?

Thanks.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Predator prey sim? 2D array of Lists? Frustrated and stumped