2016-08-16 84 views
7

我有一個移動表,決定玩家是否根據他們對AI的選擇獲勝。認爲搖滾,紙,剪刀有更多的動作。 我最終會用Python編寫代碼,但在開始之前,我想知道是否有更好的方法來完成此操作,而不是使用大量的IF語句?更好的替代大量的IF語句?價值表

表看起來是這樣的:

enter image description here

我在想,該舉動將需要分配數字,或類似的東西?我不知道從哪裏開始...

+0

可能創建一個類叫做移動併爲該擊敗原來的動作列表,並那些失去原來的。通過名稱標識的舉動,如果相對移動的名稱是您使用的結果預先定義的列表中的一個 –

回答

8

你可以使用字典嗎?事情是這樣的:

#dict of winning outcomes, the first layer represents the AI moves, and the inner 
#layer represent the player move and the outcome 
ai = { 
    'punch' : { 
     'punch' : 'tie', 
     'kick' : 'wins', 
    }, 
    'stab' : { 
     'punch' : 'loses', 
     'kick' : 'loses' 
    } 
} 

ai_move = 'punch' 
player_move = 'kick' 
print ai[ai_move][player_move] #output: wins 


ai_move = 'stab' 
player_move = 'punch' 
print ai[ai_move][player_move] #output: loses 

我沒有繪製出所有的動作,但你得到的要點

+1

你寫的比我快笑:d @dhdavvie – burakozgul

+1

哈哈你們幸運的是我有我的編輯器中打開,並準備在我的其他屏幕@burakozgul – dhdavvie

+0

@dhdavvie你看起來是迄今爲止最合乎邏輯的 – Jason

0

是,存儲決策,鍵/值對字典中所有可能的組合爲作爲結果的關鍵和決定。基本上爲可能的移動做一個查找表。

這加快了決策制定的速度,而不必存儲所有可能的組合。

def tie(): 
    print("It's a tie!") 

def lose(): 
    print("You lose") 

def win(): 
    print("You win") 
moves = { 
    # player_ai action 
    'punch_punch': tie, 
    'punch_kick': lose, 
    'punch_stab': lose, 
    <..> 
} 
player_move = <move> 
ai_move = <move> 
key = "_".join([player_move, ai_move]) 
if key in moves: 
    # Execute appropriate function 
    return moves[key]() 
raise Exception("Invalid move") 
0

你可以嘗試字典(嵌套字典)的字典。保持文本形式的值和鍵,而不是映射到數字,以提高可讀性。

outcome = {} 
outcome['punch'] = {} 
outcome['punch']['punch'] = 'Tie' 
outcome['punch']['kick'] = 'Lose' 
... 
outcome['kick'] = {} 
outcome['kick']['punch'] = 'Win' 
outcome['kick']['kick'] = 'Tie' 
... 
i_do = 'punch' 
he_does = 'fling' 
... 
if outcome[i_do][he_does] == 'Win': 
    print("Woohoo!") 
+0

作爲對可讀性的評論:這種方法的冗長性使代碼非常難以維護。寫出所有的攻擊,也許再增加幾個,你很快就會意識到,這根本不容易閱讀。 – jurgemaister

+1

「冗長」有助於維護,實際上只有在可以在輔助函數中執行的初始數據填充時才更加冗長。當我進入我的代碼的邏輯時,我寧願使用形式爲'if outcome [i_do] [he_does] =='Win':'than'如果map [attacks.index(「i_do」)] [ attacks.index(「he_does」)] == 1:'。 –

0

你可以使用Python字典來移動映射到數字:

move = {'Punch': 0, 'Kick': 1} 

然後用一個矩陣來決定勝負。 Numpy可用於此

import numpy 
move = {'Punch': 0, 'Kick': 1} 

outcome = numpy.matrix([['Tie','Loses'],['Wins','Tie']]) 


# Punch vs Punch => Tie 
print outcome[move['Punch'], move['Punch']] 

# Punch vs Kick => Punch loses 
print outcome[move['Punch'], move['Kick']] 
1

我會爲此使用2維列表。每次攻擊都被解碼爲一個索引0至5,並將雙贏和損失解碼爲1,0和-1。

所以該列表會是這個樣子(並非基於你的榜樣,我只是把一些隨機數):

table = [[1,0,-1,0,1,-1],[1,1,0,1,0,-1],...,etc.] 

你會找回它是這樣的:

table[0][1] 
5

你可以創建類似於上面的表格的攻擊圖

map = [ 
    [0,-1,-1,1,1,-1], 
    [1,0,-1,-1,1,-1], 
    [1,1,0,-1,-1,1], 
    [-1,1,1,0,-1,1], 
    [-1,-1,1,1,0,-1], 
    [1,1,-1,-1,1,0] 
] 

這裏,0是平局,1是勝利-1是虧損。

現在創建一系列攻擊,其中攻擊的位置與上圖對應。

attacks = ["Punch", "Kick", "Stab", "Throw", "Fling", "Uppercut"] 

現在你可以很容易找到,如果一個攻擊擊敗了另一

map[attacks.index("Stab")][attacks.index("Punch")] 

>>> 1 

刺奪過來衝

+0

注意:這是我第一次編寫python。如果出現問題,請發表評論或只編輯我的帖子。 – jurgemaister

+1

另外,對downvotes的評論非常感謝。 – jurgemaister

+0

海事組織嵌套的字典,文字的鍵/值是比映射值 –