我正在尝试使用python创建一种战舰机器人的简单方法,但是我的“射击”功能重复相同的坐标时遇到了一些问题。这是我的一些代码:import numpy as np
import random as rnd
numberlist = [1,3,5,7,9]
numberlist2 = [0,2,4,6,8]
my_shots = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
])def letter_to_column(letter):
"""Takes in lettervalue, and returns what column it corresponds to,
zero-indexed.
Example:
letter_to_column('A') # => 0
"""
return "ABCDEFGHIJ".index(letter.upper())def find_value(coordinatestring):
"""Takes in a coordinatestring in the form "letternumber" and returns wheter it is a boat
in the coordinate or not
"""
letter = coordinatestring[0]
row = int(coordinatestring[1])
col = letter_to_column(bokstav)
value = my_shots[row][col]
return value
这是重复拍摄地点的功能,如果有人对如何使该功能不重复已给出的“字母值”有任何解决方案,我将不胜感激。def shot(letter = "A", number = "0"):
# Chekcs if I already have shot there
while find_value(letter + number) == 1:
letter = rnd.choice("ABCDEFGHIJ")
# Uses the "chess method" by only shooting on every other route
if letter_to_column(letter) % 2 == 0:
number = str(rnd.choice(numberlist2))
else:
number = str(rnd.choice(numberlist))