Velocity Reviews > searching a list of lists as a two-dimensional array?

# searching a list of lists as a two-dimensional array?

agent-s
Guest
Posts: n/a

 02-12-2007
Basically I'm programming a board game and I have to use a list of
lists to represent the board (a list of 8 lists with 8 elements each).
I have to search the adjacent cells for existing pieces and I was
wondering how I would go about doing this efficiently. Thanks

James Stroud
Guest
Posts: n/a

 02-12-2007
agent-s wrote:
> Basically I'm programming a board game and I have to use a list of
> lists to represent the board (a list of 8 lists with 8 elements each).
> I have to search the adjacent cells for existing pieces and I was
> wondering how I would go about doing this efficiently. Thanks
>

This isn't very clear. What do you mean by "I have to search the

If piece is 1 and empty is 0 and piece is at ary[row][col]:

import operator
srch = [(i,j) for i in [-1,0,1] for j in [-1,0,1] if (i,j) != (0,0)]
is_adj = reduce(operator.or_, [ary[row+i][col+j] for (i,j) in srch]])

James

Samuel Karl Peterson
Guest
Posts: n/a

 02-12-2007
James Stroud <(E-Mail Removed)> on Sun, 11 Feb 2007 16:53:16 -0800
didst step forth and proclaim thus:

> agent-s wrote:
> > Basically I'm programming a board game and I have to use a list of
> > lists to represent the board (a list of 8 lists with 8 elements each).
> > I have to search the adjacent cells for existing pieces and I was
> > wondering how I would go about doing this efficiently. Thanks
> >

>
> This isn't very clear. What do you mean by "I have to search the
> adjacent cells for existing pieces"?
>
> If piece is 1 and empty is 0 and piece is at ary[row][col]:
>
> import operator
> srch = [(i,j) for i in [-1,0,1] for j in [-1,0,1] if (i,j) != (0,0)]
> is_adj = reduce(operator.or_, [ary[row+i][col+j] for (i,j) in srch]])

Wow, maybe it's just me (I'm a pretty bad programmer) but this is
where list comprehensions begin to look unreadable to me. Here's a
C-like way to do it, (warning, untested in python):

for i in range(:
for j in range(:
for offset_i in range(-1,2):
for offset_j in range(-1, 2):
row = i + offset_i
col = j + offset_j
if (row < 0 or row > 7) or (col < 0 or col > \
or ((row,col) == (i,j)):
continue
# else do something with board[row][col]

I realize this is gross and un-Pythonic and does the same thing the
above code does, but it's probably the way I'd choose to do it .
Then again, I've been negatively influenced by doing a game of life in
C a few months back.

--
Sam Peterson
skpeterson At nospam ucdavis.edu
software would be much better" -- unknown

Gabriel Genellina
Guest
Posts: n/a

 02-12-2007
En Mon, 12 Feb 2007 02:24:54 -0300, Samuel Karl Peterson
<(E-Mail Removed)> escribió:

> James Stroud <(E-Mail Removed)> on Sun, 11 Feb 2007 16:53:16 -0800
> didst step forth and proclaim thus:
>
>> agent-s wrote:
>> > Basically I'm programming a board game and I have to use a list of
>> > lists to represent the board (a list of 8 lists with 8 elements each).
>> > I have to search the adjacent cells for existing pieces and I was
>> > wondering how I would go about doing this efficiently. Thanks

> Wow, maybe it's just me (I'm a pretty bad programmer) but this is
> where list comprehensions begin to look unreadable to me. Here's a
> C-like way to do it, (warning, untested in python):

Just for fun, and to add one more way. This is a generator for adjacent
indexes, that can be used to search for occupied cells, for locating a
suitable next move, or whatever:

.... for ni in (i-1, i, i+1):
.... if ni not in range(: continue
.... for nj in (j-1, j, j+1):
.... if nj not in range(: continue
.... if ni!=i or nj!=j:
.... yield ni,nj
....
py>
[(3, 2), (3, 3), (3, 4), (4, 2), (4, 4), (5, 2), (5, 3), (5, 4
[(6, 2), (6, 3), (6, 4), (7, 2), (7, 4)]
[(3, 6), (3, 7), (4, 6), (5, 6), (5, 7)]
[(0, 6), (1, 6), (1, 7)]

--
Gabriel Genellina

Gabriel Genellina
Guest
Posts: n/a

 02-12-2007
En Mon, 12 Feb 2007 02:24:54 -0300, Samuel Karl Peterson
<(E-Mail Removed)> escribió:

> James Stroud <(E-Mail Removed)> on Sun, 11 Feb 2007 16:53:16 -0800
> didst step forth and proclaim thus:
>
>> agent-s wrote:
>> > Basically I'm programming a board game and I have to use a list of
>> > lists to represent the board (a list of 8 lists with 8 elements each).
>> > I have to search the adjacent cells for existing pieces and I was
>> > wondering how I would go about doing this efficiently. Thanks

> Wow, maybe it's just me (I'm a pretty bad programmer) but this is
> where list comprehensions begin to look unreadable to me. Here's a
> C-like way to do it, (warning, untested in python):

Just for fun, and to add one more way. This is a generator for adjacent
indexes, that can be used to search for occupied cells, for locating a
suitable next move, or whatever:

.... for ni in (i-1, i, i+1):
.... if ni not in range(: continue
.... for nj in (j-1, j, j+1):
.... if nj not in range(: continue
.... if ni!=i or nj!=j:
.... yield ni,nj
....
py>
[(3, 2), (3, 3), (3, 4), (4, 2), (4, 4), (5, 2), (5, 3), (5, 4
[(6, 2), (6, 3), (6, 4), (7, 2), (7, 4)]
[(3, 6), (3, 7), (4, 6), (5, 6), (5, 7)]
[(0, 6), (1, 6), (1, 7)]

--
Gabriel Genellina

Paul Rubin
Guest
Posts: n/a

 02-12-2007
"agent-s" <(E-Mail Removed)> writes:
> Basically I'm programming a board game and I have to use a list of
> lists to represent the board (a list of 8 lists with 8 elements each).
> I have to search the adjacent cells for existing pieces and I was
> wondering how I would go about doing this efficiently. Thanks

You're getting a bunch of Pythonic suggestions which are easy to
understand though not so efficient. If you're after efficiency you
might look at a book like Welsh and Baczynskyj "Computer Chess II" for
some techniques (warning, this is a rather old book though) and
program in a closer-to-the-metal language. One common approach these
days is "bit boards". Basically you represent the board state as a
bunch of 64-bit words, one bit per square. So for checking occupancy,
you'd have a word having the bits set for occupied squares. If you
only want to check adjacent squares (king moves), you could have a
bunch of bit masks (one for each square) with bits set only for the
adjacent squares (similarly for bishop moves, rook moves, etc.) Then
square, then AND it against the occupancy word. Normally you'd have

James Stroud
Guest
Posts: n/a

 02-12-2007
Paul Rubin wrote:
> "agent-s" <(E-Mail Removed)> writes:
>
>>Basically I'm programming a board game and I have to use a list of
>>lists to represent the board (a list of 8 lists with 8 elements each).
>>I have to search the adjacent cells for existing pieces and I was
>>wondering how I would go about doing this efficiently. Thanks

>
>
> You're getting a bunch of Pythonic suggestions which are easy to
> understand though not so efficient. If you're after efficiency you
> might look at a book like Welsh and Baczynskyj "Computer Chess II" for
> some techniques (warning, this is a rather old book though) and
> program in a closer-to-the-metal language. One common approach these
> days is "bit boards". Basically you represent the board state as a
> bunch of 64-bit words, one bit per square. So for checking occupancy,
> you'd have a word having the bits set for occupied squares. If you
> only want to check adjacent squares (king moves), you could have a
> bunch of bit masks (one for each square) with bits set only for the
> adjacent squares (similarly for bishop moves, rook moves, etc.) Then
> to check adjacency you'd mask off the appropriate bits for that
> square, then AND it against the occupancy word. Normally you'd have

In defense of the less efficient suggestions, he did say he had to use a
list of lists. But what you describe is pretty cool.

James

bearophileHUGS@lycos.com
Guest
Posts: n/a

 02-12-2007
James Stroud:
> import operator
> srch = [(i,j) for i in [-1,0,1] for j in [-1,0,1] if (i,j) != (0,0)]
> is_adj = reduce(operator.or_, [ary[row+i][col+j] for (i,j) in srch]])

Or maybe (untested), Python V.2.5:

srch = [(i,j) for i in [-1,0,1] for j in [-1,0,1] if (i,j) != (0,0)]
is_adj = any(ary[row+i][col+j] for (i,j) in srch)

Or:

is_adj = any(ary[row+i][col+j] for i in [-1,0,1] for j in [-1,0,1] if
(i,j) != (0,0))

Bye,
bearophile

John Machin
Guest
Posts: n/a

 02-12-2007
On Feb 12, 4:24 pm, Samuel Karl Peterson
<(E-Mail Removed)> wrote:
> C-like way to do it, (warning, untested in python):
>
> for i in range(:
> for j in range(:
> for offset_i in range(-1,2):
> for offset_j in range(-1, 2):
> row = i + offset_i
> col = j + offset_j
> if (row < 0 or row > 7) or (col < 0 or col > \
> or ((row,col) == (i,j)):
> continue
> # else do something with board[row][col]
>
> I realize this is gross and un-Pythonic and does the same thing

It's also just a little bit inefficient. Never mind the algorithm,
we'll talk about that later. Let's just improve the code a little:

side_range = range(
delta_range = range(-1, 2)
for i in side_range:
for offset_i in delta_range:
row = i + offset_i
if not (0 <= row <= 7): continue
for j in side_range:
for offset_j in delta_range:
if not offset_i and not offset_j: continue
col = j + offset_j
if not(0 <= col <= 7): continue
# *now* it's safe to do something
# with (row, col)

Such hoisting of code out of inner loops is done for you by a C
compiler -- Python assumes you know what you are doing

Now for the algorithm: all of that testing to see if you are about to
sail off the end of the world is a bit ugly and slow. You can use bit-
bashing, as Paul suggested, even though it's on Steven D'Aprano's list

Another approach that has been used is to have a 9 x 9 storage area;
the squares off the edge contain a value that is neither "empty" nor
any value that represents a piece. So with careful coding, they
automatically fail tests like "can I capture the piece on this
square" [no, it's not a piece] and "can I move here" [no, it's not
empty]. If the game is chess, you need 10 x 10 to cater for those
pesky knights.

Cheers,
John

Steven D'Aprano
Guest
Posts: n/a

 02-12-2007
On Sun, 11 Feb 2007 23:20:11 -0800, John Machin wrote:

> Now for the algorithm: all of that testing to see if you are about to
> sail off the end of the world is a bit ugly and slow. You can use bit-
> bashing, as Paul suggested, even though it's on Steven D'Aprano's list

Heh. Being a smart-alec is number 7

Seriously, this is Python. Are you *sure* bit-bashing is going to be
faster than the alternative? If this was C, or assembly, you'd almost
certainly be right. But Python is heavily object-oriented, and bit
manipulations are just methods, with all the overhead that entails.

>>> import timeit
>>> timeit.Timer("3 | 2", "").repeat()

[0.33678007125854492, 0.33447504043579102, 0.33331012725830078]
>>> timeit.Timer("3 < 2", "").repeat()

[0.30328893661499023, 0.29070115089416504, 0.28839397430419922]

The problem with bit-bashing, masking etc. is that except for the most
simple cases it is quite obfuscated. If you aren't going to gain a serious
performance boost, why bother?

--
Steven D'Aprano