OO style advice

Sean Ross sross at connectmail.carleton.ca
Mon Feb 16 13:42:42 EST 2004


"DomF" <fidtz at clara#spam#.co.uk> wrote in message
news:1076943278.22567.0 at echo.uk.clara.net...
> I have been putting together a little text chess program and have run into
a
> style issue when integrating my chess clock with my chessboard generator.
> Basically, I would like to be able to manipulate the clock independently
of
> the board but also want the clock time as displayed by the board to change
> automatically.

Hi.
My first thought is to make a Clock object that has a time attribute and
start() and stop() methods

Then have one instance of Clock for each player (and perhaps an independent
one for the board?)

If you have Player objects, perhaps they could have a clock instance. In
that way
you could do something like:

# suppose the players are maintained by the board in a list
# and you have a turn variable for accessing the player whose turn it is to
play
while board.state != STALEMATE and board.state != CHECKMATE:
    player = player[turn]
    player.clock.start()
    player.move()
    player.clock.stop()
    turn.next()   # adjust turn index to let other player move next round


you could even have player.clock.start() and player.clock.stop() at the
start
and end of the inside of player.move() so your code looks more like this

while board.state != STALEMATE and board.state != CHECKMATE:
    player[turn].move()
    turn.next()


Just an idea,
Sean





More information about the Python-list mailing list