0

现在我正在尝试将战舰棋盘游戏作为练习,并且我大部分时间都在使用带有一些杂项独立功能的类。下面的一切都是独立的。我无法完全理解它,如果我按列表中从最后一个到第一个的顺序攻击船只,这会起作用,但如果你以任何其他顺序进行,它会破坏说列表索引不存在。请帮忙。

基本上我的系统是每当一艘船被“击中”(移动与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

我做错了吗?我应该物理删除列表吗?

谢谢

4

4 回答 4

1

答案与使用 for-loop 的问题 Delete item in a list相同:

向后迭代:

for j in range(len(shipList) - 1, -1, -1):
于 2012-08-01T11:54:46.287 回答
0

如果我理解得很好,你hitlist包含所有命中(意思是,你不会在每次移动时检查):如果是这样,gecco 的症状是正确的:你不能在迭代列表时删除列表中的元素(这会使索引无效)。但是颠倒列表并不能解决这个问题,因为如果你从第一艘到最后一艘沉船,你也会遇到同样的问题。

如果您不想过多地更改代码,请替换del shipList[j]shipList[j][0] = None(您不删除列表元素,因此迭代仍然有效),然后重新定义函数isGameOver

def isGameOver(shiplist):
    ret = True
    for ship in shiplist:
        if shiplist[0] is not None:
            ret = False
            break
    return ret
于 2012-08-01T12:11:56.863 回答
0

您遇到的错误已在@gecco 答案中进行了解释。

避免使用嵌套循环可以使代码易于理解。

例如,以下函数是错误的,因为它只会检查列表中的第一个元素。

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

它可以写成

def listIsEmpty(alist):
    return not any(alist)

和 checkForSunk 函数

#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]

可以写成

# sometimes use filter can make thing easier.
shipList = [k for k in shipList if not listIsEmpty(k[0])]
于 2012-08-01T12:52:26.520 回答
-1

对不起,我无法为您提供解决方案代码,因为您没有提供足够的信息或代码让我为您修复。但我提供的代码示例可能有助于您理解这些列表。

#a list with 10 object
mylist = [1,2,3,4,5,6,7,8,9,10]
print mylist
>>> 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#if i print the 6th object:
print mylist[5]
>>> 
6
#if i delete an object from the end:
del mylist[9]
print mylist
>>>
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#as you can see, the item is gone, and the list only has 9 objects.

#if i print the 6th object:
print mylist[5]
>>> 
6

#but if i delete an item from the middle
del mylist[4]
print mylist
>>>
[1, 2, 3, 4, 6, 7, 8, 9]
#i now have 8 objects as expected, but the objects location in the list has changed.

#if i print the 6th object:
print mylist[5]
>>> 
7

我希望这有帮助

于 2012-08-01T11:45:27.683 回答