與關於此主題的許多問題不同,我的作業不是功課。我建立了一個工作的Ghost機器人。我的最終目標是建立一個撲克機器人作爲一種愛好,但Ghost似乎是一個更容易開始考慮的遊戲。Python Ghost Bot
我有問題的代碼如下:
def computer_prompt(playerName,word_string):
length_string = len(word_string)
for possibilities in wordlist:
if possibilities[:length_string].lower() == word_string:
if len(possibilities) > 3 and len(possibilities) % 2 != 0:
if check_game.is_valid_word(possibilities[length_string],wordlist):
if not check_game.word_formed(possibilities[:length_string + 1],wordlist):
print(possibilities)
return possibilities[:length_string + 1]
現在,我只希望計算機總是第二,人類總是先走。問題是,雖然電腦幾乎每時每刻都在打我,但有幾次我仍然可以勝過他。例如,如果我演奏「h」,然後他演奏「a」,然後演奏「z」,然後演奏「a」,然後演奏「r」,然後他發出一個錯誤(因爲他不承認失敗:))。
我該如何改變它,讓他知道在這種情況下,我不會說「z」之後再說「a」?很明顯,我可以將這個例子作爲例外編碼,但我想知道這個問題的一般解決方案。一般來說,現在電腦打我,因爲他在決定選擇哪封信之前會查找所有可能的單詞列表,這些單詞將以我的名字結尾。但在「危險」的例子中,他只是被卡住了,我想讓他知道他會卡在前面幾步,這樣他就不會處於第一位...
非常感謝提前!
ADDED 9/27
任何有興趣,下面的代碼似乎有點比我以前有更好的。還不夠完善,但...:
def all_possibilities(word_string, length_string):
possibilities = []
for possibility in wordlist:
if possibility[:length_string].lower() == word_string:
possibilities.append(possibility)
return possibilities
def clear_loser(possibilities):
clear_losers = []
for item in possibilities:
if len(item) % 2 == 0:
clear_losers.append(item)
return clear_losers
def first_n_letters(sub_optimal_computer_possibilities, length_string):
first_n_Letters = []
for item in sub_optimal_computer_possibilities:
first_n_Letters.append(item[:length_string + 1])
return list(set(first_n_Letters))
def existing_Optimal_Move(FIRSTNLETTERS,first_letters_of_clear_losers):
length_sub_opt_list = len(FIRSTNLETTERS)
new_list = []
for item in FIRSTNLETTERS:
if not item in first_letters_of_clear_losers:
new_list.append(item)
return new_list
def computer_prompt(word_string):
length_string = len(word_string)
possibilities = all_possibilities(word_string, length_string)
clear_losers = clear_loser(possibilities) #Create list of words that will end on computer
sub_optimal_computer_possibilities = [x for x in possibilities if x not in clear_losers] #Create list of words that will end on human (including words that might be suboptimal for me because smart human will make it end on me before getting to this word
FIRSTNLETTERS = first_n_letters(sub_optimal_computer_possibilities, length_string)
first_letters_of_clear_losers = first_n_letters(clear_losers, length_string)
optimalMove = existing_Optimal_Move(FIRSTNLETTERS, first_letters_of_clear_losers)
if optimalMove:
print("OPTIMAL MOVE")
for item in optimalMove:
#print(optimalMove)
return item[:length_string + 1]
else:
for item in FIRSTNLETTERS:
#print(FIRSTNLETTERS)
return item[:length_string + 1]
謝謝喬爾。這正是我所期待的! – user7186
如果遇到問題,請留言。 :) –
fyi我剛剛在上面添加了我的代碼,似乎基本上做的伎倆。我看了一大堆Tree實現(非常有幫助,感謝您的建議!)。我發現直覺很有幫助,但是我最終發現自己編寫的這樣一個簡短的函數具有相當的「搜索」和「遍歷」操作似乎可以做到這一點。再次感謝! – user7186