Python战舰随机数生成器
我对Python中的类相当陌生。在编写战舰游戏的过程中,我遇到了一个问题,即为计算机的战舰位置和计算机的攻击坐标选择随机的x,y坐标。我不知道是将随机数作为一个函数中的局部变量生成,还是作为类属性或实例属性生成 最初我想创建一个实例属性(如下所示),但我发现rand_x没有定义。我尝试创建一个生成随机数的战列舰函数,但每次调用时它都返回相同的一对坐标。这样做的唯一方法是为随机数创建局部变量吗?因为我将不止一次地使用随机生成器,所以不必重复那个代码就好了 谢谢你的耐心 编辑:我在随机性函数中加入了更多的代码,并将随机性函数中的Python战舰随机数生成器,python,Python,我对Python中的类相当陌生。在编写战舰游戏的过程中,我遇到了一个问题,即为计算机的战舰位置和计算机的攻击坐标选择随机的x,y坐标。我不知道是将随机数作为一个函数中的局部变量生成,还是作为类属性或实例属性生成 最初我想创建一个实例属性(如下所示),但我发现rand_x没有定义。我尝试创建一个生成随机数的战列舰函数,但每次调用时它都返回相同的一对坐标。这样做的唯一方法是为随机数创建局部变量吗?因为我将不止一次地使用随机生成器,所以不必重复那个代码就好了 谢谢你的耐心 编辑:我在随机性函数中加入了
size
替换为self.size
例如,战列舰(4,2,0,0)可能会给我一个[[2,1],[2,1]]的hitlist
,我想要随机的hitlist
import random
hitlist=[]; #a global variable
class Battleship(object):
""" Ship object container. A game where the user tries to destroy the enemy's ships User tries to guess computer's position x and y """
def __init__(self, size, numberships,position_x,position_y):
self.position_x=position_x
self.position_y=position_y
self.numberships=numberships
self.size = size
def plotships(self,r):
"""input is integer coordinates for ships and output is an array of arrays with battleship locations CREATES THE HITLIST DONT REPEAT"""
print('plotships function running')
for i in range(self.numberships):
hitlist.append(r) #random number from function randomness
print(hitlist)
return hitlist
def randomness(self):
rand_x=random.choice(range(self.size))
rand_y=random.choice(range(self.size))
randcoord=[rand_x,rand_y]
return randcoord
#Game Interface
size=int(input('Gameboard size'))
numberships=int(input('Waiting for Number of enemy ships'))
b=Battleship(size,numberships,0,0)
random=b.randomness() #create a random x y coordinate
b.plotships(random) #create a hitlist
要获得随机数,您可以导入随机库 您可以使用它在类的实例上初始化(X,Y)坐标
import random
Battleship(size, numberships, random.randint(0, WIDTH), random.randint(0, HEIGHT))
我假设你有屏幕的宽度和高度。希望能有帮助 我想这是因为你用
size
调用random.choice
,而不是self.size
i、 e
另外,您在哪里定义self.rand
?当然你在打印的时候遇到了问题
编辑-回应以下评论:
如果希望用独立随机数对的self.numberships
对填充hitlist
,则将绘图方法写入如下:
def plotships(self):
"""input is integer coordinates for ships and output is an
array of arrays with battleship locations CREATES THE
HITLIST DONT REPEAT"""
print('plotships function running')
for i in range(self.numberships):
hitlist.append(randomness()) #random number from function randomness
print(hitlist)
return hitlist
确保导入了随机
。尝试使用random.randint(0,self.size-(wtv是您的x范围)
和random.randint(0,self.size-(wtv是您的y范围)
你需要在你的问题中包含一个。目前,我们判断错误的唯一方法是猜测,这几乎肯定是错误的。@Imtiaz Raqib这是对这个问题的%100过度思考,但你可以使用更安全的随机数生成器调用(即:os.uradom
)。只是不想看到敌人预测你舰队上将的位置@st4rgut。你能问一下你期望/想要从b.plotships(random)得到什么样的输出吗?现在写的是,你正在生成一个大小为2的列表R,其中两个随机数的范围为0-self.size;然后你正在创建(并在每个增量上打印)一个大小self.numberships的列表;其中的元素都将是R。感谢您指出这一点。我删除了剩余的代码self.rand。当我使用self.size时,我认为它仍然引用相同的实例并打印出相同的随机数对。确实,似乎是问题的主要原因。谢谢我不太理解您的专业现在是个问题。hitlist应该有不同的独立随机数对吗?是的,我应该说hitlist应该返回随机数对
def plotships(self):
"""input is integer coordinates for ships and output is an
array of arrays with battleship locations CREATES THE
HITLIST DONT REPEAT"""
print('plotships function running')
for i in range(self.numberships):
hitlist.append(randomness()) #random number from function randomness
print(hitlist)
return hitlist