IndexError: index 3 is out of bounds for axis 0 with size 3 pygame

Viewed 142

working on a tic tac toe game, since I'm new to pygame, I don't know much so I'm using this project as a way to learn about pygame, anyhow I get this error randomly and don't know how to fix it, I tried looking on google but didn't find anything that I actually understood.

The error I get is;

IndexError: index 3 is out of bounds for axis 0 with size 3
import pygame, sys
import numpy as np
pygame.init()

screen_color = (28, 170, 156)
line_color = (23, 140, 135)
line_width = 9

screen = pygame.display.set_mode((550, 450))
pygame.display.set_caption("Tic Tac Toe")
screen.fill(screen_color)
board = np.zeros((3, 3))


def draw_lines():
    #1st horizontal
    pygame.draw.line(screen, line_color, (0, 135), (550, 135), line_width)
    #2nd horizontal
    pygame.draw.line(screen, line_color, (0, 300), (550, 300), line_width)
    #1st vertical
    pygame.draw.line(screen, line_color, (175, 0), (175, 450), line_width)
    #2nd vertical
    pygame.draw.line(screen, line_color, (370, 0), (370, 450), line_width)

def mark_square(row, col, player):
    board[row][col] = player

def available_square(row, col):
    if board[col][row] == 0:
        return True

    else:
        return False

def is_board_full():
    for row in range(3):
        for col in range(3):
            if board[row][col] == 0:
                return False

print(is_board_full())
print(board)
draw_lines()

player = 1

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            mouseX = event.pos[0] #X coordinate
            mouseY = event.pos[1] #Y coordinate

            clicked_row = int(mouseY // 180)
            clicked_col = int(mouseX // 160)

            #print(clicked_col)
            #print(clicked_row)

            if available_square(clicked_row, clicked_col):
                if player == 1:
                    mark_square(clicked_row, clicked_col, 1)
                    player = 2
                
                elif player == 2:
                    mark_square(clicked_row, clicked_col, 2)
                    player = 1

                print(board)

    pygame.display.update()

1 Answers

The computation of clicked_row and clicked_col is wrong. The problem is that if you click on the right side of the window, the result of mouseX // 160 may be 3.

The grid has 3 rows and 3 columns. The width is 550 and the height is 450. Compute clicked_row and clicked_col as follows:

clicked_row = mouseY * 3 // 450
clicked_col = mouseX * 3 // 550

Or even better:

clicked_row = mouseY * 3 // screen.get_height()
clicked_col = mouseX * 3 // screen.get_width()

The // operator is the floor division operator. So you don't need to convert the result with int.


Additionally you accidentally swapped row and col in the available_square function. Change:

if board[col][row] == 0:

if board[row][col] == 0:
Related