You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用字典值替换嵌套列表field中的None以生成僵尸?

问题解决:用字典替换嵌套列表None值生成塔防游戏僵尸

问题描述

作业要求开发一款打印实现的简易塔防游戏,需要利用给定的zombie字典,替换嵌套列表field中的None值来生成游戏中的僵尸,但不清楚具体实现方法。当前代码运行后无法正确显示僵尸的短名称和血量,预期僵尸能随机出现在某条路径中(显示短名称和当前血量)。

用户现有代码

import random

field = [ [None, None, None, None, None, None, None],
          [None, None, None, None, None, None, None],
          [None, None, None, None, None, None, None],
          [None, None, None, None, None, None, None],
          [None, None, None, None, None, None, None] ]


zombie = {'shortform': 'ZOMBI',
          'name': 'Zombie',
          'maxHP': 15,
          'min_damage': 3,
          'max_damage': 6,
          'moves' : 1,
          'reward': 2
          }

num_rows=len(field)
num_columns=len(field[0])
row_indexes="ABCDE"

print(' {:^7}{:^5}{:^7}'.format('1','2','3'))
print(' ',end='')

for column in range(num_columns): 
    print('+-----',end='')
print('+')

for row in range(num_rows):
    print('{}'.format(row_indexes[row]), end='') # print row index
    for column in range(num_columns): #for each column,
        element = field[row][column]      
        if element is None:      #print spacings
            print('|     ' , end='') 
        else:                     #print monster name
            print('|' + element[0], end='')
    print('|') #end row

    print(' ', end='') #spacing for 2nd | row 
    for column in range(num_columns):
        element = field[row][column]
        if element is None:  #print spacing agn
            print('|     ' , end='')
        else:                     #print monster helth
            print('|' + element[1], end='')
    print('|') #end row

    print(' ', end='') #spacing for letter
    for column in range(num_columns):
        print('+-----', end = '')
    print('+')#end of row

row_spawn=random.randint(0,4)
field[row_spawn][5]=zombie.get('shortform')

问题分析与修复方案

现有代码的核心问题是:你把僵尸的短名称字符串直接存入了field,但打印逻辑默认field的非None元素是包含两个元素的序列(取element[0]和element[1]),导致字符串被按字符拆分显示,无法正确展示僵尸信息。

修复步骤:

  1. 存储完整的僵尸信息到field:
    把field[row_spawn][5]赋值为包含短名称和血量的结构,推荐直接存入完整字典,方便后续扩展属性:

    row_spawn=random.randint(0,4)
    field[row_spawn][5] = zombie  # 存入完整僵尸字典
    
  2. 调整打印逻辑匹配存储的结构:
    修改打印部分的else分支,从字典中取对应的键值,并格式化居中显示:

    # 第一行打印僵尸短名称
    else:
        print('|{:^5}'.format(element['shortform']), end='')
    # 第二行打印僵尸血量
    else:
        print('|{:^5}'.format(str(element['maxHP'])), end='')
    

完整修复后的代码

import random

field = [ [None, None, None, None, None, None, None],
          [None, None, None, None, None, None, None],
          [None, None, None, None, None, None, None],
          [None, None, None, None, None, None, None],
          [None, None, None, None, None, None, None] ]


zombie = {'shortform': 'ZOMBI',
          'name': 'Zombie',
          'maxHP': 15,
          'min_damage': 3,
          'max_damage': 6,
          'moves' : 1,
          'reward': 2
          }

num_rows=len(field)
num_columns=len(field[0])
row_indexes="ABCDE"

# 随机生成僵尸位置
row_spawn=random.randint(0,4)
field[row_spawn][5] = zombie

print(' {:^7}{:^5}{:^7}'.format('1','2','3'))
print(' ',end='')

for column in range(num_columns): 
    print('+-----',end='')
print('+')

for row in range(num_rows):
    print('{}'.format(row_indexes[row]), end='') # 打印行索引
    for column in range(num_columns):
        element = field[row][column]      
        if element is None:
            print('|     ' , end='') 
        else:
            print('|{:^5}'.format(element['shortform']), end='')
    print('|')

    print(' ', end='')
    for column in range(num_columns):
        element = field[row][column]
        if element is None:
            print('|     ' , end='')
        else:
            print('|{:^5}'.format(str(element['maxHP'])), end='')
    print('|')

    print(' ', end='')
    for column in range(num_columns):
        print('+-----', end = '')
    print('+')

效果说明

修复后,僵尸会随机出现在某一行的第6列,单元格内会居中显示僵尸的短名称ZOMBI和初始血量15,符合预期的塔防网格显示效果。

内容的提问来源于stack exchange,提问作者user19198861

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 15:27:49