Advertisement
andruhovski

Problem Set 4b

Mar 11th, 2013
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.77 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):
  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.    returns: string or None
  23.    """
  24.     MaxScore=0
  25.     BestWord=None
  26.     Score=0
  27.     for eachWord in wordList:
  28.         if isValidWord(eachWord, hand, wordList):
  29.             Score=getWordScore(eachWord,HAND_SIZE)
  30.             if MaxScore<Score:
  31.                 MaxScore=Score
  32.                 BestWord=eachWord
  33.                 Score=0
  34.     return BestWord
  35.  
  36. #
  37. # Problem #7: Computer plays a hand
  38. #
  39. def compPlayHand(hand, wordList, n):
  40.     """
  41.    Allows the computer to play the given hand, following the same procedure
  42.    as playHand, except instead of the user choosing a word, the computer
  43.    chooses it.
  44.  
  45.    1) The hand is displayed.
  46.    2) The computer chooses a word.
  47.    3) After every valid word: the word and the score for that word is
  48.    displayed, the remaining letters in the hand are displayed, and the
  49.    computer chooses another word.
  50.    4)  The sum of the word scores is displayed when the hand finishes.
  51.    5)  The hand finishes when the computer has exhausted its possible
  52.    choices (i.e. compChooseWord returns None).
  53.  
  54.    hand: dictionary (string -> int)
  55.    wordList: list (string)
  56.    """
  57.     total=0
  58.     while calculateHandlen(hand)>0:
  59.         print ('Current Hand: ',end=" ")
  60.         displayHand(hand)
  61.         word=compChooseWord(hand, wordList)
  62.         if word!=None:
  63.             total+=getWordScore(word, n)
  64.             print (word,'earned', getWordScore(word, n), 'points. Total: ', total, 'points')
  65.         elif word==None or calculateHandlen(hand)<1:
  66.             break
  67.         hand=updateHand(hand, word)
  68.     print ('Total score: ', total, 'points.')
  69.  
  70.    
  71. #
  72. # Problem #8: Playing a game
  73. #
  74. #
  75. def playGame(wordList,n):
  76.     """
  77.    Allow the user to play an arbitrary number of hands.
  78.  
  79.    1) Asks the user to input 'n' or 'r' or 'e'.
  80.        * If the user inputs 'e', immediately exit the game.
  81.        * If the user inputs anything that's not 'n', 'r', or 'e', keep asking them again.
  82.  
  83.    2) Asks the user to input a 'u' or a 'c'.
  84.        * If the user inputs anything that's not 'c' or 'u', keep asking them again.
  85.  
  86.    3) Switch functionality based on the above choices:
  87.        * If the user inputted 'n', play a new (random) hand.
  88.        * Else, if the user inputted 'r', play the last hand again.
  89.      
  90.        * If the user inputted 'u', let the user play the game
  91.          with the selected hand, using playHand.
  92.        * If the user inputted 'c', let the computer play the
  93.          game with the selected hand, using compPlayHand.
  94.  
  95.    4) After the computer or user has played the hand, repeat from step 1
  96.  
  97.    wordList: list (string)
  98.    """      
  99.     isGameRunning = True
  100.     hand = {}
  101.  
  102.     while isGameRunning == True:
  103.         inp = input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
  104.  
  105.         if inp == 'n':
  106.             hand = dealHand(n)
  107.             while True:
  108.                 inp = input('Enter c to give this hand to a computer or u to play by yourself: ')
  109.                 if inp == 'u':
  110.                     playHand(hand.copy(), wordList, n)
  111.                     break
  112.                 elif inp == 'c':
  113.                     compPlayHand(hand.copy(), wordList, n)
  114.                     break
  115.                 else:
  116.                     print ('Invalid command.')
  117.                     print
  118.         elif inp == 'r':
  119.             if hand != {}:
  120.                 while True:
  121.                     inp = input('Enter c to give this hand to a computer or u to play by yourself: ')
  122.                     if inp == 'u':
  123.                         playHand(hand.copy(), wordList, n)
  124.                         break
  125.                     elif inp == 'c':
  126.                         compPlayHand(hand.copy(), wordList, n)
  127.                         break
  128.                     else:
  129.                         print ('Invalid command.')
  130.                         print
  131.             else:
  132.                 print ('No hand have been played yet. Please play a new hand first!')
  133.                 print
  134.         elif inp == 'e':
  135.             isGameRunning = False
  136.         else:
  137.             print ('Invalid command.')
  138.  
  139.        
  140. ##
  141. ## Build data structures used for entire session and play game
  142. ##
  143. if __name__ == '__main__':
  144.     print ("Waring! This code tested in Python 3.3 (not in Python 2.7)"
  145.     wordList = loadWords()
  146.     playGame(wordList, HAND_SIZE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement