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

新手开发者求助:文本RPG中hitpoints参数赋值异常问题

问题解决与代码优化建议

核心问题:hitpoints始终为0的原因及修复

你的Goblin和Wizard类构造函数中,参数名与类成员变量名完全一致,直接赋值时实际是给参数赋值,而非类的成员变量,导致成员变量保持默认值0。修复方法是用this关键字明确指向类的成员变量:

修正后的Goblin构造函数

public Goblin(int hitpoints, int experienceGiven)
{
    this.hitpoints = hitpoints; // this.hitpoints指向类的成员变量
    this.experienceGiven = experienceGiven;
}

如果希望哥布林初始生命值固定为50,也可以移除构造函数的参数,直接赋值:

public Goblin()
{
    hitpoints = 50;
    experienceGiven = 120;
}

修正后的Wizard构造函数

public Wizard(string _name, string _favoriteSpell, int hitpoints)
{
    spellSlots = 3;
    experience = 0f;
    this.hitpoints = hitpoints; // 修正此处,指向类成员变量
    name = _name;
    favoriteSpell = _favoriteSpell;
    Count++;
}

其他代码优化建议

1. 成员变量封装(面向对象原则)

避免使用public成员变量,改用属性实现封装,防止外部随意修改内部状态:

class Goblin
{
    public int Hitpoints { get; private set; } // 外部只能读,内部可修改
    public int ExperienceGiven { get; private set; }

    public Goblin(int hitpoints, int experienceGiven)
    {
        Hitpoints = hitpoints;
        ExperienceGiven = experienceGiven;
    }
}

2. 随机数生成优化

每次new Random()会因系统时钟精度问题生成重复随机值,全局实例化一个Random对象复用:

class Program
{
    private static readonly Random _random = new Random(); // 全局随机数实例

    static void Main(string[] args)
    {
        // 后续所有随机数用_random.Next()
        int enemyDamage = _random.Next(1, 10);
    }
}

3. 修复战斗循环逻辑

当前while循环是死循环,未修改双方生命值,需补充伤害计算与生命值扣除逻辑:

while (wizard.Hitpoints > 0 && goblin.Hitpoints > 0)
{
    // 哥布林攻击巫师
    int enemyDamage = _random.Next(1, 10);
    wizard.Hitpoints -= enemyDamage;
    Console.WriteLine($"哥布林攻击造成 {enemyDamage} 点伤害,{wizard.Name} 剩余生命值:{wizard.Hitpoints}");

    // 检查巫师是否死亡
    if (wizard.Hitpoints <= 0)
    {
        Console.WriteLine($"{wizard.Name} 被击败,任务失败!");
        break;
    }

    // 巫师施法攻击哥布林
    wizard.CastSpell();
    int fireballDamage = _random.Next(10, 30);
    goblin.Hitpoints -= fireballDamage;
    Console.WriteLine($"火球术造成 {fireballDamage} 点伤害,哥布林剩余生命值:{goblin.Hitpoints}");

    // 检查哥布林是否死亡
    if (goblin.Hitpoints <= 0)
    {
        Console.WriteLine("哥布林被击败,任务完成!");
        wizard.Experience += goblin.ExperienceGiven; // 增加经验值
    }
}

4. 使用字符串插值替代拼接

字符串插值($"...")比+拼接更清晰高效:

// 替换前
Console.WriteLine("The Goblin attacked for " + enemyDamage + " Damage");
// 替换后
Console.WriteLine($"哥布林攻击造成 {enemyDamage} 点伤害");

5. 优化魔法值(spellSlots)逻辑

可以给CastSpell方法返回施法结果,或者在战斗中自动冥想补充魔法:

public bool CastSpell()
{
    if (spellSlots > 0)
    {
        int fireballDamage = _random.Next(10, 30);
        Console.WriteLine($"{name} 施放了 {favoriteSpell}");
        Console.WriteLine($"法术造成 {fireballDamage} 点伤害");
        spellSlots--;
        experience += 0.3f;
        return true;
    }
    else
    {
        Console.WriteLine($"{name} 魔法值耗尽,开始冥想...");
        Meditate();
        return false;
    }
}

完整修正后的代码示例

using System;

namespace ConsoleApp1
{
    class Goblin
    {
        public int Hitpoints { get; set; }
        public int ExperienceGiven { get; private set; }

        public Goblin(int hitpoints, int experienceGiven)
        {
            Hitpoints = hitpoints;
            ExperienceGiven = experienceGiven;
        }

        public void GobAttack(Random random)
        {
            int enemyDamage = random.Next(1, 10);
            Console.WriteLine($"哥布林攻击造成 {enemyDamage} 点伤害");
        }
    }

    class Wizard
    {
        public string Name { get; private set; }
        public string FavoriteSpell { get; private set; }
        private int _spellSlots;
        public float Experience { get; private set; }
        public int Hitpoints { get; set; }
        public static int Count { get; private set; }

        public Wizard(string name, string favoriteSpell, int hitpoints)
        {
            _spellSlots = 3;
            Experience = 0f;
            Hitpoints = hitpoints;
            Name = name;
            FavoriteSpell = favoriteSpell;
            Count++;
        }

        public bool CastSpell(Random random)
        {
            if (_spellSlots > 0)
            {
                int fireballDamage = random.Next(10, 30);
                Console.WriteLine($"{Name} 施放了 {FavoriteSpell}");
                Console.WriteLine($"法术造成 {fireballDamage} 点伤害");
                _spellSlots--;
                Experience += 0.3f;
                return true;
            }
            else
            {
                Console.WriteLine($"{Name} 魔法值耗尽,开始冥想...");
                Meditate();
                return false;
            }
        }

        public void Meditate()
        {
            Console.WriteLine($"{Name} 冥想恢复了所有魔法值");
            _spellSlots = 3;
        }
    }

    class Program
    {
        private static readonly Random _random = new Random();

        static void Main(string[] args)
        {
            string characterName = "John Doe";
            Console.WriteLine("请输入你的角色名称:");
            characterName = Console.ReadLine();

            Wizard wizard = new Wizard(characterName, "火球术", 150);
            Goblin goblin = new Goblin(50, 120);

            Console.WriteLine($"你好,{characterName}。作为巫师,你的第一个任务是消灭肆虐小镇的哥布林。");
            Console.WriteLine("-------------");
            Console.WriteLine($"{characterName} 在哥布林袭击孩童时遭遇了它:");

            while (wizard.Hitpoints > 0 && goblin.Hitpoints > 0)
            {
                // 哥布林攻击
                goblin.GobAttack(_random);
                int enemyDamage = _random.Next(1, 10);
                wizard.Hitpoints -= enemyDamage;
                Console.WriteLine($"{wizard.Name} 剩余生命值:{wizard.Hitpoints}");

                if (wizard.Hitpoints <= 0)
                {
                    Console.WriteLine($"{wizard.Name} 被击败,任务失败!");
                    break;
                }

                // 巫师施法
                bool castSuccess = wizard.CastSpell(_random);
                if (castSuccess)
                {
                    int fireballDamage = _random.Next(10, 30);
                    goblin.Hitpoints -= fireballDamage;
                    Console.WriteLine($"哥布林剩余生命值:{goblin.Hitpoints}");

                    if (goblin.Hitpoints <= 0)
                    {
                        Console.WriteLine("哥布林被击败,任务完成!");
                        wizard.Experience += goblin.ExperienceGiven;
                        Console.WriteLine($"{wizard.Name} 获得了 {goblin.ExperienceGiven} 点经验,当前经验:{wizard.Experience}");
                    }
                }
            }

            Console.ReadKey();
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 19:18:19