-3

I want to create a game which has for example 20x30 fields,my first thought was that each field would be a list(becouse sometimes some field contains more than one object),but after reading a few questions about the array of generic lists,I've realised it's not a good solution.

The game is going to be a very simple version of Raft,playing it in console with commands like "left" so the character moves one field left. So can you recommend a good container or something to design the fields?

An illustration of the game: http://imgur.com/a/8YpeA

Ereghard
  • 109
  • 3
  • How about using a list of generic lists instead of an array of generic lists. – janos Jul 16 '17 at 09:23
  • Why do you think the array of lists is not good? – Henry Jul 16 '17 at 09:29
  • I think I will try the list of list,thanks,as for the second question,you can check it out here: https://stackoverflow.com/questions/7810074/array-of-generic-list – Ereghard Jul 16 '17 at 09:35

2 Answers2

1

If you are implementing a fixed-sized rectangular "playing surface", then I would recommend a Cell[][] where the Cell type is a custom class that represents the state of a cell on the surface.

There is no need to use generics here ... unless you are trying to implement an abstraction that can be used in many games.

Also, there is no need for list-like functionality. You don't remove or add cells to the board. You move pieces / players / whatever from one cell to another.

(If your board is not based on discreet cells, the you will need to use some other approach. But any 2D playing surface with a bounded number of discrete cells can be mapped to a Java array, one way or another.)


So can you recommend a good container or something to design the fields?

Recommendations for libraries and tools are OFF-TOPIC.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
1

There are two ways you can create generic arrays:

  1. Create private static inner class of a type:

    transient Node<K, V>[] table;
    
    public MyHashMap(int limit) {
        this.limit = limit;
        table = (Node<K, V>[]) new Node[limit];
    }
    
  2. Use Array.newInstance(class, size)

moreover using generics for custom implementations is not a good choice.

Gaurava Agarwal
  • 944
  • 1
  • 9
  • 30