Guest User

Untitled

a guest
Jan 17th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.42 KB | None | 0 0
  1. # 6.0001 Problem Set 3
  2. #
  3. # The 6.0001 Word Game
  4. # Created by: Kevin Luu <luuk> and Jenna Wiens <jwiens>
  5. #
  6. # Name : Yuta Nakamura
  7. # Time spent : <total time>
  8.  
  9. import math
  10. import random
  11. import string
  12.  
  13. VOWELS = 'aeiou'
  14. CONSONANTS = 'bcdfghjklmnpqrstvwxyz'
  15. HAND_SIZE = 7
  16.  
  17. SCRABBLE_LETTER_VALUES = {
  18. 'a': 1,
  19. 'b': 3,
  20. 'c': 3,
  21. 'd': 2,
  22. 'e': 1,
  23. 'f': 4,
  24. 'g': 2,
  25. 'h': 4,
  26. 'i': 1,
  27. 'j': 8,
  28. 'k': 5,
  29. 'l': 1,
  30. 'm': 3,
  31. 'n': 1,
  32. 'o': 1,
  33. 'p': 3,
  34. 'q': 10,
  35. 'r': 1,
  36. 's': 1,
  37. 't': 1,
  38. 'u': 1,
  39. 'v': 4,
  40. 'w': 4,
  41. 'x': 8,
  42. 'y': 4,
  43. 'z': 10,
  44. '*': 0
  45. }
  46.  
  47. # -----------------------------------
  48. # Helper code
  49. # (you don't need to understand this helper code)
  50.  
  51. WORDLIST_FILENAME = "words.txt"
  52.  
  53.  
  54. def load_words():
  55. """
  56. Returns a list of valid words. Words are strings of lowercase letters.
  57.  
  58. Depending on the size of the word list, this function may
  59. take a while to finish.
  60. """
  61.  
  62. print("Loading word list from file...")
  63. # inFile: file
  64. inFile = open(WORDLIST_FILENAME, 'r')
  65. # wordlist: list of strings
  66. wordlist = []
  67. for line in inFile:
  68. wordlist.append(line.strip().lower())
  69. print(" ", len(wordlist), "words loaded.")
  70. return wordlist
  71.  
  72.  
  73. def get_frequency_dict(sequence):
  74. """
  75. Returns a dictionary where the keys are elements of the sequence
  76. and the values are integer counts, for the number of times that
  77. an element is repeated in the sequence.
  78.  
  79. sequence: string or list
  80. return: dictionary
  81. """
  82.  
  83. # freqs: dictionary (element_type -> int)
  84. freq = {}
  85. for x in sequence:
  86. freq[x] = freq.get(x, 0) + 1
  87. return freq
  88.  
  89.  
  90. # (end of helper code)
  91. # -----------------------------------
  92.  
  93.  
  94. #
  95. # Problem #1: Scoring a word
  96. #
  97. def get_word_score(word, n):
  98. """
  99. Returns the score for a word. Assumes the word is a
  100. valid word.
  101.  
  102. You may assume that the input word is always either a string of letters,
  103. or the empty string "". You may not assume that the string will only contain
  104. lowercase letters, so you will have to handle uppercase and mixed case strings
  105. appropriately.
  106.  
  107. The score for a word is the product of two components:
  108.  
  109. The first component is the sum of the points for letters in the word.
  110. The second component is the larger of:
  111. 1, or
  112. 7*wordlen - 3*(n-wordlen), where wordlen is the length of the word
  113. and n is the hand length when the word was played
  114.  
  115. Letters are scored as in Scrabble; A is worth 1, B is
  116. worth 3, C is worth 3, D is worth 2, E is worth 1, and so on.
  117.  
  118. word: string
  119. n: int >= 0
  120. returns: int >= 0
  121. """
  122.  
  123. sum_chars = 0
  124. word = word.lower()
  125. for char in word:
  126. sum_chars += SCRABBLE_LETTER_VALUES[char]
  127. coeff = max((7 * len(word) - 3 * (n - len(word))), 1)
  128. return sum_chars * coeff
  129.  
  130.  
  131. #
  132. # Make sure you understand how this function works and what it does!
  133. #
  134.  
  135.  
  136. def display_hand(hand):
  137. """
  138. Displays the letters currently in the hand.
  139.  
  140. For example:
  141. display_hand({'a':1, 'x':2, 'l':3, 'e':1})
  142. Should print out something like:
  143. a x x l l l e
  144. The order of the letters is unimportant.
  145.  
  146. hand: dictionary (string -> int)
  147. """
  148.  
  149. for letter in hand.keys():
  150. for j in range(hand[letter]):
  151. print(letter, end=' ') # print all on the same line
  152. print() # print an empty line
  153.  
  154.  
  155. #
  156. # Make sure you understand how this function works and what it does!
  157. # You will need to modify this for Problem #4.
  158. #
  159.  
  160.  
  161. def deal_hand(n):
  162. """
  163. Returns a random hand containing n lowercase letters.
  164. ceil(n/3) letters in the hand should be VOWELS (note,
  165. ceil(n/3) means the smallest integer not less than n/3).
  166.  
  167. Hands are represented as dictionaries. The keys are
  168. letters and the values are the number of times the
  169. particular letter is repeated in that hand.
  170.  
  171. n: int >= 0
  172. returns: dictionary (string -> int)
  173. """
  174.  
  175. hand = {}
  176. num_vowels = int(math.ceil(n / 3)) - 1
  177.  
  178. for i in range(num_vowels):
  179. x = random.choice(VOWELS)
  180. hand[x] = hand.get(x, 0) + 1
  181.  
  182. for i in range(num_vowels, n):
  183. x = random.choice(CONSONANTS)
  184. hand[x] = hand.get(x, 0) + 1
  185.  
  186. hand["*"] = 1
  187.  
  188. return hand
  189.  
  190.  
  191. #
  192. # Problem #2: Update a hand by removing letters
  193. #
  194.  
  195.  
  196. def update_hand(hand, word):
  197. """
  198. Does NOT assume that hand contains every letter in word at least as
  199. many times as the letter appears in word. Letters in word that don't
  200. appear in hand should be ignored. Letters that appear in word more times
  201. than in hand should never result in a negative count; instead, set the
  202. count in the returned hand to 0 (or remove the letter from the
  203. dictionary, depending on how your code is structured).
  204.  
  205. Updates the hand: uses up the letters in the given word
  206. and returns the new hand, without those letters in it.
  207.  
  208. Has no side effects: does not modify hand.
  209.  
  210. word: string
  211. hand: dictionary (string -> int)
  212. returns: dictionary (string -> int)
  213. """
  214.  
  215. word = word.lower()
  216. new_hand = hand.copy()
  217. for char in word:
  218. if new_hand.get(char, 0) > 0:
  219. new_hand[char] -= 1
  220. return new_hand
  221.  
  222.  
  223. #
  224. # Problem #3: Test word validity
  225. #
  226.  
  227.  
  228. def is_valid_word(word, hand, word_list):
  229. """
  230. Returns True if word is in the word_list and is entirely
  231. composed of letters in the hand. Otherwise, returns False.
  232. Does not mutate hand or word_list.
  233.  
  234. word: string
  235. hand: dictionary (string -> int)
  236. word_list: list of lowercase strings
  237. returns: boolean
  238. """
  239.  
  240. word = word.lower()
  241. if "*" in word:
  242. subsituted_words = map(lambda v: word.replace("*", v), VOWELS)
  243. for w in subsituted_words:
  244. if w in word_list:
  245. return True
  246. return False
  247. else:
  248. if word not in word_list:
  249. return False
  250. word_freq_dict = get_frequency_dict(word)
  251. for char in word_freq_dict.keys():
  252. if word_freq_dict[char] > hand.get(char, 0):
  253. return False
  254. return True
  255.  
  256.  
  257. #
  258. # Problem #5: Playing a hand
  259. #
  260.  
  261.  
  262. def calculate_handlen(hand):
  263. """
  264. Returns the length (number of letters) in the current hand.
  265.  
  266. hand: dictionary (string-> int)
  267. returns: integer
  268. """
  269. sum_len = 0
  270. for keys in hand.keys():
  271. sum_len += hand[keys]
  272. return sum_len
  273.  
  274.  
  275. def play_hand(hand, word_list):
  276. """
  277. Allows the user to play the given hand, as follows:
  278.  
  279. * The hand is displayed.
  280.  
  281. * The user may input a word.
  282.  
  283. * When any word is entered (valid or invalid), it uses up letters
  284. from the hand.
  285.  
  286. * An invalid word is rejected, and a message is displayed asking
  287. the user to choose another word.
  288.  
  289. * After every valid word: the score for that word is displayed,
  290. the remaining letters in the hand are displayed, and the user
  291. is asked to input another word.
  292.  
  293. * The sum of the word scores is displayed when the hand finishes.
  294.  
  295. * The hand finishes when there are no more unused letters.
  296. The user can also finish playing the hand by inputing two
  297. exclamation points (the string '!!') instead of a word.
  298.  
  299. hand: dictionary (string -> int)
  300. word_list: list of lowercase strings
  301. returns: the total score for the hand
  302.  
  303. """
  304.  
  305. # BEGIN PSEUDOCODE <-- Remove this comment when you implement this function
  306. # Keep track of the total score
  307. total_score = 0
  308.  
  309. # As long as there are still letters left in the hand:
  310. while calculate_handlen(hand) > 0:
  311.  
  312. # Display the hand
  313. print("Current Hand: ", end="")
  314. display_hand(hand)
  315.  
  316. # Ask user for input
  317. input_word = input(
  318. 'Enter word, or "!!" to indicate that you are finished: ')
  319. # If the input is two exclamation points:
  320. # End the game (break out of the loop)
  321. if input_word == "!!":
  322. break
  323.  
  324. # Otherwise (the input is not two exclamation points):
  325. # If the word is valid:
  326. # Tell the user how many points the word earned,
  327. # and the updated total score
  328. else:
  329. if is_valid_word(input_word, hand, word_list):
  330. score = get_word_score(input_word, calculate_handlen(hand))
  331. total_score += score
  332. print('"{}" earned {} points. Total: {} points'.format(
  333. input_word, score, total_score))
  334. # Otherwise (the word is not valid):
  335. # Reject invalid word (print a message)
  336. else:
  337. print("That is not a valid word. Please choose another word.")
  338. # update the user's hand by removing the letters of their inputted word
  339. hand = update_hand(hand, input_word)
  340. # Game is over (user entered '!!' or ran out of letters),
  341. if calculate_handlen(hand) <= 0:
  342. print("Ran out of letters. Total score: {} points".format(
  343. total_score))
  344. break
  345. # so tell user the total score
  346.  
  347. # Return the total score as result of function
  348. return total_score
  349.  
  350.  
  351. #
  352. # Problem #6: Playing a game
  353. #
  354.  
  355. #
  356. # procedure you will use to substitute a letter in a hand
  357. #
  358.  
  359.  
  360. def substitute_hand(hand, letter):
  361. """
  362. Allow the user to replace all copies of one letter in the hand (chosen by user)
  363. with a new letter chosen from the VOWELS and CONSONANTS at random. The new letter
  364. should be different from user's choice, and should not be any of the letters
  365. already in the hand.
  366.  
  367. If user provide a letter not in the hand, the hand should be the same.
  368.  
  369. Has no side effects: does not mutate hand.
  370.  
  371. For example:
  372. substitute_hand({'h':1, 'e':1, 'l':2, 'o':1}, 'l')
  373. might return:
  374. {'h':1, 'e':1, 'o':1, 'x':2} -> if the new letter is 'x'
  375. The new letter should not be 'h', 'e', 'l', or 'o' since those letters were
  376. already in the hand.
  377.  
  378. hand: dictionary (string -> int)
  379. letter: string
  380. returns: dictionary (string -> int)
  381. """
  382.  
  383. new_hand = hand.copy()
  384. if letter not in hand.keys():
  385. return new_hand
  386. new_hand[letter] = max(0, new_hand[letter] - 1)
  387. prohibited_chars = set([char for char in hand.keys() if hand[char] > 0])
  388. available_chars = list(set(VOWELS + CONSONANTS) - prohibited_chars)
  389. new_letter = random.choice(available_chars)
  390. new_hand[new_letter] = 1
  391. return new_hand
  392.  
  393.  
  394. def play_game(word_list):
  395. """
  396. Allow the user to play a series of hands
  397.  
  398. * Asks the user to input a total number of hands
  399.  
  400. * Accumulates the score for each hand into a total score for the
  401. entire series
  402.  
  403. * For each hand, before playing, ask the user if they want to substitute
  404. one letter for another. If the user inputs 'yes', prompt them for their
  405. desired letter. This can only be done once during the game. Once the
  406. substitue option is used, the user should not be asked if they want to
  407. substitute letters in the future.
  408.  
  409. * For each hand, ask the user if they would like to replay the hand.
  410. If the user inputs 'yes', they will replay the hand and keep
  411. the better of the two scores for that hand. This can only be done once
  412. during the game. Once the replay option is used, the user should not
  413. be asked if they want to replay future hands. Replaying the hand does
  414. not count as one of the total number of hands the user initially
  415. wanted to play.
  416.  
  417. * Note: if you replay a hand, you do not get the option to substitute
  418. a letter - you must play whatever hand you just had.
  419.  
  420. * Returns the total score for the series of hands
  421.  
  422. word_list: list of lowercase strings
  423. """
  424.  
  425. # ask number of hands player want to play
  426. num_play = int(input("Enter total number of hands: "))
  427. total_score = 0
  428. can_replay = True
  429.  
  430. for i in range(num_play):
  431. hand = deal_hand(HAND_SIZE)
  432. # ask if the player want to sustitute the word
  433.  
  434. if can_replay:
  435. while True:
  436. print("Current hand: ", end="")
  437. display_hand(hand)
  438. is_substitute = input(
  439. "Would you like to substitute a letter? ")
  440.  
  441. if is_substitute == "yes":
  442. change_char = input(
  443. "Which letter would you like to replace: ")
  444.  
  445. hand = substitute_hand(hand, change_char)
  446. break
  447. elif is_substitute == "no":
  448. break
  449. else:
  450. print("choose 'yes' or 'no'")
  451.  
  452. # play one hand
  453. hand_score = play_hand(hand, word_list)
  454. total_score += hand_score
  455. print("-" * 10)
  456.  
  457. # ask if the user wants to replay the hand
  458. if can_replay:
  459. while True:
  460. is_repeat = input("Would you like to replay the hand: ")
  461. if is_repeat == "yes":
  462. replay_score = play_hand(hand, word_list)
  463. if replay_score > hand_score:
  464. total_score = total_score - hand_score + replay_score
  465. can_replay = False
  466. print("-" * 10)
  467. break
  468. elif is_repeat == "no":
  469. break
  470. else:
  471. print("choose 'yes' or 'no'")
  472.  
  473. # print the total score
  474. print("Total score score over all hands: {}".format(total_score))
  475.  
  476.  
  477. #
  478. # Build data structures used for entire session and play game
  479. # Do not remove the "if __name__ == '__main__':" line - this code is executed
  480. # when the program is run directly, instead of through an import statement
  481. #
  482. if __name__ == '__main__':
  483. word_list = load_words()
  484. play_game(word_list)
Add Comment
Please, Sign In to add comment