Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Problem Set 5: Ghost
- # Name: vaboro
- # Collaborators: none
- # Time: approximately 5-8 hours
- #
- import random
- # -----------------------------------
- # Helper code
- # (you don't need to understand this helper code)
- import string
- WORDLIST_FILENAME = "words.txt"
- MIN_WORD_LENGTH = 3 # this variable shows how many letters the shortest word is allowed in Ghost
- 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."
- return wordlist
- def get_frequency_dict(sequence):
- """
- Returns a dictionary where the keys are elements of the sequence
- and the values are integer counts, for the number of times that
- an element is repeated in the sequence.
- sequence: string or list
- return: dictionary
- """
- # freqs: dictionary (element_type -> int)
- freq = {}
- for x in sequence:
- freq[x] = freq.get(x,0) + 1
- return freq
- # (end of helper code)
- # -----------------------------------
- # Actually load the dictionary of words and point to it with
- # the wordlist variable so that it can be accessed from anywhere
- # in the program.
- wordlist = load_words()
- # TO DO: your code begins here!
- #
- # There are two ways to lose Ghost:
- # 1) To form a word longer than 3 letters
- # 2) To create a fragment (of any size) which cannot become a word by adding more letters
- # The second condition raises the issue of checking if the fragment has a valid
- # extension and can become a word. I can form a dictionary for each word in 'wordlist' but
- # this will not guarantee that two dictionaries will correspond to the same
- # words, or I can create another wordlist from the original dictionary each time a letter is
- # added by the next player. If combination of letters is not a word and is in the new dictionary
- # then this combination can form a word. If combination of letters is a word then the player
- # loses. If combination of letters is not a word and is not in the new dictionary then the
- # player loses.
- #
- def is_passed(fragment):
- """
- Returns True if player entered a word no longer than MIN_WORD_LENGTH letters or created
- a fragment that can become a word by adding more letters; otherwise, if player entered
- a word longer than MIN_WORD_LENGTH or created a fragment that cannot become a word
- by adding more letter, returns False. True means the player passes the turn, False means
- the player loses the game.
- fragment: string
- returns: tuple -> boolean, string
- """
- number_of_letters = len(fragment)
- player_wordlist = []
- for word in wordlist:
- player_wordlist.append(word[:number_of_letters])
- if fragment in player_wordlist: # the fragment can become a word by adding more letters
- if fragment in wordlist: # the fragment is a word!
- if number_of_letters <= MIN_WORD_LENGTH:
- message = 'is a word but no longer than ' + str(MIN_WORD_LENGTH) + ' letters!'
- return True, message # player created a valid word less than or equal 3 letters long and passed the turn
- else:
- message = "'" + fragment + "'" + ' is a word!'
- return False, message # player created a valid word more than 3 letters long and lost the game
- else:
- message = 'can become a word by adding more letters.'
- return True, message # the fragment can become a word by adding more letters
- else:
- message = "no word begins with " + "'" + fragment + "'" + "!"
- return False, message # the fragment cannot become a word by adding more letters
- def play_turn(fragment, player):
- '''
- Returns True if 'player' passes the turn and False if 'player' loses the game.
- player -> string
- fragment -> string
- returns: tuple -> boolean, string, string
- '''
- letter = ' '
- print "Current word fragment: '" + fragment + "'"
- while letter not in string.ascii_letters:
- letter = raw_input(player + ' says letter: ' ).lower()
- print
- if letter not in string.ascii_letters:
- print "'" + letter + "' is not an alphabetic character. Please try again."
- current_fragment = fragment + letter
- turn = is_passed(current_fragment)
- if not turn[0]:
- print player + " loses because " + turn[1]
- return False, player, current_fragment
- else:
- if player == 'Player 1':
- player = 'Player 2'
- else:
- player = 'Player 1'
- return True, player, current_fragment
- def play_ghost():
- next_turn = True
- player = 'Player 1'
- fragment = ''
- print 'Welcome to Ghost!'
- print 'Player 1 goes first.'
- print
- while next_turn:
- next_turn, player, fragment = play_turn(fragment, player)
- play_ghost()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement