At 06:38 PM 10/29/2007, Kent Johnson wrote:
Dick Moores wrote:
1. Is wrapping the long lines where I have done so OK?

I usually indent the second line of a wrapped line so it stands out more.

Done in newest version 10 < http://www.rcblue.com/Python/chessTimerForWebV10.py>

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]

playerName[i]
moveCounter[i]
remainingPlayerTime[i]

Where i  = 1, or i = -1
Because White moves first, a line near the top would initialize i by
i = 1

Then when I want to switch to deal with Black I'd first have the line
i *= -1

Then for White again, the same line again
i *= -1

Is that something like what you were thinking? Or was it to have just one six-element list,
playerStuff = [ 'White', 'Black', whiteMoveCounter, blackMoveCounter, remainingWhiteTime, remainingBlackTime]
and a function that would pick out the correct one from each of the 3 pairs. All White's stuff has an even index, and all Black's stuff has an odd index, so I suppose I could work with that. Or perhaps a list of 3-element lists?

4. Should more of the code be in functions?

That might make it read more easily. Alternately some blank lines to break the code into sections would aid readability.

I'll go with the latter, if it's just as good. < http://www.rcblue.com/Python/chessTimerForWebV10.py>

5. Is there a way to collapse lines 130-137?:
if player == 1: # player is White
                 whiteMoveCounter += 1
                 print "Black to make move %d" % (blackMoveCounter)
                 remainingWhiteTime -= timeUsedThisMove
elif player == -1: # player is Black
                 blackMoveCounter += 1
                 print "White to make move %d" % (whiteMoveCounter)
                 remainingBlackTime -= timeUsedThisMove

If the players were objects then this might look like

currentPlayer.moveCounter += 1
otherPlayer.printNextMove()
currentPlayer.remainingTime -= timeUsedThisMove

Switching players would be done with
currentPlayer, otherPlayer = otherPlayer, currentPlayer

I'll have to think on that.

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():'  (line 116).  I hope I'm wrong about
this, and someone can show me how to fix it so that the unix people
can give me some criticism/advice as well."

Also, in the script I first asked about when I started this thread, < http://www.rcblue.com/Python/chessTimerForWebV6.py>, I used a couple of functions of mine that employ winsound.Beep() to beep when a player's time is up. I suppose there is something like winsound.Beep() in unix. Is there a way to build it in so that both Windows and unix users can have a beep?

BTW I already received advice from Gabriel Genellina to use time.sleep(0.1) to 'to be nice to other running processes". The way the script < http://www.rcblue.com/Python/chessTimerForWebV9.py> is written, my computer's CPU is running 100%. I saw that even with a sleep(0.001) I can barely tell the script is running by looking at CPU usage. So this is now in the new version as lines 157-158

else:
     time.sleep(0.001)

as well as the new indentations and blank lines you recommended above: < http://www.rcblue.com/Python/chessTimerForWebV10.py>

Thanks, Kent.

Dick

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to