Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
connect 4 unknown erroer
#1
This is the start of a 4 in a row game / connect 4. I am experiencing the error:

line 10, in is_valid_location
if board[bottom_row][column] == 0:
IndexError: index 5 is out of bounds for axis 0 with size 5


I would really appreciate some help thanks.
import numpy as np
turn = 0
game_over = False

def board_create(column, row):
    board = np.zeros((row,column))
    return board

def is_valid_location(column_choice, board, bottom_row):
    if board[bottom_row][column] == 0:
        return False
    else:
        return True

row = 5
column = int(input("How many columns: "))
board = board_create(column, row)

while not game_over:
    if turn == 0:
        invalid = True
        bottom_row = row
        while invalid == True:
            column = str(column)
            column_choice = int(input("Player 1 make your selection (1-"+column+"):"))
            column = int(column)
            invalid = is_valid_location(column_choice, board, bottom_row)
    else:
        while invalid == True:
            column = str(column)
            column_choice = int(input("Player 2 make your selection (1-"+column+"):"))
            column = int(column)
            invalid = is_valid_location(column_choice, board, bottom_row)
    turn = turn + 1
    turn = turn % 2
Reply
#2
(Aug-16-2019, 10:27 AM)Help_me_Please Wrote: IndexError: index 5 is out of bounds for axis 0 with size 5
axis is with size 5, so indexes are from 0 to 4
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
What does that mean? and so what do I need to change?
Reply
#4
(Aug-16-2019, 10:43 AM)Help_me_Please Wrote: What does that mean? and so what do I need to change?

There are zero-based index and there is finger-based index. Formula for converting:

finger-based index - 1 = zero-based index
zero-based index + 1 = finger-based index

Users count with their fingers, but for Python you need to convert it into zero-based index.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
What do I change the line too, to avoid this issue.
Reply
#6
you take index as input from user and ask for number between 1 and column. Because indexes are zero-based, you need a number between 0 and column-1. It's up to you how you will achieve that - ask the user or post-process the user input before using it in your code that raise IndexError.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Note also that the function is_valid_location can be simplified to, e.g. return not board[bottom_row][column] == 0. Since the expression board[bottom_row][column] == 0 already evaluates to a boolean, the if and else are unnecessary.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020