简介
在上面的第一部分我们实现了发牌-构成单词-积分并输出的基础性工作,在这一部分我们将试着让计算机发牌,计算机来玩游戏
计算机游玩部分
在这部分我们能够构建一个游戏模式,使得计算机跟我们一起玩单词游戏。
具体模式是怎样的呢?
当每次开局,我们能够选择是开始新手牌、上一把手牌还是退出游戏。
一旦选定新手牌或者上一把手牌,我们会让你选定是让计算机玩还是自己玩,如此能够返回不同的游戏组合和得分可能性.
在这个问题中最有趣的是处理各种游戏模式之间的切换问题,实际上,如果让我自己设计者这个游戏,我可能会利用前面处理好的单人模式,然后把电脑模式封装起来,而不是脚本化地写在同一个文件里面
代码
计算机产生选词部分,
让计算机每次从当前hand当中拼出能够在wordList中找到的最大分值的词语,这种算法不知道是不是能够完全战胜人类的选词,因为人类可以不必要每次都选择最大分值,而可以利用奖励分值的机制打败计算机,这种算法有待商榷!
from ps4a import *
import time
n=HAND_SIZE
hand=dealHand(n)
#
#
# Problem #6: Computer chooses a 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
"""
ms=0
cword=None
for word in wordList:
if isValidWord(word, hand, wordList) and getWordScore(word, n)>ms:
cword=word
ms=getWordScore(word, n)
return cword
计算机玩一手部分和游戏模式选择部分,其中游戏模式选择部分对不同地方出的输入错误,其中的处理方式尤为值得关注
#
# 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)
n: integer (HAND_SIZE; i.e., hand size required for additional points)
"""
total=0
while calculateHandlen(hand)!=0:
displayHand(hand)
print('')
word=compChooseWord(hand, wordList, n)
assert isValidWord(word, hand, wordList)==True
if type(word)==type(None):
print('Total score:',total,'points.')
break
else:
total+=getWordScore(word, n)
print('"',word,'" earned ',getWordScore(word, n),'points. Total:',total,'points.')
hand=updateHand(hand, word)
if calculateHandlen(hand)==0:
print('Total score:',total,'points.')
break
else:
print(' ')
# Problem #8: Playing a game
#
#
def playGame(wordList):
"""
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)
"""
pr=0
while True:
mode=input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ')
if mode not in ['n','r','e']:
print('Invalid command.')
continue
if mode=='e':
break
while True:
person=input('Enter u to have yourself play, c to have the computer play:')
if person not in ['u','c']:
print('Invalid command.')
else:
print('')
break
if person=='u':
if mode=='r':
if pr==0:
print('You have not played a hand yet. Please play a new hand first!')
continue
else:
playHand(hand,wordList,n)
continue
if mode=='n':
n=HAND_SIZE
hand=dealHand(n)
playHand(hand,wordList,n)
pr+=1
continue
elif person=='c':
if mode=='r':
if pr==0:
print('You have not played a hand yet. Please play a new hand first!')
continue
else:
compPlayHand(hand,wordList,n)
continue
if mode=='n':
n=HAND_SIZE
hand=dealHand(n)
compPlayHand(hand,wordList,n)
pr+=1
continue
#
# Build data structures used for entire session and play game
#
if __name__ == '__main__':
wordList = loadWords()
playGame(wordList)
网友评论