現在、私は戦艦ボードゲームを練習として作成しようとしています。ほとんどの場合、いくつかのその他のスタンドアロン関数でクラスを使用しています。以下はすべてスタンドアロンです。これは、リストの最後から最初の順に船を攻撃するとうまくいきますが、他の順序で行くと、リストのインデックスが存在しないと言って壊れます。助けてください。
基本的に私のシステムは、船が「ヒット」するたびに(移動はshipListの位置と同じ位置にあります)、hitListを追加します。これらの関数は、船の既知の位置に対して hitList 内の項目のいずれかがチェックされているかどうかを確認します...船オブジェクトが作成されるときに別のリストに作成されます。私はこれを2日間機能させようとしています
def checkForSunk(shipList, hitList):
#Lists is something like this [[0,1],[0,2],[0,3]]
#ShipList[0] is list, [1] is name of ship
#ShipList is ALL ships.
print 'SHIPLIST : %s' % (shipList)
#[[[[0,1],[0,2],[0,3],[0,4],[0,5],[],[]], 'Destroyer'], [[[0,1],[0,2],[0,3],[0,4],[0,5],[],[]], 'Destroyer'], [[[0,1],[0,2],[0,3],[0,4],[0,5],[],[]], 'Destroyer']]
#[[[0,1],[0,2],[0,3],[0,4],[0,5],[],[]], 'Destroyer']
# 0 1
print 'HITLIST : %s ' % (hitList)
for j in range(len(shipList)):
for i in shipList[j][0]:
if i in hitList:
print 'True in ship # %s' % (shipList[j][1])
del shipList[j][0][shipList[j][0].index(i)] #Delete that part of the ship from the list.
#Check if there's any empty ships, and delete the ship if there are.
for j in range(len(shipList)):
print shipList[j] #Problem around here!!!!!!!!!!!!
if listIsEmpty(shipList[j][0]):
print '%s has been sunk!' % (shipList[j][1])
del shipList[j]
def isGameOver(shiplist):
if shiplist == []:
return True
else:
return False
def listIsEmpty(list):
for i in range(len(list)):
if list[i] != []: #If it finds anything thats not empty, return False. Else true
return False
else:
return True
私はそれについてすべて間違っていますか?リストを物理的に削除する必要がありますか?
ありがとう