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

Dick Moores rdm at rcblue.com
Wed Oct 31 18:54:24 CET 2007


At 03:49 AM 10/30/2007, Kent Johnson wrote:
>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]

Done, I think. Seems to be a big improvement. 
<http://www.rcblue.com/Python/chessTimerForWebV12.py>

You'll notice I've given up for the time being on trying to have it 
useable by unix-ers. This meant that I could use winsound again to 
get 3 short beeps when a player reaches his time limit and loses.

> 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.

Well, Kent (and Bob Gailer), thanks for trying to help with classes, 
but I still don't get them. I've just got to spend time with using 
classes in much simpler scripts. You guys may think my chessTimer is 
a piece of cake, but it stretched me a lot even without classes.

>>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?

I answered this earlier, in my post of Tue, 30 Oct 2007 22:25:45 -0700 .

Dick 



More information about the Tutor mailing list