[Tutor] checking for a condition across all members of a list
simultaneously
Pijus Virketis
virketis@fas.harvard.edu
Thu, 10 May 2001 17:00:43 -0400
Hi!
I am writing a little crosses-and-naughts game. At its heart is a n*m
matrix of Cells, instances of a class that represents the states of a
single cell (i.e. "-", "x" and "o"). I can set_state() and return_state().
Now I need to implement victory checks. So, for instance, the horizontal
check would in principle go something like this:
def check_horizontal(Matrix):
for row in Matrix:
if row[0].return_state() == row[1].return_state() == row[2].return_state():
print "victory!"
Except that I, perhaps out of stubbornness, really don't want to build in
the dimensions of my matrix into the code (what if someone wants to try out
a 5*5 x-o game? :)). What's a nice way of going across the whole row (or
down a column, in the next step)?
Thanks!
Pijus
----------------------------------------------------------------------------
-------------------
Please have a look at my weblog at www.fas.harvard.edu/~virketis.