Advertisement
ArCiGo

ps4b.py

Jan 10th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.64 KB | None | 0 0
  1. from ps4a import *
  2. import time
  3.  
  4.  
  5. #
  6. #
  7. # Problem #6: Computer chooses a word
  8. #
  9. #
  10. def compChooseWord(hand, wordList, n):
  11.     """
  12.    Given a hand and a wordList, find the word that gives
  13.    the maximum value score, and return it.
  14.  
  15.    This word should be calculated by considering all the words
  16.    in the wordList.
  17.  
  18.    If no words in the wordList can be made from the hand, return None.
  19.  
  20.    hand: dictionary (string -> int)
  21.    wordList: list (string)
  22.    n: integer (HAND_SIZE; i.e., hand size required for additional points)
  23.  
  24.    returns: string or None
  25.    """
  26.     # Create a new variable to store the maximum score seen so far (initially 0)
  27.     maximumScore = 0
  28.     # Create a new variable to store the best word seen so far (initially None)  
  29.     bestWord = None
  30.     # For each word in the wordList
  31.     for word in wordList :
  32.         # If you can construct the word from your hand
  33.         # (hint: you can use isValidWord, or - since you don't really need to test if the word is in the wordList - you can make a similar function that omits that test)
  34.         if isValidWord(word, hand, wordList) :
  35.             # Find out how much making that word is worth
  36.             score = getWordScore(word, n)
  37.             # If the score for that word is higher than your best score
  38.             if score > maximumScore :
  39.                 # Update your best score, and best word accordingly
  40.                 maximumScore = score
  41.                 bestWord = word
  42.  
  43.     # return the best word you found.
  44.     return bestWord
  45.  
  46. #
  47. # Problem #7: Computer plays a hand
  48. #
  49. def compPlayHand(hand, wordList, n):
  50.     """
  51.    Allows the computer to play the given hand, following the same procedure
  52.    as playHand, except instead of the user choosing a word, the computer
  53.    chooses it.
  54.  
  55.    1) The hand is displayed.
  56.    2) The computer chooses a word.
  57.    3) After every valid word: the word and the score for that word is
  58.    displayed, the remaining letters in the hand are displayed, and the
  59.    computer chooses another word.
  60.    4)  The sum of the word scores is displayed when the hand finishes.
  61.    5)  The hand finishes when the computer has exhausted its possible
  62.    choices (i.e. compChooseWord returns None).
  63.  
  64.    hand: dictionary (string -> int)
  65.    wordList: list (string)
  66.    n: integer (HAND_SIZE; i.e., hand size required for additional points)
  67.    """
  68.     totalScore = 0
  69.     word = ''
  70.    
  71.     while calculateHandlen(hand) > 0 :
  72.         print("Current hand: "),
  73.         displayHand(hand)
  74.        
  75.         word = compChooseWord(hand, wordList, n)
  76.        
  77.         if word == None :
  78.             break
  79.         else :
  80.             totalScore += getWordScore(word, n)
  81.             print("\""+str(word)+"\" earned "+str(getWordScore(word, n))+" points. Total: "+str(totalScore))
  82.             hand = updateHand(hand, word)
  83.     print("Total score: "+str(totalScore)+" points.")
  84.  
  85. #
  86. # Problem #8: Playing a game
  87. #
  88. #
  89. def playGame(wordList):
  90.     """
  91.    Allow the user to play an arbitrary number of hands.
  92.  
  93.    1) Asks the user to input 'n' or 'r' or 'e'.
  94.        * If the user inputs 'e', immediately exit the game.
  95.        * If the user inputs anything that's not 'n', 'r', or 'e', keep asking them again.
  96.  
  97.    2) Asks the user to input a 'u' or a 'c'.
  98.        * If the user inputs anything that's not 'c' or 'u', keep asking them again.
  99.  
  100.    3) Switch functionality based on the above choices:
  101.        * If the user inputted 'n', play a new (random) hand.
  102.        * Else, if the user inputted 'r', play the last hand again.
  103.      
  104.        * If the user inputted 'u', let the user play the game
  105.          with the selected hand, using playHand.
  106.        * If the user inputted 'c', let the computer play the
  107.          game with the selected hand, using compPlayHand.
  108.  
  109.    4) After the computer or user has played the hand, repeat from step 1
  110.  
  111.    wordList: list (string)
  112.    """
  113.     while True :
  114.         userInput = raw_input(str("Enter n to deal a new hand, r to replay the last hand, or e to end game: "))
  115.         print("")
  116.        
  117.         if userInput == "n" :
  118.             while True :
  119.                 userTypeGame = raw_input(str("Enter u to have yourself play, c to have the computer play: "))
  120.                 if userTypeGame == "u" :
  121.                     n = HAND_SIZE
  122.                     hand = dealHand(n)
  123.                     playHand(hand, wordList, n)
  124.                     break
  125.                 elif userTypeGame == "c" :
  126.                     n = HAND_SIZE
  127.                     hand = dealHand(n)
  128.                     compPlayHand(hand, wordList, n)
  129.                     break
  130.                 else :
  131.                   print("Invalid command.")
  132.         elif userInput == "r" :
  133.             while True :
  134.                 try :
  135.                     len(hand) > 0
  136.                     userTypeGame_r = raw_input(str("Enter u to have yourself play, c to have the computer play: "))
  137.                     if userTypeGame_r == "u" :
  138.                         playHand(hand, wordList, n)
  139.                         break;
  140.                     elif userTypeGame_r == "c" :
  141.                         compPlayHand(hand, wordList, n)
  142.                         break
  143.                     else :
  144.                         print("Invalid command.")
  145.                 except :
  146.                     print("You have not played a hand yet. Please play a new hand first!")
  147.                     break
  148.         elif userInput == "e" :
  149.             break
  150.         else :
  151.             print("Invalid command.")
  152.                
  153. #
  154. # Build data structures used for entire session and play game
  155. #
  156. if __name__ == '__main__':
  157.     wordList = loadWords()
  158.     playGame(wordList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement