Werden wir Helden für einen Tag

Home | About | Archive

Stupid computer game #2

Posted on Mar 14, 2011 by Chung-hong Chan

An implementation of ghost

MIT 6.00 homework.

You may download the dictionary from here,


#!/usr/bin/python
# Problem Set 5: Ghost
# Name: CH Chan
# Collaborators: Nil 
# Time: An afternoon
#

import random
import string

WORDLIST_FILENAME = "words.txt"

def load_words():
    """
    Returns a list of valid words. Words are strings of lowercase letters.
    
    Depending on the size of the word list, this function may
    take a while to finish.
    """
    #STFU print "Loading word list from file..."
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r', 0)
    # wordlist: list of strings
    wordlist = []
    for line in inFile:
        wordlist.append(line.strip().lower())
    # STFU print "  ", len(wordlist), "words loaded."
    return wordlist

def isValidWord(wordlist, inputWord):
    ''' Test for the validity for the word in GhosT
    return 0 for OK, return 1 for word, return 2 for broken'''
    #test 1: inputWord in wordlist with length < = 3
    if inputWord in wordlist:
        if len(inputWord) <=3:
            return 0
        else:
            return 1
    else:
    #test #2: inputWord is a prefix
        anymatch = 0
        for word in wordlist:
            if word.startswith(inputWord): #test #2: inputWord is a prefix
                anymatch +=1
        if anymatch == 0:
            return 2
        else:
            return 0

def inputValidator():
    keepOn = True
    while keepOn:
        userInput = raw_input()
        if len(userInput) > 1:
            print 'Too long, please input exactly one character.'
        elif userInput not in string.ascii_letters:
            print 'Invalid input, please input exactly one character.'
        else:
            keepOn=False
    #process userInput
    lowerUserInput = userInput.lower()
    return lowerUserInput

# can change the number of players
NUM_PLAYERS = 2
STARTING_PLAYER = 1
def ghost(wordlist):
    print 'Welcome to Ghost!'
    loser = 0
    currentPlayer = STARTING_PLAYER
    fragment = ''
    print 'Player '+str(currentPlayer)+' goes first...'
    while loser == 0:
        print 'Player '+str(currentPlayer)+' says letter:'
        userInput = inputValidator()
        fragment += userInput
        validFragment = isValidWord(wordlist,fragment)
        if validFragment == 1:
            print 'Current word fraqment: '+str(fragment.upper())
            loser=currentPlayer
            warning='Player '+str(loser)+' loses because '+str(fragment.upper())+' is a word!'
        if validFragment == 2:
            print 'Current word fraqment: '+str(fragment.upper())
            loser=currentPlayer
            warning='Player '+str(loser)+' loses because no word begins with '+str(fragment.upper())
        if validFragment == 0:
            currentPlayer += 1
            print 'Current word fraqment: '+str(fragment.upper())
            if currentPlayer > NUM_PLAYERS:
                currentPlayer = STARTING_PLAYER
    print warning
    # declare winners
    allPlayer = range(1,NUM_PLAYERS+1)
    allPlayer.remove(loser)
    for winner in allPlayer:
        print 'Player '+str(winner)+' wins!'

#main loop

if __name__ == '__main__':
    wordlist = load_words()
    ghost(wordlist)

Powered by Jekyll and profdr theme