【问题标题】:Index error: "out of bounds" python Othello索引错误:“越界”python Othello
【发布时间】:2020-04-23 04:47:27
【问题描述】:

即使我认为我已经确保它不会发送任何超出范围的内容,我也会收到超出范围的索引错误。我添加了一个打印功能,但它没有打印出 4,因为错误正在抱怨..所以不知道在这里做什么。我的代码中显然还有许多其他缺陷,但这是我现在正在处理的缺陷——在从 move_left 调用的函数 validMove 中。

这是我的代码:

import numpy as np 
size = input ("Welcome to Othello! Please choose size of board: ")

def makeBoard(size):
    board = np.zeros((size, size))
    first = int((size/2)-1)
    second = int(size/2)
    board[first][first] = 1
    board[first][second] = 2
    board[second][first] = 2
    board[second][second] = 1
    print(board)
    return board

def make_a_move(board, player, size):
    accepted = moves_left(board, player, int(size))
    while accepted:
        print("Player", player)
        block = input("Choose where to put chip: ")
        choice_row, choice_col = block[0], block[1]
        if valid_input(choice_row, choice_col, int(size)):
            choice_row, choice_col = int(block[0])-1, int(block[1])-1
            flipList = validMove(choice_row, choice_col, board, player)
        else:
            continue 
        if allowed_input(flipList, choice_row, choice_col, board):
            pass
        else:
            continue
        flipChips(board, flipList, player, choice_row, choice_col)
        return board

def valid_input(choice_row, choice_col, size): 
    try:
        choice_row = int(choice_row)
        choice_col = int(choice_col)
    except:
        print("You need to choose a positive number. ")
        return False
    if choice_row <= size and choice_col <= size: 
        return True
    else:
        print("You need to choose a row and column within the board. ")
        return False 
    return True

def isOnBoard(row, col, size): 
    if 0 <= row <= size-1 and 0 <= col <= size-1:
        return True
    else:
        return False

def allowed_input(flipList, choice_row, choice_col, board):
    if board[choice_row][choice_col] != 0:
        print("There is already a chip at this block. Choose an empty block. ")
        return False
    elif len(flipList) == 0:
        print("This is not a valid move. Place your chip so you can flip others. ")
        return False
    else:
        return True

def moves_left(board, player, size):
    for row_element in range(size): 
        for col_element in range(size):
            flipList = validMove(row_element, col_element, board, player) 
            if len(flipList) != 0:
                return True 
    print("There are no valid moves for player", player, "now. Wait your turn. ")
    return False


def validMove (row, col, board, player):
    #player 1: white or 2: black
    flipList = []
    otherplayer = 2 
    if player == 2:
        otherplayer = 1

    adjacent = (row, col+1) 
    counter = 0
    if isOnBoard(adjacent[0], adjacent[1], int(size)):
        while board[adjacent[0]][adjacent[1]] == otherplayer:
            counter += 1
            adjacent = (row, col+1+counter)
        if isOnBoard(adjacent[0], adjacent[1], int(size)) and board[row][col+counter+1] == player:
            for i in range(counter):
                flipList.append((row, col + i + 1))

    adjacent = (row, col - 1)
    counter = 0
    #if (col-1) != -1:
    if isOnBoard(adjacent[0], adjacent[1], int(size)):
        while board[adjacent[0]][adjacent[1]] == otherplayer:
            counter += 1
            adjacent = (row, col - 1 - counter)
        if isOnBoard(adjacent[0], adjacent[1], int(size)) and board[row][col-counter-1] == player:
            for i in range(counter):
                flipList.append((row, col - i - 1))

    adjacent = (row + 1, col)
    counter = 0
    if isOnBoard(adjacent[0], adjacent[1], int(size)):
        while board[adjacent[0]][adjacent[1]] == otherplayer:
            counter += 1
            adjacent = (row  + 1 + counter, col)
            if isOnBoard(adjacent[0], adjacent[1], int(size)) and board[row  + 1 + counter][col] == player:
                for i in range(counter):
                    flipList.append((row + i + 1, col))

    adjacent = (row - 1, col)
    counter = 0
    if isOnBoard(adjacent[0], adjacent[1], int(size)):
        while board[adjacent[0]][adjacent[1]] == otherplayer:
            counter += 1
            adjacent = (row - 1 - counter, col)
            if isOnBoard(adjacent[0], adjacent[1], int(size)) and board[row - 1 - counter][col] == player:
                for i in range(counter):
                    flipList.append((row - i - 1, col))

    adjacent = (row + 1, col + 1) 
    counter = 0
    if isOnBoard(adjacent[0], adjacent[1], int(size)):
        print(adjacent[0], adjacent[1])
        while board[adjacent[0]][adjacent[1]] == otherplayer: #THIS IS LINE 176
            counter += 1
            adjacent = (row + 1 + counter, col + 1 + counter)
            if isOnBoard(adjacent[0], adjacent[1], int(size)) and board[row + 1 + counter][col + 1 + counter] == player:
                for i in range(counter):
                    flipList.append((row + i + 1, col + i + 1))

    adjacent = (row + 1, col - 1)
    counter = 0
    if isOnBoard(adjacent[0], adjacent[1], int(size)):
        while board[adjacent[0]][adjacent[1]] == otherplayer:
            counter += 1
            adjacent = (row + 1 + counter, col - 1 - counter)
            if isOnBoard(adjacent[0], adjacent[1], int(size)) and board[row + 1 + counter][col - 1 - counter] == player:
                for i in range(counter):
                    flipList.append((row + i + 1, col - i - 1))

    adjacent = (row - 1, col - 1)
    counter = 0
    if isOnBoard(adjacent[0], adjacent[1], int(size)):
        while board[adjacent[0]][adjacent[1]] == otherplayer:
            counter += 1
            adjacent = (row - 1 - counter, col - 1 - counter)
            if isOnBoard(adjacent[0], adjacent[1], int(size)) and board[row - 1 - counter][col - 1 - counter] == player:
                for i in range(counter):
                    flipList.append((row - i - 1, col - i - 1))


    adjacent = (row - 1, col + 1)
    counter = 0
    if isOnBoard(adjacent[0], adjacent[1], int(size)):
        while board[adjacent[0]][adjacent[1]] == otherplayer:
            counter += 1
            adjacent = (row - 1 - counter, col + 1 + counter)
            if isOnBoard(adjacent[0], adjacent[1], int(size)) and board[row - 1 - counter][col + 1 + counter] == player:
                for i in range(counter):
                    flipList.append((row - i - 1, col + i + 1))

    return flipList

def flipChips(board, flipList, player, choice_row, choice_col):
    if player == 2:
        board[choice_row][choice_col] = 2
        for element in flipList:
            board[element[0]][element[1]] = 2
    if player == 1:
        board[choice_row][choice_col] = 1
        for element in flipList:
            board[element[0]][element[1]] = 1
    print(board)
    return board

def countChips(board):
    blackChips = []
    whiteChips = []
    for list in board:
        for brick in list:
            if brick == 2:
                blackChips.append(brick)
            if brick == 1:
                whiteChips.append(brick)
    print("Black Chips:", len(blackChips), "White Chips:", len(whiteChips))
    if len(blackChips)>len(whiteChips):
        winner = "black"
    else:
        winner = "white" 
    return winner


def main():
    board = makeBoard(int(size))
    print("")
    first_player = 2
    second_player = 1
    round = 0
    while moves_left(board, first_player, int(size)) or moves_left(board, second_player, int(size)):
        if round % 2 == 0:
            board = make_a_move(board, first_player, size)
        else:
            board = make_a_move(board, second_player, size)
        round += 1
    winner = countChips(board)
    print("The game is over. The winner is", winner)

main()

This is the error I´m getting:

1 1
1 1
1 2
Traceback (most recent call last):
  File "reversi.py", line 289, in <module>
    main()
  File "reversi.py", line 284, in main
    board = make_a_move(board, second_player, size)
  File "reversi.py", line 41, in make_a_move
    accepted = moves_left(board, player, int(size))
  File "reversi.py", line 99, in moves_left
    flipList = validMove(row_element, col_element, board, player) 
  File "reversi.py", line 176, in validMove
    while board[adjacent[0]][adjacent[1]] == otherplayer:
IndexError: index 4 is out of bounds for axis 0 with size 4

【问题讨论】:

  • 我猜.. board的行数是4,而相邻的[0]也是4。所以当你运行时,它变成board[4][adjacent[1]]这就是为什么超出界限错误。很抱歉,这是一个猜测。我没能花足够的时间处理代码。
  • 是的,但是正如您所看到的,终端打印出:1 1 1 1 1 2 from the print(adjacent[0],similar[1]) from the if-string,这意味着4 永远不会传递到 if 字符串中。所以我不明白为什么它说有任何索引 4。isOnBoard 函数确保任何超出范围的内容都不会传递到索引中。
  • 好的 .. size 的值(作为输入)是什么...以便我可以尝试在此处复制错误? ..换句话说..我怎样才能复制错误?
  • 大小 = 4 :)

标签: python index-error othello


【解决方案1】:

您正在第 175 行打印(直到那里都很好)。之后,您将在第 176 行进入 While 循环,在第 177 行中,您将 Counter 递增 1,在第 178 行,您将在已经递增的 Counter 之上添加 1。因此,当 while 循环进行下一次迭代时,它现在超出了范围。您可以通过在第 178 行之后添加以下行来了解行为。我认为您将能够发现问题。

print(adjacent) ## Add me after line 178

很抱歉,我无法提出更多建议或发现更多问题,因为我不了解游戏的运作方式。 希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2014-01-27
    • 2014-06-06
    • 2015-03-22
    • 2013-10-13
    • 2015-01-10
    • 2017-03-23
    • 2015-02-04
    相关资源
    最近更新 更多