简体   繁体   English

IndexError:检查矩阵内容时列表索引超出范围

[英]IndexError: list index out of range when checking matrix contents

I am currently programming a version of a game called Othello, where you can choose how big the board should be (alternating from 4x4 to 10x10). 我目前正在编写一个名为Othello的游戏版本,您可以在其中选择棋盘的尺寸(从4x4到10x10交替显示)。 Now when I am trying to insert a faulty message as when you input a coordinate that is outside the board area it does not work. 现在,当我尝试插入错误消息时(例如,当您输入板面区域之外的坐标时)不起作用。 At the moment the input as when you place the tile in a nonlegal move works as well as if you input just 1 coordinate or multiple coordinates (it should only be two as one for the x and y-coordinate) 目前,输入和您以非合法移动方式放置图块一样,以及您仅输入1个坐标或输入多个坐标(对于x和y坐标,输入只能是两个)都是可行的。

(Sorry I know I have posted a very similar question but as I then solved this problem, when I continued with my work, the problem have popped up again) (对不起,我知道我已经发布了一个非常类似的问题,但是当我解决了这个问题时,当我继续工作时,该问题再次弹出)

     def isOnBoard(self, x, y):
         return x >= 0 and x <= self.size-1 and y >= 0 and y <= self.size-1 




     def legalMove(self, tile, startX, startY):
 if not self.isOnBoard(startX, startY) == False\
    or self.board[startX][startY] != ' '\
    or not self.isOnBoard(startX, startY):
     return False
     #(lots more down here that checks if the placed move is legal but nonrelevant to the question)


    def playerMove(self,tile): 

        while True:
            move = input().lower()
                if self.legalMove(tile, x, y) == False:
                    print('Wrong input, try again') #<--- checks that the input coordinate is legal
                else:
                    break
            else:
                print('this was wrong try again!.') #<-- checks that input coordinate just consists of two characters
        return [x, y]

if self.board[startX][startY] != ' ' or not self.isOnBoard(startX, startY): IndexError: list index out of range 如果self.board [startX] [startY]!=''还是self.isOnBoard(startX,startY):IndexError:列表索引超出范围

I'd propose you change your legalMove function's definition. 我建议您更改legalMove函数的定义。 You don't need to pass isOnBoard as a parameter (and you don't, so why keep it?): 您不需要将isOnBoard作为参数传递(并且您不需要,为什么要保留它?):

def legalMove(self, tile, startX, startY):

isOnBoard is defined, and checks whether indices are out of bounds or not. 定义了isOnBoard ,并检查索引是否超出范围。 I would recommend making the call to isOnBoard inside legalMove . 我会建议进行调用,以isOnBoard legalMove This way you complete the function, functionally. 这样,您就可以从功能上完成功能。 Here's the proposed changes: 这是建议的更改:

def legalMove(self, tile, startX, startY):
     if not self.isOnBoard(startX, startY)\
        or self.board[startX][startY] != ' '\
        or not self.isOnBoard(startX, startY):
         return False

You need to check self.isOnBoard(startX, startY) before you check the value of self.board[startX][startY] . 检查self.board[startX][startY]的值之前 self.isOnBoard(startX, startY)需要检查self.isOnBoard(startX, startY) If the move is out of bounds (not on the board), then it will throw an exception if you try to access those indeces in the array, which is what you are experiencing. 如果移动超出范围(不在板上),那么如果您尝试访问数组中的这些索引,则会遇到异常,这将引发异常。

Bad version: 错误版本:

if self.board[startX][startY] != ' ' or not self.isOnBoard(startX, startY):

Better version: 更好的版本:

if not self.isOnBoard(startX, startY) or self.board[startX][startY] != ' ':

Because Python employs short-circuiting for conditionals, this is all you need. 由于Python对条件使用短路,因此这就是您所需要的。

The point is that the call to isOnBoard() must come strictly before any attempt to access the list. 关键是必须严格执行对isOnBoard()的调用, 然后才能尝试访问该列表。 If you have an x,y coordinate pair that fails the isOnBoard() condition, that means one or both of the indexes are out of bounds by definition. 如果您的x,y坐标对不符合isOnBoard()条件,则意味着其中一个或两个索引均超出定义范围。 Therefore, any attempt to access the list will most assuredly result in a list index out of range exception. 因此,任何访问列表的尝试都将最有可能导致列表索引超出范围异常。 So if you have a coordinate pair that fails isOnBoard(), then you must not attempt to check the list at those coordinates. 因此,如果您有一个失败于isOnBoard()的坐标对,则一定不要尝试在这些坐标处检查列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM