I am currently creating the game battleships in Python where a player plays against computer. I have managed to create boards for the both of them and placed ships for the player's board. But I'm stuck on placing the ships randomly using random.randint for the computer board.
I get this error in output: File "python", line 98, in comp_place_ships TypeError: 'function' object is not subscriptable
Here is the code:
#Computer place ships
def comp_place_ships(comp_board, ships):
for i, j in ships.items():
ship_not_placed = True
while ship_not_placed:
ori = random.randint(0,1)
x = random.randint(0,9)
y = random.randint(0,9)
placement = comp_board[x][y]
if ori == 0 and placement == '.':
for k in range(j):
comp_board[x][y] = i
comp_board[x+k][y] = i
ship_not_placed = False
elif ori == 1 and placement == '.':
for k in range(j):
comp_board[x][y] = i
comp_board[x][y+k] = i
ship_not_placed = False
elif ori != 0 or 1 and placement != '.':
print('Invalid choice, please try again.')
I know the part "placement = comp_board[x][y]" is the part that makes the error-code but don't know how to solve it. Im using the variable placement to check if the coordinate is equal to '.' which is the standard value of each cell in the two dimensional list. Are there somebody out there who might have any suggestions on how to solve this?
PS! I know the code is poorly structured and I have not added the other if conditions such as how to not go off board with the ship placement. Will add that when I've tackled this problem =)
Aucun commentaire:
Enregistrer un commentaire