Kyl*_* Me 1 python oop python-3.x
我很抱歉我的问题没有发表.帮我想一个,我会改变它(如果可能的话).
这就是我想要做的.我会尽量保持简短.
有些村庄在坐标网格中随机产生,(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?')
Run Code Online (Sandbox Code Playgroud)
这打印游戏板.然后我在考虑创建一个看起来像这样的类:
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):
print('It is tribe ' +self.tribe)
def getCoords(self):
print(str(self.coordinates[0])+','+str(self.coordinates[1]))
Run Code Online (Sandbox Code Playgroud)
所以现在是我坚持的部分.如何让玩家可以输入坐标并查看这样的村庄?
您的代码存在一些问题,这些问题会阻止您为问题实施干净的解决方案.
首先,我制作board_x并board_y实际包含整数而不是字符串,因为你在__init__方法中生成随机整数new_village.
>>> 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]
Run Code Online (Sandbox Code Playgroud)
另外,我会在地图上创建一个所有位置的列表,其中没有村庄,但是这样:
locations = [(x,y) for x in board_x for y in board_y]
Run Code Online (Sandbox Code Playgroud)
现在,您的课程代码的关键问题是两个村庄可以在完全相同的位置产生.当发生这种情况并且用户输入坐标时,您如何知道应该打印哪些值?为了防止这种情况,您可以将您传递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'))
Run Code Online (Sandbox Code Playgroud)
既然你已经为你的村庄开了一个班级,你的清单villages实际上应该包含这个班级的实例,即代替
villages = [['5','2'],['5','5'],['8','5']]
Run Code Online (Sandbox Code Playgroud)
你可以发行
villages = [new_village(locations) for i in range(n)]
Run Code Online (Sandbox Code Playgroud)
n您想要的村庄数量在哪里?现在,为了使进一步的查找更容易,我建议创建一个字典,将您的电路板上的位置映射到村庄实例:
villdict = {vill.coordinates:vill for vill in villages}
Run Code Online (Sandbox Code Playgroud)
最后,现在可以轻松处理用户输入并在输入位置打印村庄的值.
>>> inp = tuple(int(x) for x in input('input x,y: ').split(','))
input x,y: 5,4
>>> inp
(5, 4)
Run Code Online (Sandbox Code Playgroud)
您现在可以发出:
if inp in villdict:
chosen = villdict[inp]
print(chosen.name)
print(chosen.tribe)
else:
print('this spot on the map has no village')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
966 次 |
| 最近记录: |