James Stroud <[EMAIL PROTECTED]> 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(8):
    for j in range(8):
        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 > 8) \
                   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
"if programmers were paid to remove code instead of adding it,
software would be much better" -- unknown
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to