-1

In the Tetris game in the PyGame book, they create a board using:

 board = []
 for i in range(BOARDWIDTH):
    board.append([BLANK] * BOARDHEIGHT)
 return board

While that's fine, a list of lists, I was thinking about how they usually represent that in Java and other languages, as an array with set index - for example, int board[][] = new int[8][8]; // create 64 integers (a 2d array example).

From what I understand, tuples are more similar to the array (vs List which is more like ArrayList or LinkedList of Java). They have set number of elements and can't be added to, and hold a set number of items.

Why would we not create a tuple of tuples for a board, for example

board = ( (BLANK,)*10 ,)*10

Do they use lists of lists for some optimization in Python's internal representation of the board, or just because they assume users know List more commonly than Tuple?

This has the added benefit of being functional and making immutable objects. If you want to make a board with a state in which you moved in tic tac toe for example, you could try something like:

tictac = ((0,0,0),)*3
vals = (tuple((tictac[r][c]) if r!=1 or c!=1 else 'x' for (r,row) in enumerate(tictac)) for c in range(3))
for i in vals: print i

to move a 1 to the center.

List comprehensions are quite flexible in Python and this is more functionally-correct.

NoBugs
  • 9,310
  • 13
  • 80
  • 146
  • 2
    1. There's not much use in a board you can't change, and 2. multiplying `list`s like that will lead to [trouble](http://stackoverflow.com/questions/240178/python-list-of-lists-changes-reflected-across-sublists-unexpectedly). – TigerhawkT3 Feb 03 '16 at 07:48
  • @TigerhawkT3 The side-effect problem in linked question actually wouldn't happen if they created immutable tuples and operated on copies of them in functional style. – NoBugs Feb 05 '16 at 06:13

1 Answers1

1

The difference between list and tuple in python is that tuple is immutable. That's why you can't change the size of a tuple (since you can't change it at all).

So the reason one normally would chose a list in this case is that you normally want to change the contents of the game board.

The benefits of having a matrix of fixed size may be slim. One could of course argue that one might get some performance benefit, but AFAIK the built in types are anyway of dynamic size in the sense that the size is determined at runtime and each time you access the container. In this aspect the list and tuple does not differ, that is when you write board[3][4] python would first access board check if it's length is larger than 3 so it can deliver the [3] element, then that element happens to be a list/tuple which is then checked for it size so it's larger than 4 so it can deliver the [4] element.

One could also argue that it would be for safety reasons or that it's "good practice" to not allow the board to be resized, and I could agree on that. What one then would do to allow modification is to wrap it all in a class that doesn't allow for resizing. One should bear in mind however that python doesn't provide very hard protection of classes that prohibit you from fiddling with internals - rather python trusts the programmer to not doing ugly things instead of trying to forbid.

skyking
  • 13,817
  • 1
  • 35
  • 57
  • You can pass a *changed copy* of the board as in functional programming style. Please see updated question. – NoBugs Feb 05 '16 at 06:02
  • @NoBugs Yes, however then you don't have the restriction that size need to be unchanged either - the mutability and the possibility to change size goes hand in hand even here. – skyking Feb 05 '16 at 06:07