Reputation: 364
For creating a UnboundedGrid and show it:
ActorWorld world = new ActorWorld(new UnboundedGrid<Actor>());
world.show();
For creating a BoundedGrid (let's say 10x10) and show it:
ActorWorld world = new ActorWorld(new BoundedGrid<Actor>(10, 10));
world.show();
There is any way to create a Custom UnboundedGrid? By custom, I mean one of the parameters (cols or rows) is not unbounded.
I've checked UnboundedGrid's GridWorld source code and it extends AbstactGrid which implements Grid but I don't imagine any way of doing this.
Upvotes: 0
Views: 470
Reputation: 1988
The best idea is to implement it yourself by extending AbstractGrid. I suggest using an array of Maps that map integers representing the columns with the actors and the length of the array represents the number of rows. (Remember, you can't have generic arrays, so you have to use raw types.) Like this:
public class CustomUnboundedGrid<E> extends AbstractGrid<E>
{
private Map[] data;
private int cols;
public CustomUnboundedGrid(int rows, int cols)
{
if(cols <= 0)
throw new IllegalArgumentException();
try
{
data = new Map[rows];
for(int i = 0; i < rows; ++i)
{
data[i] = new HashMap();
}
}
catch(NegativeArraySizeException e)
{
throw new IllegalArgumentException();
}
}
//override methods from AbstractGrid
}
Upvotes: 1