1

I'm programming a game in python3.6. There is some pawns on the board which are instance of the class 'pawn'. There is also boulders on the board which are instance of the class boulder. I just want to store these pawns and boulders in an array, like a numpy.array.

I have 2 problems :

  • There is different type of object in the array, which is not possible with a numpy.array

  • How can I represent an empty cell on my board, because I can't use an object, which has not the same type as the others.

How can I solve these two problems ? Is there already an object which can represent grid, an array and which accepts different type ?

6
  • 2
    Why not just use a list of lists? Commented Mar 7, 2019 at 18:23
  • 2
    "I just want to store these pawns and boulders in an array, like a numpy.array." But why? Why would you want to use a numpy.ndarray here, which are specialized data-structures for holding typed-multi-dimensional arrays? You could use a structured array, but again, why? What benefit do you get over using a normal list with normal python objects? Are you trying to represent N-dimensional chessboards with potentially millions of rows/slices? IOW, yes there is already an object that does that, it's called a list Commented Mar 7, 2019 at 18:25
  • 2
    An alternative to a list of lists, is a dictionary. That could be better if a lot of the board is empty. Commented Mar 7, 2019 at 18:26
  • Note, you could use a numpy array with dtype=object, but again, why not just use a list at that point? A numpy dtype=object array is basically a less useful python list Commented Mar 7, 2019 at 18:26
  • 1
    For a chess size board, storage efficiency isn't an issue. But display and search efficiencies still matter. An array, object or otherwise, is good for display purposes, but not that good for searching. Commented Mar 7, 2019 at 18:54

1 Answer 1

3

Use np.dtype(object): np.array(board, dtype=np.dtype(object))

As for empty cells: just set them to None.


Edit: as some people have suggested, you might not need a numpy array at all. A list of lists or a dict with tuple indexes might solve your problem just fine and remove the overhead of using numpy.

Sign up to request clarification or add additional context in comments.

9 Comments

why? Why not use a list??? A numpy.ndarray with dtype=object is essentially a less performant / useful python list
He asked about numpy arrays, and it's possible to use them. So why not? But yeah, lists should work just fine too.
If someone asks how to shoot themselves in the foot, one should generally say "don't shoot yourself in the foot, instead, do <a better alternative>"
numpy arrays also allow for more flexible indexing, and have some functionality beyond what standard lists provide.
@user2699 what functionality would be relevant for a 8x8 chessboard of Python objects??? This makes no sense. You shouldn't be encouraging people to use the wrong tool.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.