Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #coding=utf-8
- #chessTimer.py
- """
- Can be used as a substitute for a real chess timer.
- Must be run at command line.
- """
- import time
- import msvcrt
- import winsound
- def getTimeLimit():
- while True:
- try:
- limit = float(raw_input("Enter time limit in minutes: "))
- except ValueError:
- print "\nThat's not a number!"
- continue
- else:
- break
- return limit * 60
- def secsToHMS(seconds):
- """
- Convert seconds to hours:minutes:seconds,
- with seconds rounded to hundredths of a second.
- """
- if seconds < 0:
- return "-%.2f secs" % (-1.0 * seconds)
- minutes, seconds = divmod(seconds, 60)
- hours, minutes = divmod(minutes, 60)
- return "%02d:%02d:%05.2f" % (hours, minutes, seconds)
- def beep():
- """A very short Beep"""
- pitch = 500
- length = 30
- winsound.Beep(pitch,length)
- def beepN(times,interval):
- """Calls beep() n times at interval of x seconds."""
- for k in range(times-1):
- beep()
- time.sleep(interval)
- beep()
- def printInstructions():
- print \
- """
- Press spacebar when player moves.
- Press "c" at checkmate.
- There will be report every 5 seconds on the time remaining for each player, etc.
- """
- print
- printInstructions()
- print "Set the time limit for each player"
- timeLimit = getTimeLimit()
- raw_input("Press Enter to start game")
- print
- print "Game started at", time.strftime('%H:%M:%S')
- print "White to move"
- timeStart = time.time()
- timeAtMoveStart = timeStart
- remainingWhiteTime = timeLimit
- remainingBlackTime = timeLimit
- whiteMoveCounter = 1
- blackMoveCounter = 1
- timeNow = timeStart
- oldTimeNow = timeStart
- NAME = 0
- MOVES = 1
- TIME = 2
- whitePlayer = ['White', whiteMoveCounter, remainingWhiteTime]
- blackPlayer = ['Black', blackMoveCounter, remainingBlackTime]
- currentPlayer = whitePlayer
- otherPlayer = blackPlayer
- while True:
- if msvcrt.kbhit():
- key = msvcrt.getch()
- # When a move is made
- if key == " ":
- print
- markTime = time.time()
- timeUsedThisMove = markTime - timeAtMoveStart
- print "%s moved" % currentPlayer[NAME]
- print "Time used for move %d by %s was %s" % (
- currentPlayer[MOVES], currentPlayer[NAME], secsToHMS(timeUsedThisMove))
- currentPlayer[TIME] -= timeUsedThisMove
- print "%s's remaining time is %s" % (
- currentPlayer[NAME], secsToHMS(currentPlayer[TIME]))
- print "%s's remaining time is %s" % (
- otherPlayer[NAME], secsToHMS(otherPlayer[TIME]))
- currentPlayer[MOVES] += 1
- print "%s to make move %d" % (otherPlayer[NAME], otherPlayer[MOVES])
- currentPlayer, otherPlayer = otherPlayer, currentPlayer
- timeAtMoveStart = markTime
- oldTimeNow = timeNow
- # At checkmate
- elif key == "c":
- print
- print "%s wins by CHECKMATE with move %d!" % (
- currentPlayer[NAME], currentPlayer[MOVES])
- timeUsedThisMoveSoFar = timeNow - timeAtMoveStart
- print "%s's remaining time was %s" % (
- currentPlayer[NAME], secsToHMS(currentPlayer[TIME] - timeUsedThisMoveSoFar))
- print "%s's remaining time was %s" % (
- otherPlayer[NAME], secsToHMS(otherPlayer[TIME]))
- break
- # Make some time for other processes on computer
- else:
- time.sleep(0.001)
- timeNow = time.time()
- timeUsedThisMoveSoFar = timeNow - timeAtMoveStart
- # Print report approx. every 5 seconds
- if timeNow >= oldTimeNow + 4.99:
- print
- print "Time used by %s so far this move is %s" % (
- currentPlayer[NAME], secsToHMS(timeUsedThisMoveSoFar))
- print "%s's remaining time is %s" % (
- currentPlayer[NAME], secsToHMS(
- currentPlayer[TIME] - timeUsedThisMoveSoFar))
- print "%s's remaining time is %s" % (
- otherPlayer[NAME], secsToHMS(otherPlayer[TIME]))
- print "It's still %s's move %d" % (
- currentPlayer[NAME], currentPlayer[MOVES])
- oldTimeNow = timeNow
- # Check if a player has reached the time limit
- if currentPlayer[TIME] <= timeUsedThisMoveSoFar:
- print
- print "TIME'S UP for %s" % currentPlayer[NAME]
- print "%s WINS!" % otherPlayer[NAME]
- print "%s's remaining time was %s" % (
- otherPlayer[NAME], secsToHMS(otherPlayer[TIME]))
- print "%s made %d moves" % (
- otherPlayer[NAME], (otherPlayer[MOVES]-1))
- print "%s made %d moves before his time expired" % (
- currentPlayer[NAME], (currentPlayer[MOVES]-1))
- beepN(3, .2)
- break
- print
- raw_input("Hit Enter to close ")
Advertisement
Add Comment
Please, Sign In to add comment