【设计模式】题目小练2

// 题1. AI 对话系统

// 你要做一个 NPC 对话系统:

// 不同 NPC 有不同的对话脚本(村民、商人、战士)。

// 同时对话还会受玩家状态影响(任务是否完成、声望高低)。

// 👉 请结合 模板方法模式 和 策略模式,设计一下系统结构。

cs 复制代码
using System;

// 抽象策略
public interface ISpeakStrategy
{
    string Execute();
}

// 具体策略
public class RespectStrategy : ISpeakStrategy
{
    public string Execute() => "尊敬";
}
public class StrangeStrategy : ISpeakStrategy
{
    public string Execute() => "陌生";
}
public class SeriousStrategy : ISpeakStrategy
{
    public string Execute() => "严肃";
}
public class AppreciateStrategy : ISpeakStrategy
{
    public string Execute() => "赞赏";
}

// 模板方法模式 ------ NPC基类
public abstract class NPC
{
    // 模板方法:定义固定流程
    public void Talk(ISpeakStrategy strategy)
    {
        Greet();
        CoreDialogue(strategy);
        Farewell();
    }

    protected abstract void Greet();                    // 打招呼
    protected abstract void CoreDialogue(ISpeakStrategy strategy);  // 核心对话
    protected abstract void Farewell();                 // 告别
}

// 具体NPC
public class Villager : NPC
{
    protected override void Greet() => Console.WriteLine("村民:你好啊!");
    protected override void CoreDialogue(ISpeakStrategy strategy)
        => Console.WriteLine($"村民用「{strategy.Execute()}」的语气交谈。");
    protected override void Farewell() => Console.WriteLine("村民:路上小心!");
}

public class Merchant : NPC
{
    protected override void Greet() => Console.WriteLine("商人:客官请进。");
    protected override void CoreDialogue(ISpeakStrategy strategy)
        => Console.WriteLine($"商人用「{strategy.Execute()}」的语气讨价还价。");
    protected override void Farewell() => Console.WriteLine("商人:欢迎下次光临!");
}

public class Warrior : NPC
{
    protected override void Greet() => Console.WriteLine("战士:哼,你找我吗?");
    protected override void CoreDialogue(ISpeakStrategy strategy)
        => Console.WriteLine($"战士用「{strategy.Execute()}」的语气交流。");
    protected override void Farewell() => Console.WriteLine("战士:走好,不要添麻烦。");
}

// 玩家状态决定策略
public class Player
{
    public bool IsCompletedTask { get; set; }
    public float Reputation { get; set; }

    public ISpeakStrategy ChooseStrategyByTask()
        => IsCompletedTask ? new SeriousStrategy() : new AppreciateStrategy();

    public ISpeakStrategy ChooseStrategyByReputation()
        => Reputation >= 60f ? new RespectStrategy() : new StrangeStrategy();
}

// 客户端测试
public class Client
{
    public static void Main()
    {
        Player player = new Player { IsCompletedTask = true, Reputation = 80 };

        NPC villager = new Villager();
        NPC merchant = new Merchant();
        NPC warrior = new Warrior();

        villager.Talk(player.ChooseStrategyByReputation());
        merchant.Talk(player.ChooseStrategyByReputation());
        warrior.Talk(player.ChooseStrategyByTask());
    }
}
相关推荐
Yupureki几秒前
从零开始的C++学习生活 1:命名空间,缺省函数,函数重载,引用,内联函数
c语言·开发语言·c++·学习·visual studio
青草地溪水旁11 分钟前
设计模式(C++)详解——策略模式(2)
c++·设计模式·策略模式
鄃鳕19 分钟前
高并发日志项目中,C++IO的使用
开发语言·c++
笨手笨脚の19 分钟前
设计模式-装饰器模式
java·设计模式·装饰器模式·结构型设计模式
点云侠28 分钟前
PCL 生成缺角立方体点云
开发语言·c++·人工智能·算法·计算机视觉
MediaTea1 小时前
Python 库手册:keyword 关键字查询
开发语言·python
睿思达DBA_WGX1 小时前
使用 python-docx 库操作 word 文档(1):文件操作
开发语言·python·word
LateFrames4 小时前
用 【C# + Winform + MediaPipe】 实现人脸468点识别
python·c#·.net·mediapipe
人工干智能7 小时前
科普:Python 中,字典的“动态创建键”特性
开发语言·python
初听于你8 小时前
缓存技术揭秘
java·运维·服务器·开发语言·spring·缓存