本文介绍了如何检查单元是否与现有单元垂直相邻(相邻)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一个简单的游戏,其中放置在嵌套列表中的建筑物必须紧挨着另一座建筑物。我面临的问题是,如果建筑物被放置在侧面,我不能使用for loop来检查现有建筑物,因为它不再是列表的一部分,因为建筑物的某些侧面什么都没有。
board = [['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
['', '', '', '']]
building_list = ['HSE', 'FAC', 'SHP', 'HWY', 'BCH']
输出:
Turn 2
A B C D
+-----+-----+-----+-----+
1| | | | |
+-----+-----+-----+-----+
2| | HSE | | |
+-----+-----+-----+-----+
3| | | | |
+-----+-----+-----+-----+
4| | | | |
+-----+-----+-----+-----+
1. Build a HWY
2. Build a HWY
3. See remaining buildings
4. See current score
5. Save game
0. Exit to main menu
Your choice? 1
Build where? a3
You must build next to an existing building.
我当前的代码:
def build_buildings():
global turn
a = 0
m = 1
j = 1
k = 1
i = 1
if option == 1:
building = option1
else:
building = option2
location = input('Build where? ')
location.split()
col = location[0]
row = int(location[1]) - 1
if col == 'a':
col = 0
elif col == 'b':
col = 1
elif col == 'c':
col = 2
else:
col = 3
if turn > 1:
if col == 0:
m = -1
if row == 0:
j = 0
if col == 3:
k = -1
if row == 3:
i = 0
if (board[int(row + i)][col] or board[int(row - j)][col] or board[int(row)][col + k]
or board[int(row)][col - m]) != '':
board[int(row)][col] = building
else:
board[int(row)][col] = ''
print('You must build next to an existing building.')
print()
turn -= 1
else:
board[int(row)][col] = building
推荐答案
尝试此代码:
def build(place: str, what: int, first_build: bool = False):
if len(place) == 2 and 0 <= what < len(building_list): # check the arguments
x, y = x_axe.get(place[0].upper(), -1), int(place[1]) if place[1].isdigit() else -1 # get the x,y coordinates
# check the conditions to build
if 0 <= x <= width and 0 <= y <= height and board[y][x] == '' and
(first_build or any(
0 <= x + dx <= width and 0 <= y + dy <= height and board[y + dy][x + dx] != '' for dy, dx in
((-1, 0), (0, 1), (1, 0), (0, -1)))):
board[y][x] = building_list[what] # build!
# global variables
board = [['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
['', '', '', '']]
building_list = ['HSE', 'FAC', 'SHP', 'HWY', 'BCH']
x_axe = {'A': 0, 'B': 1, 'C': 2, 'D': 3} # make dict with x indexes for use in build()
# get the max coordinates of the board
height = len(board) - 1
width = len(board[0]) - 1
# simple examples; replace with your code
build('a3', 0, True) # first build with True as third argument
build('b3', 1) # other buildings; will be built
build('d3', 2) # will not be built because of conditions
build('B2', 2) # will be built
build('S9', 3) # will not be built due to wrong symbol and wrong y coordinate
# output the board. first, make the format strings for the table
line = f" {'+-----' * (width + 1)}+" # +-----+-----+-----+-----+
row = f" {'|{:^5}' * (width + 1)}|" # |{:^5}|{:^5}|{:^5}|{:^5}|
header = f" {'{:^6}' * (width + 1)}" # {:^6}{:^6}{:^6}{:^6}
print(header.format(*x_axe.keys()), line, sep='
') # header
for i in range(height + 1):
print(str(i) + row.format(*board[i]), line, sep='
')
输出:
A B C D
+-----+-----+-----+-----+
0 | | | | |
+-----+-----+-----+-----+
1 | | | | |
+-----+-----+-----+-----+
2 | | SHP | | |
+-----+-----+-----+-----+
3 | HSE | FAC | | |
+-----+-----+-----+-----+
这篇关于如何检查单元是否与现有单元垂直相邻(相邻)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!