Advertisement
zSezn

Untitled

Feb 11th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. # 6.00x Problem Set 4A Template
  2. #
  3. # The 6.00 Word Game
  4. # Created by: Kevin Luu <luuk> and Jenna Wiens <jwiens>
  5. # Modified by: Sarina Canelake <sarina>
  6. #
  7.  
  8. import random
  9. import string
  10.  
  11. VOWELS = 'aeiou'
  12. CONSONANTS = 'bcdfghjklmnpqrstvwxyz'
  13. HAND_SIZE = 7
  14.  
  15. SCRABBLE_LETTER_VALUES = {
  16. 'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10
  17. }
  18.  
  19. # -----------------------------------
  20. # Helper code
  21. # (you don't need to understand this helper code)
  22.  
  23. WORDLIST_FILENAME = "words.txt"
  24.  
  25. def loadWords():
  26. """
  27. Returns a list of valid words. Words are strings of lowercase letters.
  28.  
  29. Depending on the size of the word list, this function may
  30. take a while to finish.
  31. """
  32. print "Loading word list from file..."
  33. # inFile: file
  34. inFile = open(WORDLIST_FILENAME, 'r', 0)
  35. # wordList: list of strings
  36. wordList = []
  37. for line in inFile:
  38. wordList.append(line.strip().lower())
  39. print " ", len(wordList), "words loaded."
  40. return wordList
  41.  
  42. def getFrequencyDict(sequence):
  43. """
  44. Returns a dictionary where the keys are elements of the sequence
  45. and the values are integer counts, for the number of times that
  46. an element is repeated in the sequence.
  47.  
  48. sequence: string or list
  49. return: dictionary
  50. """
  51. # freqs: dictionary (element_type -> int)
  52. freq = {}
  53. for x in sequence:
  54. freq[x] = freq.get(x,0) + 1
  55. return freq
  56.  
  57.  
  58. # (end of helper code)
  59. # -----------------------------------
  60.  
  61. #
  62. # Problem #1: Scoring a word
  63. #
  64. def getWordScore(word, n):
  65. """
  66. Returns the score for a word. Assumes the word is a valid word.
  67.  
  68. The score for a word is the sum of the points for letters in the
  69. word, multiplied by the length of the word, PLUS 50 points if all n
  70. letters are used on the first turn.
  71.  
  72. Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is
  73. worth 3, D is worth 2, E is worth 1, and so on (see SCRABBLE_LETTER_VALUES)
  74.  
  75. word: string (lowercase letters)
  76. n: integer (HAND_SIZE; i.e., hand size required for additional points)
  77. returns: int >= 0
  78. """
  79.  
  80.  
  81.  
  82. #
  83. # Problem #2: Make sure you understand how this function works and what it does!
  84. #
  85. def displayHand(hand):
  86. """
  87. Displays the letters currently in the hand.
  88.  
  89. For example:
  90. >>> displayHand({'a':1, 'x':2, 'l':3, 'e':1})
  91. Should print out something like:
  92. a x x l l l e
  93. The order of the letters is unimportant.
  94.  
  95. hand: dictionary (string -> int)
  96. """
  97. for letter in hand.keys():
  98. for j in range(hand[letter]):
  99. print letter, # print all on the same line
  100. print # print an empty line
  101.  
  102. #
  103. # Problem #2: Make sure you understand how this function works and what it does!
  104. #
  105. def dealHand(n):
  106. """
  107. Returns a random hand containing n lowercase letters.
  108. At least n/3 the letters in the hand should be VOWELS.
  109.  
  110. Hands are represented as dictionaries. The keys are
  111. letters and the values are the number of times the
  112. particular letter is repeated in that hand.
  113.  
  114. n: int >= 0
  115. returns: dictionary (string -> int)
  116. """
  117. hand={}
  118. numVowels = n / 3
  119.  
  120. for i in range(numVowels):
  121. x = VOWELS[random.randrange(0,len(VOWELS))]
  122. hand[x] = hand.get(x, 0) + 1
  123.  
  124. for i in range(numVowels, n):
  125. x = CONSONANTS[random.randrange(0,len(CONSONANTS))]
  126. hand[x] = hand.get(x, 0) + 1
  127.  
  128. return hand
  129.  
  130. #
  131. # Problem #2: Update a hand by removing letters
  132. #
  133. def updateHand(hand, word):
  134. """
  135. Assumes that 'hand' has all the letters in word.
  136. In other words, this assumes that however many times
  137. a letter appears in 'word', 'hand' has at least as
  138. many of that letter in it.
  139.  
  140. Updates the hand: uses up the letters in the given word
  141. and returns the new hand, without those letters in it.
  142.  
  143. Has no side effects: does not modify hand.
  144.  
  145. word: string
  146. hand: dictionary (string -> int)
  147. returns: dictionary (string -> int)
  148. """
  149. # TO DO ... <-- Remove this comment when you code this function
  150.  
  151.  
  152.  
  153. #
  154. # Problem #3: Test word validity
  155. #
  156. def isValidWord(word, hand, wordList):
  157. """
  158. Returns True if word is in the wordList and is entirely
  159. composed of letters in the hand. Otherwise, returns False.
  160.  
  161. Does not mutate hand or wordList.
  162.  
  163. word: string
  164. hand: dictionary (string -> int)
  165. wordList: list of lowercase strings
  166. """
  167. # TO DO ... <-- Remove this comment when you code this function
  168.  
  169.  
  170. #
  171. # Problem #4: Playing a hand
  172. #
  173.  
  174. def calculateHandlen(hand):
  175. """
  176. Returns the length (number of letters) in the current hand.
  177.  
  178. hand: dictionary (string-> int)
  179. returns: integer
  180. """
  181. # TO DO... <-- Remove this comment when you code this function
  182.  
  183.  
  184.  
  185. def playHand(hand, wordList, n):
  186. """
  187. Allows the user to play the given hand, as follows:
  188.  
  189. * The hand is displayed.
  190. * The user may input a word or a single period (the string ".")
  191. to indicate they're done playing
  192. * Invalid words are rejected, and a message is displayed asking
  193. the user to choose another word until they enter a valid word or "."
  194. * When a valid word is entered, it uses up letters from the hand.
  195. * After every valid word: the score for that word is displayed,
  196. the remaining letters in the hand are displayed, and the user
  197. is asked to input another word.
  198. * The sum of the word scores is displayed when the hand finishes.
  199. * The hand finishes when there are no more unused letters or the user
  200. inputs a "."
  201.  
  202. hand: dictionary (string -> int)
  203. wordList: list of lowercase strings
  204. n: integer (HAND_SIZE; i.e., hand size required for additional points)
  205.  
  206. """
  207. # BEGIN PSEUDOCODE <-- Remove this comment when you code this function; do your coding within the pseudocode (leaving those comments in-place!)
  208. # Keep track of the total score
  209.  
  210. # As long as there are still letters left in the hand:
  211.  
  212. # Display the hand
  213.  
  214. # Ask user for input
  215.  
  216. # If the input is a single period:
  217.  
  218. # End the game (break out of the loop)
  219.  
  220.  
  221. # Otherwise (the input is not a single period):
  222.  
  223. # If the word is not valid:
  224.  
  225. # Reject invalid word (print a message followed by a blank line)
  226.  
  227. # Otherwise (the word is valid):
  228.  
  229. # Tell the user how many points the word earned, and the updated total score, in one line followed by a blank line
  230.  
  231. # Update the hand
  232.  
  233.  
  234. # Game is over (user entered a '.' or ran out of letters), so tell user the total score
  235.  
  236.  
  237. #
  238. # Problem #5: Playing a game
  239. #
  240.  
  241. def playGame(wordList):
  242. """
  243. Allow the user to play an arbitrary number of hands.
  244.  
  245. 1) Asks the user to input 'n' or 'r' or 'e'.
  246. * If the user inputs 'n', let the user play a new (random) hand.
  247. * If the user inputs 'r', let the user play the last hand again.
  248. * If the user inputs 'e', exit the game.
  249. * If the user inputs anything else, tell them their input was invalid.
  250.  
  251. 2) When done playing the hand, repeat from step 1
  252. """
  253. # TO DO ... <-- Remove this comment when you code this function
  254. print "playGame not yet implemented." # <-- Remove this line when you code the function
  255.  
  256.  
  257.  
  258.  
  259. #
  260. # Build data structures used for entire session and play game
  261. #
  262. if __name__ == '__main__':
  263. wordList = loadWords()
  264. playGame(wordList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement