Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ps4a import *
- import time
- #
- #
- # Problem #6: Computer chooses a word
- #
- #
- def compChooseWord(hand, wordList):
- """
- 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)
- returns: string or None
- """
- MaxScore=0
- BestWord=None
- Score=0
- for eachWord in wordList:
- if isValidWord(eachWord, hand, wordList):
- Score=getWordScore(eachWord,HAND_SIZE)
- if MaxScore<Score:
- MaxScore=Score
- BestWord=eachWord
- Score=0
- return BestWord
- #
- # Problem #7: Computer plays a hand
- #
- 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)
- """
- total=0
- while calculateHandlen(hand)>0:
- print ('Current Hand: ',end=" ")
- displayHand(hand)
- word=compChooseWord(hand, wordList)
- if word!=None:
- total+=getWordScore(word, n)
- print (word,'earned', getWordScore(word, n), 'points. Total: ', total, 'points')
- elif word==None or calculateHandlen(hand)<1:
- break
- hand=updateHand(hand, word)
- print ('Total score: ', total, 'points.')
- #
- # Problem #8: Playing a game
- #
- #
- def playGame(wordList,n):
- """
- Allow the user to play an arbitrary number of hands.
- 1) Asks the user to input 'n' or 'r' or 'e'.
- * If the user inputs 'e', immediately exit the game.
- * If the user inputs anything that's not 'n', 'r', or 'e', keep asking them again.
- 2) Asks the user to input a 'u' or a 'c'.
- * If the user inputs anything that's not 'c' or 'u', keep asking them again.
- 3) Switch functionality based on the above choices:
- * If the user inputted 'n', play a new (random) hand.
- * Else, if the user inputted 'r', play the last hand again.
- * If the user inputted 'u', let the user play the game
- with the selected hand, using playHand.
- * If the user inputted 'c', let the computer play the
- game with the selected hand, using compPlayHand.
- 4) After the computer or user has played the hand, repeat from step 1
- wordList: list (string)
- """
- isGameRunning = True
- hand = {}
- while isGameRunning == True:
- inp = input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
- if inp == 'n':
- hand = dealHand(n)
- while True:
- inp = input('Enter c to give this hand to a computer or u to play by yourself: ')
- if inp == 'u':
- playHand(hand.copy(), wordList, n)
- break
- elif inp == 'c':
- compPlayHand(hand.copy(), wordList, n)
- break
- else:
- print ('Invalid command.')
- print
- elif inp == 'r':
- if hand != {}:
- while True:
- inp = input('Enter c to give this hand to a computer or u to play by yourself: ')
- if inp == 'u':
- playHand(hand.copy(), wordList, n)
- break
- elif inp == 'c':
- compPlayHand(hand.copy(), wordList, n)
- break
- else:
- print ('Invalid command.')
- print
- else:
- print ('No hand have been played yet. Please play a new hand first!')
- print
- elif inp == 'e':
- isGameRunning = False
- else:
- print ('Invalid command.')
- ##
- ## Build data structures used for entire session and play game
- ##
- if __name__ == '__main__':
- print ("Waring! This code tested in Python 3.3 (not in Python 2.7)"
- wordList = loadWords()
- playGame(wordList, HAND_SIZE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement