abhall
abhall

Reputation: 5

list index out of range

I am trying to write a program for connect 4 and this is the function that checks for a winner. When I run the function, I get the error message: list index out of range. How do I fix this?

def winner(board):
    # Check rows for winner
    for c in range(NUM_ROWS): #number of rows
        for r in range(NUM_COLS-3): #number of columns filled to win
            if (board[r][c] == board[r][c + 1] == board[r][c + 2] == board[r][c + 3]) and (board[r][c] != " "):
                return true

    # Check columns for winner
    for c in range(NUM_COLS): #number of rows
        for r in range(NUM_ROWS-3): #number of columns filled to win 
            if (board[r][c] == board[r + 1][c] == board[r + 2][c] ==board[r + 3][c]) and (board[r][c] != " "):
                return true

    # Check diagonal (top-left to bottom-right) for winner
    for c in range(NUM_COLS-3): #number of columns in a winning diagonal 
        for r in range(NUM_ROWS): #number of rows in a winning diagonal
            if (board[r][c] == board[r + 1][c + 1] == board[r + 2][c + 2] == board[r + 3][c + 3]) and (board[r][c] != " "):
                return true


    # Check diagonal (bottom-left to top-right) for winner
    for r in range(NUMC_COLS-3): #columns to make a diagonal
        for c in range(NUM_ROWS-3): #rows to make a diagonal
            if (board[r][c] == board[r - 1][c + 1] == board[r - 2][c + 2] == board[r - 3][c + 3]) and (board[r][c] != " "):
                return true

    # No winner: return the empty string
    return False 

Upvotes: 0

Views: 153

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1122382

You have your rows and columns loop swapped:

for c in range(NUM_ROWS): #number of rows
    for r in range(NUM_COLS-3): #number of columns filled to win
        if (board[r][c] == board[r][c + 1] == board[r][c + 2] == board[r][c + 3]) and (board[r][c] != " "):

Note that c (columns) ranges over NUM_ROWS there, and r (rows) ranges over NUM_COLS.

You do this again in the last nested loop:

for r in range(NUMC_COLS-3): #columns to make a diagonal
    for c in range(NUM_ROWS-3): #rows to make a diagonal
        if (board[r][c] == board[r - 1][c + 1] == board[r - 2][c + 2] == board[r - 3][c + 3]) and (board[r][c] != " "):

But you also misspelled NUMC_COLS so your program never got this far.

In the 3rd block, you are also going out of bounds for the r values:

# Check diagonal (top-left to bottom-right) for winner
for c in range(NUM_COLS-3): #number of columns in a winning diagonal 
    for r in range(NUM_ROWS): #number of rows in a winning diagonal
        if (board[r][c] == board[r + 1][c + 1] == board[r + 2][c + 2] == board[r + 3][c + 3]) and (board[r][c] != " "):
            return true

Here r + 1 will raise an IndexError when you reach the last iteration of the for r in range(NUM_ROWS) loop as you didn't limit that loop to NUM_ROWS - 3.

Upvotes: 2

RichS
RichS

Reputation: 943

You need to fix this block: (see how you're saying r+3...you can't go say, to NUM_ROWS + 3)

for c in range(NUM_COLS-3): #number of columns in a winning diagonal 
    for r in range(NUM_ROWS): #number of rows in a winning diagonal
        if (board[r][c] == board[r + 1][c + 1] == board[r + 2][c + 2] == board[r + 3][c + 3]) and (board[r][c] != " "):
            return true

Upvotes: 1

Related Questions