2017-10-30 7 views
1

私は戦艦ゲームを作っています。このコードは、何隻のボートが残っているかを確認します。私はそれがループのために簡単に行うことができますが、何らかの理由でそれの周りに私の頭をラップすることはできません知っている。これはコードです:if文のこのチェーンを簡単に書く方法は?

#empty Dictonary. This Dict will store the coords of the ship 
boatDict = {'destroyer1': [], 'destroyer2': [], 
      'submarine1': [], 'submarine2': [], 
      'battleship': [], 'carrier': []} 

if boatDict['destroyer1'] == []: 
    destroyer = 2 
elif boatDict['destroyer1'] != []: 
    destroyer = 1 
elif boatDict['destroyer2'] != []: 
    destroyer = 0 

if boatDict['submarine1'] == []: 
    submarine = 2 
elif boatDict['submarine1'] != []: 
    submarine = 1 
elif boatDict['submarine2'] != []: 
    submarine = 0 

if boatDict['battleship'] == []: 
    battleship = 1 
elif boatDict['battleship'] != []: 
    battleship = 0 

if boatDict['carrier'] == []: 
    carrier = 1 
elif boatDict['carrier'] != []: 
    carrier = 0 

ありがとうございます!

EDIT:質問とコード明確

+0

空でないときに、リストには何が入っていますか? –

+1

コード全体を投稿してください。 – yash

+0

三元表記がここでは良いアイデアとなるかどうかは不明です。 –

答えて

1

ブール値、空のリストは偽であり、非空のリストがそうであるよう。さらに、ブール値TrueFalseは実際には、変装の整数1と0です。結果として、簡単に書くことができます

destroyers = sum(bool(boatDict[x]) for x in 'destroyer1', 'destroyer2') 
submarines = sum(bool(boatDict[x]) for x in 'submarine1', 'submarine2') 

battleships = int(bool(boatDict['battleship'])) 
carrier = int(bool(boatDict['carrier'])) 
+0

うわーこれは素晴らしいです!それをオンライナーに変えることは何もない:D – ThomasNL

0

boatListが辞書であると仮定を作られて、あなたはどのように多くの駆逐艦を排除されている得ることができます

destroyersEliminated = 0 
for k, v in boatList.items(): #in py2, use iteritems() 
    if k in ["destroyer1", "destroyer2"] and v != []: 
     destroyersEliminated += 1 

その後、あなたは総数からそれを引くことができます駆逐艦の数:

リストの理解でこれを行うこともできます:

destroyersEliminated = len(k for k in boatList 
    if k in ["destroyer1", "destroyer2"] 
    and boatList[k] != []) 

サイドノート:このボートは、タイプとステータスのオブジェクトであるオブジェクト指向のアプローチを含む、このクリーナーを作成する方法はたくさんあります。それらのリストをループして、タイプとステータスをチェックしてカウントを得ることができます。

2

私はこのように、ボートの数辞書を作成します。

boatDict = {'destroyer1': [3, 5], 'destroyer2': [7, 2], 
      'submarine1': [1, 1], 'submarine2': [], 
      'battleship': [], 'carrier': [5, 2]} 

boatCount = {} 

for key in boatDict: 
    if boatDict[key] != []: 
     boat = ''.join(i for i in str(key) if not i.isdigit()) 
     if boat not in boatCount: 
      boatCount[boat] = 1 
     else: 
      boatCount[boat] += 1 

print(boatCount) 

#output 

{'submarine': 1, 'carrier': 1, 'destroyer': 2} 

だから、空の辞書、ボートの数を作成します。次に、ボットの辞書をループし、空のリストに関連付けられていないすべてのキーについて、ボートという変数を作成します。ボート変数はボートカウント辞書のキーとして使用されます。また、destroyer1やdestroyer2のようなキーから数値を取り除きます。なぜなら新しい辞書ではこれらを結合して単純にdestroyerと呼ぶべきだからです。

次のif文は、新しいボート変数がボートカウントに存在するかどうかをチェックします。そうでない場合は、新しいキーを作成し、そのキーのカウントを1に設定します。キーがすでに存在する場合、elseステートメントはそのキーのカウントに1を加えます。

すべて破壊された船のための0を表示する更新:

boatDict = {'destroyer1': [3, 5], 'destroyer2': [7, 2], 
      'submarine1': [1, 1], 'submarine2': [], 
      'battleship': [], 'carrier': [5, 2]} 

boatCount = {} 

for key in boatDict: 
    boat = ''.join(i for i in str(key) if not i.isdigit()) 
    if boat in boatCount: 
     if boatDict[key] != []: 
      boatCount[boat] += 1 
    else: 
     if boatDict[key] != []: 
      boatCount[boat] = 1 
     else: 
      boatCount[boat] = 0 

print(boatCount) 

#output 

{'destroyer': 2, 'battleship': 0, 'submarine': 1, 'carrier': 1} 
+0

これはトリックを行うことができるように見えます。あなたはこの部分をもう少し説明できますか? 'boat = '' .join(私はstr(キー)のiでないならi。ボート番号: ボートカウント[ボート] = 1 else: ボートカウント[ボート] + = 1' – ThomasNL

+1

@ThomasNL。上記の説明を確認してください。 – kbball

+0

これを行う方法はありますか?boatCountが0の場合は、それも辞書に追加されますか? – ThomasNL

関連する問題