[Tutor] Looking for suggestions for improving chessTimer.py code

Kent Johnson kent37 at tds.net
Tue Oct 30 11:49:07 CET 2007


Dick Moores wrote:

>>> 2. I've used 1 for White player, -1 for Black player, and (-1)*player
>>> to alternate players. Is there a better way?
>>> 3. I've used a lot of variables. Too Many?
>>
>> I think it might work well to either put the player variables into 
>> lists or simple classes.
> 
> I have no confidence with classes, but how about this for lists:
> 
> playerName = ['', 'White', 'Black']
> moveCounter = [0, whiteMoveCounter, blackMoveCounter}
> remainingPlayerTime = [0, remainingWhiteTime, remainingBlackTime]

> Is that something like what you were thinking?

No. I was thinking more like this:
whitePlayer = [ 'White', whiteMoveCounter, remainingWhiteTime]
blackPlayer = [ 'Black', blackMoveCounter, remainingBlackTime]

You could even define offsets:
NAME = 0
MOVES = 1
TIME = 2

and refer to
currentPlayer[NAME]

 From here to simple classes is not a big step:

class player(object):
   def __init__(self, name, moveCounter, remainingTime):
     self.name = name
     self.moveCounter = moveCounter
     self.remainingTime = remainingTime

whitePlayer = player('White', 1, timeLimit)
blackPlayer = player('Black', 1, timeLimit)

Then playerName(player) becomes player.name and 
remainingPlayerTime(player) becomes player.remainingTime.

> Any thoughts about my number 6?: "I thought I had a way to make this 
> script useable on unix as well
> as Windows. Thus the section with the 3 classes. But it won't run on
> unix, because it was necessary to import msvcrt outside of the
> classes--it wouldn't compile otherwise, and because of the need for
> the line  'if msvcrt.kbhit():'

Why do you need to call kbhit() at all? Why not just call getch() and 
wait for the next character?

Kent


More information about the Tutor mailing list