pyzuki

chessTimer.py

Sep 12th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.95 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. #chessTimer.py
  4. """
  5. Can be used as a substitute for a real chess timer.
  6. Must be run at command line.
  7. """
  8.  
  9. import time
  10. import msvcrt
  11. import winsound
  12.  
  13. def getTimeLimit():
  14.     while True:
  15.         try:
  16.             limit = float(raw_input("Enter time limit in minutes: "))
  17.         except ValueError:
  18.             print "\nThat's not a number!"
  19.             continue
  20.         else:
  21.             break
  22.     return limit * 60
  23.  
  24. def secsToHMS(seconds):
  25.     """
  26.    Convert seconds to hours:minutes:seconds,
  27.    with seconds rounded to hundredths of a second.
  28.    """
  29.     if seconds < 0:
  30.         return "-%.2f secs" % (-1.0 * seconds)
  31.     minutes, seconds = divmod(seconds, 60)
  32.     hours, minutes = divmod(minutes, 60)
  33.     return "%02d:%02d:%05.2f" % (hours, minutes, seconds)
  34.  
  35. def beep():
  36.     """A very short Beep"""
  37.     pitch = 500
  38.     length = 30
  39.     winsound.Beep(pitch,length)
  40.    
  41. def beepN(times,interval):
  42.     """Calls beep() n times at interval of x seconds."""
  43.     for k in range(times-1):
  44.         beep()
  45.         time.sleep(interval)
  46.     beep()
  47.    
  48. def printInstructions():
  49.     print \
  50.     """
  51.    Press spacebar when player moves.
  52.    Press "c" at checkmate.
  53.    
  54.    There will be report every 5 seconds on the time remaining for each player, etc.
  55.    """
  56.    
  57. print
  58. printInstructions()
  59. print "Set the time limit for each player"
  60. timeLimit = getTimeLimit()
  61.  
  62. raw_input("Press Enter to start game")
  63. print
  64. print "Game started at", time.strftime('%H:%M:%S')
  65. print "White to move"
  66.  
  67. timeStart = time.time()
  68. timeAtMoveStart = timeStart
  69. remainingWhiteTime = timeLimit
  70. remainingBlackTime = timeLimit
  71. whiteMoveCounter = 1
  72. blackMoveCounter = 1
  73. timeNow = timeStart
  74. oldTimeNow = timeStart
  75. NAME = 0
  76. MOVES = 1
  77. TIME = 2
  78. whitePlayer = ['White', whiteMoveCounter, remainingWhiteTime]
  79. blackPlayer = ['Black', blackMoveCounter, remainingBlackTime]
  80. currentPlayer = whitePlayer
  81. otherPlayer = blackPlayer
  82.  
  83. while True:
  84.     if msvcrt.kbhit():
  85.         key = msvcrt.getch()
  86.        
  87.         # When a move is made
  88.         if key == " ":
  89.             print
  90.             markTime = time.time()
  91.             timeUsedThisMove = markTime - timeAtMoveStart
  92.             print "%s moved" % currentPlayer[NAME]
  93.             print "Time used for move %d by %s was %s" % (
  94.              currentPlayer[MOVES], currentPlayer[NAME], secsToHMS(timeUsedThisMove))
  95.             currentPlayer[TIME] -= timeUsedThisMove
  96.             print "%s's remaining time is %s" % (
  97.               currentPlayer[NAME], secsToHMS(currentPlayer[TIME]))
  98.             print "%s's remaining time is %s" % (
  99.               otherPlayer[NAME], secsToHMS(otherPlayer[TIME]))
  100.             currentPlayer[MOVES] += 1
  101.             print "%s to make move %d" % (otherPlayer[NAME], otherPlayer[MOVES])
  102.             currentPlayer, otherPlayer = otherPlayer, currentPlayer
  103.             timeAtMoveStart = markTime
  104.             oldTimeNow = timeNow
  105.            
  106.         # At checkmate
  107.         elif key == "c":
  108.             print
  109.             print "%s wins by CHECKMATE with move %d!" % (
  110.               currentPlayer[NAME], currentPlayer[MOVES])
  111.             timeUsedThisMoveSoFar = timeNow - timeAtMoveStart
  112.             print "%s's remaining time was %s" % (
  113.               currentPlayer[NAME], secsToHMS(currentPlayer[TIME] - timeUsedThisMoveSoFar))
  114.             print "%s's remaining time was %s" % (
  115.               otherPlayer[NAME], secsToHMS(otherPlayer[TIME]))
  116.             break
  117.        
  118.     # Make some time for other processes on computer
  119.     else:
  120.         time.sleep(0.001)
  121.        
  122.     timeNow = time.time()
  123.     timeUsedThisMoveSoFar = timeNow - timeAtMoveStart
  124.    
  125.     # Print report approx. every 5 seconds
  126.     if timeNow >= oldTimeNow + 4.99:
  127.         print
  128.         print "Time used by %s so far this move is %s" % (
  129.           currentPlayer[NAME], secsToHMS(timeUsedThisMoveSoFar))
  130.         print "%s's remaining time is %s" % (
  131.           currentPlayer[NAME], secsToHMS(
  132.           currentPlayer[TIME] - timeUsedThisMoveSoFar))
  133.         print "%s's remaining time is %s" % (
  134.           otherPlayer[NAME], secsToHMS(otherPlayer[TIME]))
  135.         print "It's still %s's move %d" % (
  136.           currentPlayer[NAME], currentPlayer[MOVES])
  137.         oldTimeNow = timeNow
  138.        
  139.     # Check if a player has reached the time limit
  140.     if currentPlayer[TIME] <= timeUsedThisMoveSoFar:
  141.         print
  142.         print "TIME'S UP for %s" % currentPlayer[NAME]
  143.         print "%s WINS!" % otherPlayer[NAME]
  144.         print "%s's remaining time was %s" % (
  145.           otherPlayer[NAME], secsToHMS(otherPlayer[TIME]))
  146.         print "%s made %d moves" % (
  147.           otherPlayer[NAME], (otherPlayer[MOVES]-1))
  148.         print "%s made %d moves before his time expired" % (
  149.           currentPlayer[NAME], (currentPlayer[MOVES]-1))
  150.         beepN(3, .2)
  151.         break
  152. print
  153. raw_input("Hit Enter to close ")
Advertisement
Add Comment
Please, Sign In to add comment