I'm really new to programming and am trying to create a battleships game using Pygame. My game features and AI vs a player and currently I am struggling on how to put the AI's bombs into place. I've created a function(bombs) that sets the grid[row][column] to 2, where it'll output "Boom" if clicked. It works if I set and individual value to 2 as shown in line 55, but I want the bombs to be set randomly.
The part of my game that deals with the AI's bombs:
import random
def bombs():
for i in range(0,8):
row = random.randint(1,8)
column = random.randint(1,8)
grid[row][column] = 2
print(row,column)
i = i+1
# Create a 2 dimensional array. A two dimensional array is simply a list of lists.
grid = []
for row in range(8):
# Add an empty array that will hold each cell
# in this row
grid.append([])
for column in range(0,8):
grid[row].append(0) # Append a cell
The error:
Traceback (most recent call last):
File "C:\Users\hazza\OneDrive\Desktop\Python\CHECK.py", line 54, in <module>
bombs()
File "C:\Users\hazza\OneDrive\Desktop\Python\CHECK.py", line 9, in bombs
grid[row][column] = 2
IndexError: list index out of range