def compPlayHand(hand: d_si, wordList: List[str], n: int) -> None: """Allows the computer to play the given hand, following the same procedure as playHand, except instead of the user choosing a word, the computer chooses it. 1) The hand is displayed. 2) The computer chooses a word. 3) After every valid word: the word and the score for that word is displayed, the remaining letters in the hand are displayed, and the computer chooses another word. 4) The sum of the word scores is displayed when the hand finishes. 5) The hand finishes when the computer has exhausted its possible choices (i.e. compChooseWord returns None). hand: dictionary (string -> int) wordList: list (string) n: integer (HAND_SIZE; i.e., hand size required for additional points) Args: Args: hand: dict of letter generate for a func wordList: all words acepted n: hand size Return: None: """ # Keep track of the total score max_score = 0 # As long as there are still letters left in the hand: while (calculateHandlen(hand) > 0): # Display the hand print("Current Hand: ", end=' ') displayHand(hand) # computer's word pc_word = compChooseWord(hand, wordList, n) # If the input is a single period: if pc_word == None: # End the game (break out of the loop) break # Otherwise (the input is not a single period): else: # If the word is not valid: if (not isValidWord(pc_word, hand, wordList)): print('This is a terrible error! I need to check my own code!') break # Otherwise (the word is valid): else: # Tell the user how many points the word earned, and the # updated total score score = getWordScore(pc_word, n) max_score += score print( f'"{pc_word}" earned {score} points. Total: {max_score}' + ' points\n') # Update hand and show the updated hand to the user hand = updateHand(hand, pc_word) # Game is over (user entered a '.' or ran out of letters), so tell user # the total score print(f'Total score: {max_score} points.')
def compChooseWordbk(hand, wordList, n): """ Given a hand and a wordList, find the word that gives the maximum value score, and return it. This word should be calculated by considering all the words in the wordList. If no words in the wordList can be made from the hand, return None. hand: dictionary (string -> int) wordList: list (string) n: integer (HAND_SIZE; i.e., hand size required for additional points) returns: string or None """ # Create a new variable to store the maximum score seen so far (initially 0) bestScore = 0 # Create a new variable to store the best word seen so far (initially None) bestWord = None # For each word in the wordList for word in wordList: # If you can construct the word from your hand if ps4a.isValidWord(word, hand, wordList): # find out how much making that word is worth score = ps4a.getWordScore(word, n) # If the score for that word is higher than your best score if (score > bestScore): # update your best score, and best word accordingly bestScore = score bestWord = word # return the best word you found. return bestWord
def compPlayHand(hand, wordList, n): """ Allows the computer to play the given hand, following the same procedure as playHand, except instead of the user choosing a word, the computer chooses it. 1) The hand is displayed. 2) The computer chooses a word. 3) After every valid word: the word and the score for that word is displayed, the remaining letters in the hand are displayed, and the computer chooses another word. 4) The sum of the word scores is displayed when the hand finishes. 5) The hand finishes when the computer has exhausted its possible choices (i.e. compChooseWord returns None). hand: dictionary (string -> int) wordList: list (string) n: integer (HAND_SIZE; i.e., hand size required for additional points) """ # Keep track of the total score totalScore = 0 # As long as there are still letters left in the hand: while (ps4a.calculateHandlen(hand) > 0) : # Display the hand print("Current Hand: ", end=' ') ps4a.displayHand(hand) # computer's word word = compChooseWord(hand, wordList, n) # If the input is a single period: if word == None: # End the game (break out of the loop) break # Otherwise (the input is not a single period): else : # If the word is not valid: if (not ps4a.isValidWord(word, hand, wordList)) : print('This is a terrible error! I need to check my own code!') break # Otherwise (the word is valid): else : # Tell the user how many points the word earned, and the updated total score score = ps4a.getWordScore(word, n) totalScore += score print('"' + word + '" earned ' + str(score) + ' points. Total: ' + str(totalScore) + ' points') # Update hand and show the updated hand to the user hand = ps4a.updateHand(hand, word) print() # Game is over (user entered a '.' or ran out of letters), so tell user the total score print('Total score: ' + str(totalScore) + ' points.')
def compPlayHand(hand, wordList, n): """ Allows the computer to play the given hand, following the same procedure as playHand, except instead of the user choosing a word, the computer chooses it. 1) The hand is displayed. 2) The computer chooses a word. 3) After every valid word: the word and the score for that word is displayed, the remaining letters in the hand are displayed, and the computer chooses another word. 4) The sum of the word scores is displayed when the hand finishes. 5) The hand finishes when the computer has exhausted its possible choices (i.e. compChooseWord returns None). hand: dictionary (string -> int) wordList: list (string) n: integer (HAND_SIZE; i.e., hand size required for additional points) """ total = 0 while True: if not ps4a.calculateHandlen(hand) or not compChooseWord( hand, wordList, n): print("Total score: {} points.".format(total)) return print("Current Hand: ", end="") ps4a.displayHand(hand) for word in wordList: if ps4a.isValidWord(word, hand, wordList): score = ps4a.getWordScore(word, n) total += score print('"{}" earned {} points. Total: {} points'.format( word, score, total)) hand = ps4a.updateHand(hand, word)
def compChooseWord(hand, wordList, n): """ Given a hand and a wordList, find the word that gives the maximum value score, and return it. This word should be calculated by considering all the words in the wordList. If no words in the wordList can be made from the hand, return None. hand: dictionary (string -> int) wordList: list (string) n: integer (HAND_SIZE; i.e., hand size required for additional points) returns: string or None """ bestscore = 0 bestword = None for word in wordList: if ps4a.isValidWord(word, hand, wordList): if ps4a.getWordScore(word, n) > bestscore: bestscore = ps4a.getWordScore(word, n) bestword = word return bestword
def compChooseWord(hand: d_si, wordList: List[str], n: int) -> Optional[str]: """Given a hand and a wordList, find the word that gives the maximum value score, and return it. This word should be calculated by considering all the words in the wordList. If no words in the wordList can be made from the hand, return None. returns: string or None Args: hand: dict of letter generate for a func wordList: all words acepted n: hand size Returns: string or None """ # Create a new variable to store the maximum score seen so far(initially 0) max_score = 0 # Create a new variable to store the best word seen so far (initially None) best_word = None # For each word in the wordList for word in wordList: # If you can construct the word from your hand if isValidWord(word, hand, wordList): # find out how much making that word is worth score = getWordScore(word, n) # If the score for that word is higher than your best score if (score > max_score): # update your best score, and best word accordingly max_score = score best_word = word # return the best word you found. return best_word
def compPlayHand(hand, wordList, n): """ Allows the computer to play the given hand, as follows: * The hand is passed as a parameter to a procedure. * The computer may return a word or None to indicate she's done playing. * Invalid words are rejected, and the hand is terminated. * When a valid word is entered, it uses up letters from the hand. * After every valid word: the score for that word is displayed, the remaining letters in the hand are displayed, and the computer procedure is called again to input another word. * The sum of the word scores is displayed when the hand finishes. * The hand finishes when there are no more unused letters or the * computer procedure returns no words. hand: dictionary (string -> int) wordList: list of lowercase strings n: integer (HAND_SIZE; i.e., hand size required for additional points) """ strHand = '' handPoints = 0 wordPoints = 0 handLen = 0 currentWord = '' def prtLine1(cH): outputLine = 'Current Hand: ' + cH print(outputLine) # end of code def prtLine2(cw, wpts, hpts): outputLine = '"'+ cw + '"' + ' earned ' + str(wpts) + ' points. Total: ' + str(hpts) + ' points' print(outputLine) # end of code outputLine3 = 'Invalid word, hand terminated.' def prtLine4(hpts): outputLine = 'Run out of letters. Total score: ' + str(hpts) + ' points.' print(outputLine) # end of code def prtLine5(hpts): outputLine = 'Total score: '+ str(hpts) + ' points.' print(outputLine) # end of code inputLine1 = 'Calling procedure to enter word. If no word is returned hand is finished.' # Main loop while True: # Keep track of the total score # As long as there are still letters left in the hand handLen = calculateHandlen(hand) # see if hand still valid if handLen == 0: prtLine4(handPoints) # no more words return # end of game # Display the hand strHand = '' for letter in hand.keys(): # get hand in display format for i in range(hand[letter]): strHand += letter + ' ' prtLine1(strHand) # display current hand # Ask user for input # currentWord = raw_input(inputLine1) # get new word # print(inputLine1) # get new word currentWord = compChooseWord(hand, wordList, n) # If there is no input: if currentWord == None: # computer has no more words # End the game (break out of the loop) break # get out of loop # Otherwise (the input is not a single period): # If the word is not valid: if not isValidWord(currentWord, hand, wordList): # check if word is valid # Reject invalid word (print a message and end hand) print(outputLine3) # tell the user break else: # Otherwise (the word is valid): # Tell the user how many points the word earned, and the updated total score, in one line followed by a blank line wordPoints = getWordScore(currentWord, n) # get points earned handPoints += wordPoints # accumulate hand points prtLine2(currentWord, wordPoints, handPoints) # tell user points earned print('') # additional line # Update the hand hand = updateHand(hand, currentWord) # update hand # Game is over (user entered a '.' or ran out of letters), so tell user the total score prtLine5(handPoints) return None
def test_isValidWord(wordList): """ Unit test for isValidWord """ failure = False # test 1 word = "hello" handOrig = getFrequencyDict(word) handCopy = handOrig.copy() if not isValidWord(word, handCopy, wordList): print("FAILURE: test_isValidWord()") print( "\tExpected True, but got False for word: '" + word + "' and hand:", handOrig, ) failure = True # Test a second time to see if wordList or hand has been modified if not isValidWord(word, handCopy, wordList): print("FAILURE: test_isValidWord()") if handCopy != handOrig: print( "\tTesting word", word, "for a second time - be sure you're not modifying hand.", ) print("\tAt this point, hand ought to be", handOrig, "but it is", handCopy) else: print( "\tTesting word", word, "for a second time - have you modified wordList?", ) wordInWL = word in wordList print("The word", word, "should be in wordList - is it?", wordInWL) print( "\tExpected True, but got False for word: '" + word + "' and hand:", handCopy, ) failure = True # test 2 hand = {"r": 1, "a": 3, "p": 2, "e": 1, "t": 1, "u": 1} word = "rapture" if isValidWord(word, hand, wordList): print("FAILURE: test_isValidWord()") print( "\tExpected False, but got True for word: '" + word + "' and hand:", hand) failure = True # test 3 hand = {"n": 1, "h": 1, "o": 1, "y": 1, "d": 1, "w": 1, "e": 2} word = "honey" if not isValidWord(word, hand, wordList): print("FAILURE: test_isValidWord()") print( "\tExpected True, but got False for word: '" + word + "' and hand:", hand) failure = True # test 4 hand = {"r": 1, "a": 3, "p": 2, "t": 1, "u": 2} word = "honey" if isValidWord(word, hand, wordList): print("FAILURE: test_isValidWord()") print( "\tExpected False, but got True for word: '" + word + "' and hand:", hand) failure = True # test 5 hand = {"e": 1, "v": 2, "n": 1, "i": 1, "l": 2} word = "evil" if not isValidWord(word, hand, wordList): print("FAILURE: test_isValidWord()") print( "\tExpected True, but got False for word: '" + word + "' and hand:", hand) failure = True # test 6 word = "even" if isValidWord(word, hand, wordList): print("FAILURE: test_isValidWord()") print( "\tExpected False, but got True for word: '" + word + "' and hand:", hand) print( "\t(If this is the only failure, make sure isValidWord() isn't mutating its inputs)" ) failure = True if not failure: print("SUCCESS: test_isValidWord()")