def play_hand(hand,points_dict):
"""
Allows the user to play the given hand, as follows:
* The hand is displayed.
* The user may input a word.
* An invalid word is rejected, and a message is displayed asking
the user to choose another word.
* When a valid word is entered, it uses up letters from the hand.
* After every valid word: the score for that word and the total
score so far are displayed, the remaining letters in the hand
are displayed, and the user is asked 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.
The user can also finish playing the hand by inputing a single
period (the string '.') instead of a word.
* The final score is displayed.
hand: dictionary (string -> int)
dictionary: dictionary of lower case string words and scores
"""
userWords = None
score = 0.00
counter = 0
x = rem_Hand(hand)##Creates a working hand x, keeping original hand to print
print 'Current Hand: ', display_hand(x)
time_limit = get_time_limit(points_dict,k)
overall_time = 0
while counter < HAND_SIZE:
start_time = time.time()
userWords = pick_best_word(x,points_dict)
end_time = time.time()
if userWords == '.':
return '"." received. Game over. Total Score: %.2f'%score
else:
total_time = (end_time-start_time)
overall_time += total_time
if time_limit-overall_time <= .0004:
return 'You exceeded the time limit. Total score: %.2f'%score
if total_time == 0:
total_time = 0.0005
score += (points_dict[userWords])/total_time
counter += len(userWords)
x = update_hand(x,userWords)
if counter < HAND_SIZE:
print 'It took %0.2f seconds to provide an answer' %total_time
print "'",userWords,"'", 'earned', ('%0.2f' %((points_dict[userWords])/total_time)),'points. Current Score: ',('%.2f'%score)
print 'Current hand: ',; display_hand(x)
return 'Total Score: %.2f'%score
def testph(hand):
'''test play_hand with string hand'''
assert type(hand)==type('ab'), 'hand must be a string'
x = get_frequency_dict(hand)
return play_hand(x,points_dict)
##beginning of ps6
def pick_best_word(hand,points_dict):
'''Return the highest scoring word from words_dict that can be made
with the given hand.
Return '.' if no words can be made with the given hand.
hand: directory of frequency
points_dict: dictionary of words and scores
'''
score=0
words = points_dict.keys()
ansFound=False
for i in range(len(words)):
if hand == get_frequency_dict(words[i]):
return words[i]
else:
counter=0
for k in get_frequency_dict(words[i]).keys():
if k in hand.keys():
if hand[k]>=get_frequency_dict(words[i])[k]:
counter+=1
if counter==len(get_frequency_dict(words[i])):
if points_dict[words[i]]>score:
score=points_dict[words[i]]
ans=words[i]
ansFound=True
if ansFound:
return ans
else:
return '.'
def get_time_limit(points_dict,k):
'''
Return the time limit for the computer player as a function of
the multiplier k.
points_dict should be the same dictionary that is created by
get_words_to_points
'''
start_time = time.time()
#Do some computation. The only purpose of the computation is so we can
#figure out how long the computer takes to perfom a known task.
for word in points_dict:
get_frequency_dict(word)
get_word_score(word,HAND_SIZE)
end_time=time.time()
return (end_time - start_time)*k
#
#
def play_game():
"""
Allow the user to play an arbitrary number of hands.
* Asks the user to input 'n' or 'r' or 'e'.
* If the user inputs 'n', let the user play a new (random) hand.
When done playing the hand, ask the 'n' or 'e' question again.
* If the user inputs 'r', let the user play the last hand again.
* If the user inputs 'e', exit the game.
* If the user inputs anything else, ask them again.
"""
hand = deal_hand(HAND_SIZE) # random init
while True:
cmd = raw_input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
if cmd == 'n':
hand = deal_hand(HAND_SIZE)
play_hand(hand.copy(), points_dict)
print
elif cmd == 'r':
play_hand(hand.copy(), points_dict)
print
elif cmd == 'e':
break
else:
print "Invalid command."
# Build data structures used for entire session and play game
#
if __name__ == '__main__':
#word_list = load_words()
play_game()