Advertisement
vaboro

ps5_ghost.py

Apr 28th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.17 KB | None | 0 0
  1. # Problem Set 5: Ghost
  2. # Name: vaboro
  3. # Collaborators: none
  4. # Time: approximately 5-8 hours
  5. #
  6.  
  7. import random
  8.  
  9. # -----------------------------------
  10. # Helper code
  11. # (you don't need to understand this helper code)
  12. import string
  13.  
  14. WORDLIST_FILENAME = "words.txt"
  15. MIN_WORD_LENGTH = 3 # this variable shows how many letters the shortest word is allowed in Ghost
  16.  
  17. def load_words():
  18.     """
  19.    Returns a list of valid words. Words are strings of lowercase letters.
  20.    
  21.    Depending on the size of the word list, this function may
  22.    take a while to finish.
  23.    """
  24.     print "Loading word list from file..."
  25.     # inFile: file
  26.     inFile = open(WORDLIST_FILENAME, 'r', 0)
  27.     # wordlist: list of strings
  28.     wordlist = []
  29.     for line in inFile:
  30.         wordlist.append(line.strip().lower())
  31.     print "  ", len(wordlist), "words loaded."
  32.     return wordlist
  33.  
  34. def get_frequency_dict(sequence):
  35.     """
  36.    Returns a dictionary where the keys are elements of the sequence
  37.    and the values are integer counts, for the number of times that
  38.    an element is repeated in the sequence.
  39.  
  40.    sequence: string or list
  41.    return: dictionary
  42.    """
  43.     # freqs: dictionary (element_type -> int)
  44.     freq = {}
  45.     for x in sequence:
  46.         freq[x] = freq.get(x,0) + 1
  47.     return freq
  48.  
  49.  
  50. # (end of helper code)
  51. # -----------------------------------
  52.  
  53. # Actually load the dictionary of words and point to it with
  54. # the wordlist variable so that it can be accessed from anywhere
  55. # in the program.
  56.  
  57. wordlist = load_words()
  58.  
  59. # TO DO: your code begins here!
  60. #
  61. # There are two ways to lose Ghost:
  62. # 1) To form a word longer than 3 letters
  63. # 2) To create a fragment (of any size) which cannot become a word by adding more letters
  64. # The second condition raises the issue of checking if the fragment has a valid
  65. # extension and can become a word. I can form a dictionary for each word in 'wordlist' but
  66. # this will not guarantee that two dictionaries will correspond to the same
  67. # words, or I can create another wordlist from the original dictionary each time a letter is
  68. # added by the next player. If combination of letters is not a word and is in the new dictionary
  69. # then this combination can form a word. If combination of letters is a word then the player
  70. # loses. If combination of letters is not a word and is not in the new dictionary then the
  71. # player loses.
  72. #
  73.      
  74. def is_passed(fragment):
  75.     """
  76.    Returns True if player entered a word no longer than MIN_WORD_LENGTH letters or created
  77.    a fragment that can become a word by adding more letters; otherwise, if player entered
  78.    a word longer than MIN_WORD_LENGTH or created a fragment that cannot become a word
  79.    by adding more letter, returns False. True means the player passes the turn, False means
  80.    the player loses the game.
  81.  
  82.    fragment: string
  83.    returns: tuple -> boolean, string
  84.    """
  85.  
  86.     number_of_letters = len(fragment)
  87.  
  88.     player_wordlist = []
  89.    
  90.     for word in wordlist:
  91.         player_wordlist.append(word[:number_of_letters])
  92.  
  93.     if fragment in player_wordlist: # the fragment can become a word by adding more letters
  94.         if fragment in wordlist: # the fragment is a word!
  95.             if number_of_letters <= MIN_WORD_LENGTH:
  96.                 message = 'is a word but no longer than ' + str(MIN_WORD_LENGTH) + ' letters!'
  97.                 return True, message # player created a valid word less than or equal 3 letters long and passed the turn
  98.             else:
  99.                 message = "'" + fragment + "'" + ' is a word!'
  100.                 return False, message # player created a valid word more than 3 letters long and lost the game
  101.         else:
  102.             message = 'can become a word by adding more letters.'
  103.             return True, message # the fragment can become a word by adding more letters
  104.     else:
  105.         message = "no word begins with " + "'" + fragment + "'" + "!"
  106.         return False, message # the fragment cannot become a word by adding more letters
  107.  
  108. def play_turn(fragment, player):
  109.     '''
  110.    Returns True if 'player' passes the turn and False if 'player' loses the game.
  111.  
  112.    player -> string
  113.    fragment -> string
  114.    returns: tuple -> boolean, string, string
  115.    '''
  116.     letter = ' '
  117.  
  118.     print "Current word fragment: '" + fragment + "'"
  119.     while letter not in string.ascii_letters:
  120.         letter = raw_input(player + ' says letter: ' ).lower()
  121.         print
  122.         if letter not in string.ascii_letters:
  123.             print "'" + letter + "' is not an alphabetic character. Please try again."
  124.     current_fragment = fragment + letter
  125.     turn = is_passed(current_fragment)
  126.     if not turn[0]:
  127.         print player + " loses because " + turn[1]
  128.         return False, player, current_fragment
  129.     else:
  130.         if player == 'Player 1':
  131.             player = 'Player 2'
  132.         else:
  133.             player = 'Player 1'
  134.         return True, player, current_fragment
  135.  
  136. def play_ghost():
  137.  
  138.     next_turn = True
  139.  
  140.     player = 'Player 1'
  141.        
  142.     fragment = ''
  143.  
  144.     print 'Welcome to Ghost!'
  145.     print 'Player 1 goes first.'
  146.     print
  147.    
  148.     while next_turn:
  149.         next_turn, player, fragment = play_turn(fragment, player)
  150.  
  151. play_ghost()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement