gpt4 book ai didi

python - 打印二维列表的一部分

转载 作者:太空狗 更新时间:2023-10-30 01:10:56 25 4
gpt4 key购买 nike

我必须创建一个迷宫游戏,该游戏接收用户的命令作为输入以玩游戏。我已经编写了迷宫游戏的代码。我想要修改的是在打印给用户时(移动完成后)只显示迷宫的部分。这是我的迷宫:

level = [
["1"," ","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],
["1"," "," ","1","1","1","1","1","1","1"," "," "," "," "," "," "," "," "," "," ","1","1","1","1","1"],
["1"," "," ","1","1","1","1","1","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," "," "," "," "," "," ","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," "," "," "," "," "," ","1","1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," "," "," ","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1","1","1"," "," "," "," "," "," ","1"],
["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"," ","1"]
]

start_maze = level[0][1] #start of maze
end_maze = level[9][23] #end of maze

这样的输出:

The initial configuration of the maze is:
1 X 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

我如何才能让玩家在四处移动时只能看到迷宫的一部分,而看不到尽头(这很容易做到)?

例如,要有这样的 View :

    1   1 1 1
1 1 1
1 X 1 1
1
1

即这样他在每个方向上只能看到另外 2 个细胞。

我希望我把问题说清楚了。如果需要的话,下面是主要游戏的代码部分:

player = {'y': 0, 'x': 1}
level[player['y']][player['x']] = 'X'

# Translate keywords into coordinate changes
move_modifications = {'UP': {'y': -1, 'x': 0},
'DOWN': {'y': 1, 'x': 0},
'LEFT': {'y':0, 'x': -1},
'RIGHT': {'y': 0, 'x': 1}}

def player_move(maze):

# Main game loop
play = True
while play:
move = input("Please enter a command (LEFT/RIGHT/UP/DOWN): ")
move = move.upper()

coords = move_modifications[move]

new_y = player['y'] + coords['y']
new_x = player['x'] + coords['x']

#Catch them if they try to leave the map
try:
maze_position = maze[new_y][new_x]
except IndexError:
print("Not on map")
continue

if maze_position != '1':
# Move on the map
maze[player['y']][player['x']] = ' '
maze[new_y][new_x] = 'X'

# Update player coords
player['y'] = new_y
player['x'] = new_x

# Print result
print_level(maze)

其余代码只是四处移动,不包括迷宫的任何打印。

最佳答案

NumPy数组可以方便地对二维列表进行切片。考虑下面的代码和切片。

# load numpy from its module
import numpy as np

# create a numpy array
np_level = np.array(level)

# slice the map
slice = np_level[max(new_y-2,0) : new_y+3, \ # y slice
max(new_x-2,0) : new_x+3] # x slice

# print the slice
print(slice)
# or print_level(slice), whichever one suits your case

这需要您的二维数组 level , 创建一个 NumPy 数组 np_level , 然后从 [y-2, y+3) 范围打印它的一部分和 [x-2, x+3) (使用间隔符号)。这意味着它从 y-2 中分割数组至 y+2来自 x-2x+2 包容性

max(y-2, 0)有没有万一y-2低于 0(即负)。使用负 begin-index 和正 end-index 进行切片将返回一个空列表(例如 some_array[-1:1] ==> [] )。 max(x-2, 0)也是如此.我们可以也添加min(y+3, <height-of-level>)min(x+3, <width-of-level>)对于 end 索引,但数组切片已经处理了这些边缘情况——所以这一切都很好。

注意这需要安装 NumPy 模块。如果尚未安装,请输入 python -m pip install numpy 安装它进入命令行/终端。

关于python - 打印二维列表的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55162821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com