[Tutor] Re: Timing a loop: How to interrupt ?

Sheila King sheila@thinkspot.net
Sun, 13 May 2001 21:46:46 -0700


On Sun, 13 May 2001 21:37:04 -0700, Sheila King <sheila@thinkspot.net>  wrote
about Timing a loop: How to interrupt ?:

:I'm writing a portion of a Boggle game for my students who are learning Python
:right now. I'm going to let them extend it and finish it up. Right now, it just
:rolls up the 16 dice and places them randomly in the tray, displays the tray and
:lets you type in words that you see, until the time is over two minutes.
:
:[There is currently no dictionary checking, no scoring, etc... I'm leaving that
:for the students.]
:
:I've included my script below.

Oh, uh, I forgot to include the code. Duh.

See below.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/

# So far, this game rolls the sixteen dice, and places them in
# random locations in the game board, and displays the game board.
# it times for two minutes of display, and then prints "GAME OVER"

import time
import random

diceList = [ ('A', 'A', 'C', 'O', 'T', 'I'), \
            ('M', 'A', 'S', 'O', 'H', 'R'), \
            ('Y', 'H', 'I', 'F', 'E', 'E'), \
            ('V', 'A', 'N', 'D', 'E', 'Z'), \
            ('A', 'Y', 'B', 'T', 'L', 'I'), \
            ('B', 'I', 'R', 'O', 'F', 'X'), \
            ('A', 'P', 'D', 'C', 'M', 'E'), \
            ('P', 'I', 'S', 'E', 'H', 'N'), \
            ('R', 'U', 'W', 'I', 'L', 'G'), \
            ('U', 'K', 'G', 'E', 'L', 'Y'), \
            ('I', 'V', 'G', 'N', 'T', 'E'), \
            ('D', 'E', 'W', 'O', 'N', 'S'), \
            ('C', 'L', 'A', 'R', 'E', 'S'), \
            ('L', 'E', 'T', 'S', 'U', 'P'), \
            ('J', 'O', 'M', 'A', 'B', 'QU'), \
            ('D', 'T', 'O', 'K', 'N', 'U')]

class BoggleGame:

    def __init__(self):
        self.board = {}
        diceCount = 0
        for row in range(1,5):
            for col in range(1,5):
                self.board[(row,col)]= None
        for loc in range(0, 16):
            while (1):
                row = random.randrange(1, 5)
                col = random.randrange(1, 5)
                if self.board[(row, col)] == None:
                    break
            letter = random.choice(diceList[loc])
            self.board[(row, col)] = letter

    def chooseRandomLoc(self):
        rows = (1, 2, 3, 4)
        return (random.choice(rows), random.choice(rows))

    def displayBoard(self):
        print # start board display on a newline
        for row in range(1,5):
            for col in range(1,5):
                print self.board[(row,col)], " ",  # extra ending comma
                                                   # prevents newline
            print # empty line print

if __name__ == '__main__':
    testGame = BoggleGame()
    submittedwordlist = []
    print "The two minute time begins NOW!\n"
    starttime = time.clock()
    testGame.displayBoard()
    while (time.clock() - starttime < 2* 60):
        submittedwordlist.append(raw_input("Enter words:\n"))
    print
    print "Time's Up."
    print "Here are the words you entered:\n"
    for word in submittedwordlist:
        print word