使用(x,y)坐标到达特定的“村庄”(python3)

2024-07-04 15:44:44 发布

您现在位置:Python中文网/ 问答频道 /正文

很抱歉我的问题没有涉及这个题目。帮我想一个,我会改变它(如果可能的话)。在

这是我想做的。我会尽量简短明了。在

有些村庄在坐标网格中随机产生(0-9)。每个村庄都有一个等级、坐标和一个随机的村庄名称。在

我已经成功地想出了如何打印游戏板。我被困在玩家能够输入坐标来查看村庄的细节。在

这是我目前掌握的密码。在

def drawing_board():
board_x = '0 1 2 3 4 5 6 7 8 9'.split()
board_y = '1 2 3 4 5 6 7 8 9'.split()
total_list = [board_x]
for i in range(1,10):
    listy = []
    for e in range(0,9):
        if e == 0:
            listy.append(str(i))
        listy.append('.')
    total_list.append(listy)
return total_list
drawing = drawing_board()
villages = [['5','2'],['5','5'],['8','5']] #I would like these to be random 
                                      #and associated with specific villages.
                                      #(read below)
for i in villages:
    x = int(i[1])
    y = int(i[0])
    drawing[x][y] = 'X'

for i in drawing:
    print(i)
print()
print('What village do you want to view?')

这会打印游戏板。然后我想做一个这样的课程:

^{pr2}$

所以现在我被卡住了。 我怎样才能让玩家在这里输入坐标并查看这样一个村庄?在


Tags: inboard游戏forrangelisttotalsplit
3条回答

您可以使用^{}接受用户的字符串。因为你有两个坐标要打进去,也许打两次电话吧。获得字符串后,通过int()将位置转换为整数。在

如果要生成随机位置,可以使用^{}random.randrange()的语法如下:

num = random.randrange(start, stop) #OR
num = random.randrange(start, stop, step)

这将随机生成一个从start到{}的数字,不包括stop本身。这与range()非常相似。第一个方法假定步长为1,而第二个方法可以指定可选的第三个参数,该参数指定随机整数生成的步长。例如,如果start = 2stop = 12和{},这将从[2, 4, 6, 8, 10]的集合中随机生成一个整数。在

您的代码中存在一些问题,这些问题使您无法为您的问题实现一个干净的解决方案。在

首先,我让board_xboard_y实际上包含整数而不是字符串,因为您是在new_village__init__方法中生成随机整数。在

>>> board_x = list(range(10))
>>> board_y = list(range(1,10))
>>> board_x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> board_y
[1, 2, 3, 4, 5, 6, 7, 8, 9]

此外,我会在地图上创建一个列表,列出地图上还没有这样的村庄的所有地点:

^{pr2}$

现在,类代码的关键问题是两个村庄可以在完全相同的位置生成。当发生这种情况并且用户输入坐标时,您如何知道应该打印哪些值?为了防止这种情况,您可以将您的locations传递给__init__方法。在

def __init__(self, locations):
    # sanity check: is the board full?
    if not locations:
        print('board is full!')
        raise ValueError

    # choose random location on the board as coordinates, then delete it from the global list of locations
    self.coordinates = random.choice(locations)
    del locations[locations.index(self.coordinates)]

    # choose name and tribe 
    self.name = 'Random name'
    self.tribe = random.choice(('gauls', 'teutons'))

因为您已经为您的村庄创建了一个类,您的列表villages实际上应该包含该类的实例,即

villages = [['5','2'],['5','5'],['8','5']]

你可以发行

villages = [new_village(locations) for i in range(n)] 

其中n是您想要的村庄数量。 现在,为了便于进一步查找,我建议您创建一个字典,将您板上的位置映射到village实例:

villdict = {vill.coordinates:vill for vill in villages}

最后,现在很容易处理用户输入并在输入位置打印村庄的值。在

>>> inp = tuple(int(x) for x in input('input x,y: ').split(','))
input x,y: 5,4
>>> inp
(5, 4)

现在可以发布:

if inp in villdict:
    chosen = villdict[inp]
    print(chosen.name)
    print(chosen.tribe)
else:
    print('this spot on the map has no village')

一般来说,你的getTribe()函数应该return部落而不是{}它。但是,我要做的主要事情是创建一个单独的函数来测试一组提供的坐标是否与村庄的坐标相同。假设你改变了你的班级:

import random

class new_village():
    def __init__(self):
        self.name = 'Random name'
        x = random.randint(1,9)
        y = random.randint(1,9)
        self.coordinates = [x,y]
        tribe = random.randint(1,2)
        if tribe == 1:
            self.tribe = 'gauls'
        elif tribe == 2:
            self.tribe = 'teutons'

    def getTribe(self): return self.tribe

    def getCoords(self): return self.coordinates

    def areCoordinates(self, x, y):
        if [x, y] == self.coordinates: return True
        else: return False

    def printDetails(self):
        print 'Details for the Tribe are: '
        print '\t Tribe:', self.tribe 
        print '\t Coordinates: ', self.coordinates

你有很多村庄,比如:

^{pr2}$

然后,您可以始终得到与特定坐标相对应的村庄:

In [14]: map(lambda v: v.printDetails() , filter(lambda v: v.getCoords() == [8,6]  , vs))
Details for the Tribe are:
         Tribe: teutons
         Coordinates:  [8, 6]
Out[14]: [None]

如果村庄不在:

In [15]: map(lambda v: v.printDetails() , filter(lambda v: v.getCoords() == [2,3]  , vs))
Out[15]: []

请注意,代码的结构方式可能有多个村庄可以在同一坐标下生成。这可以很容易地用一个谨慎的声明来处理。在

相关问题 更多 >

    热门问题