persei7


Joined 1 year ago
Homeworks submitted:
Homework comments:
1
0

About Me

No description provided.

Classes

Introduction to Economic Analysis

Class status: Under Construction
Role: Student
. 0% complete

Reddit Learns Programming

Class status: Under Construction
Role: Student
. 0% complete

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

Class status: Established
Role: Student
. 5% complete

Submitted Assignments

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 9, HW 2

My implementation:

# Problem Set 5: Ghost
# Name: 
# Collaborators: 
# Time: 
#

import random
import string

# -----------------------------------
# Helper code
# (you don't need to understand this helper code)
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.
    """
    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())
    print "  ", len(wordlist), "words loaded."
    inFile.close()
    return wordlist

def ghost(wordlist,player):

    gameover = False
    i = 0
    word_frag = ""

    print "\n \nWelcome to Ghost!"

    while not gameover:
        
        new_wordlist = []
        if i > 0: print "\nThe current word frament is", word_frag.upper()
        else: print

        no_letter = True
        while no_letter:
            letter = raw_input("%s. Please enter your letter: " %player[i%2])

            if letter not in string.ascii_letters or len(letter) != 1:
                print "Please enter a single letter!"
            else:
                no_letter = False
        
        word_frag += letter
        
        for word in wordlist:
                if i < len(word) and word[i] == letter.lower():
                        new_wordlist.append(word)

        if len(new_wordlist) == 0:
            gameover = True
            print "\nGAME OVER LOSER! %s loses because no word begins with %s." %(player[i%2],word_frag)
        elif word_frag in new_wordlist and len(word_frag) > 3:
            gameover = True
            print "\nGAME OVER LOSER! %s loses because %s is a word." %(player[i%2],word_frag)
        else:
            wordlist = new_wordlist
            i += 1
##            print wordlist[:10]

    return
def play_game():

    wordlist = load_words()
    player = [None]*2

    print
    
    for a in range(2):
        player[a] = raw_input("PLAYER %i: Please enter your name: " %(a+1))

    while True:
        cmd = raw_input('Enter n to play a new game or e to end game: ')
        if cmd == 'n':
            ghost(wordlist,player)
            print
        elif cmd == 'e':
            break
        else:
            print "Invalid command."

play_game()   

persei7 1 year ago