博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
state-game.cs
阅读量:7079 次
发布时间:2019-06-28

本文共 4661 字,大约阅读时间需要 15 分钟。

  using System;
  using System.Collections.Generic;
  namespace StatePattern {
    // State Pattern               D-J Miller and Judith Bishop  Sept 2007
    // Simple game where the context changes the state based on user input
    // Has four states, each with 6 operations
      
    abstract class IState {
      public virtual string Move(Context context) {return "";}
      public virtual string Attack(Context context) {return "";}
      public virtual string Stop(Context context) {return "";}
      public virtual string Run(Context context) {return "";}
      public virtual string Panic(Context context) {return "";}
      public virtual string CalmDown(Context context) {return "";}
    }
    // There are four States
    class RestingState : IState {
      public override string Move(Context context) {
        context.State = new MovingState();
        return "You start moving";
      }
      public override string Attack(Context context) {
        context.State = new AttackingState();
        return "You start attacking the darkness";
      }
      public override string Stop(Context context) {
        return "You are already stopped!";
      }
      public override string Run(Context context) {
        return "You cannot run unless you are moving";
      }
      public override string Panic(Context context) {
        context.State = new PanickingState();
        return "You start Panicking and begin seeing things";
      }
      public override string CalmDown(Context context) {
        return "You are already relaxed";
      }
    }
  class AttackingState : IState {
    public override string Move(Context context) {
      return "You need to stop attacking first";
    }
    public override string Attack(Context context) {
      return "You attack the darkness for " +
           (new Random().Next(20) + 1) + " damage";
    }
    public override string Stop(Context context) {
      context.State = new RestingState();
      return "You are calm down and come to rest";
    }
    public override string Run(Context context) {
      context.State = new MovingState();
      return "You Run away from the fray";
    }
     public override string Panic(Context context) {
       context.State = new PanickingState();
       return "You start Panicking and begin seeing things";
    }
    public override string CalmDown(Context context) {
      context.State = new RestingState();
      return "You fall down and sleep";
    }
  }
  class PanickingState : IState {
    public override string Move(Context context) {
      return "You move around randomly in a blind panic";
    }
    public override string Attack(Context context) {
      return "You start attacking the darkness, but keep on missing";
    }
    public override string Stop(Context context) {
      context.State = new MovingState();
      return "You are start relaxing, but keep on moving";
    }
    public override string Run(Context context) {
      return "You run around in your panic";
    }
    public override string Panic(Context context) {
      return "You are already in a panic";
    }
    public override string CalmDown(Context context) {
      context.State = new RestingState();
      return "You relax and calm down";
    }
  }
  class MovingState : IState {
    public override string Move(Context context) {
      return"You move around randomly";
    }
    public override string Attack(Context context) {
      return"You need to stop moving first";
    }
    public override string Stop(Context context) {
      context.State = new RestingState();
      return"You stand still in a dark room";
    }
    public override string Run(Context context) {
      return"You run around in cirles";
    }
    public override string Panic(Context context) {
      context.State = new PanickingState();
      return"You start Panicking and begin seeing things";
    }
    public override string CalmDown(Context context) {
      context.State = new RestingState();
      return"You stand still and relax";
    }
  }
   
   class Context {
    public IState State {get; set; }
    public void Request(char c) {
      string result;
      switch (char.ToLower(c)) {
        case 'm' : result = State.Move(this);
             break;
        case 'a' : result = State.Attack(this);
            break;
        case 's' : result = State.Stop(this);
            break;
        case 'r' : result = State.Run(this);
           break;
        case 'p' : result = State.Panic(this);
           break;
        case 'c' : result = State.CalmDown(this);
            break;
        case 'e' : result = "Thank you for playing \"The RPC Game\"";
            break;
        default : result = "Error, try again";
           break;
      }
      Console.WriteLine(result);
    }
  }
  static class Program {
    // The user interface
    static void Main () {
      // context.s are States
      // Decide on a starting state and hold onto the Context thus established
      Context context = new Context();
      context.State = new RestingState();
      char command = ' ';
      Console.WriteLine("Welcome to \"The State Game\"!");
      Console.WriteLine("You are standing here looking relaxed!");
      while (char.ToLower(command) != 'e') {
        Console.WriteLine("\nWhat would you like to do now?");
        Console.Write("   Move    Attack    Stop    Run    Panic    CalmDown    Exit the game: ==>");
        string choice;
        do
          choice = Console.ReadLine();
          while (choice==null);
          command = choice[0];
          context.Request(command);
        }
      }
    }
  }

转载地址:http://jucml.baihongyu.com/

你可能感兴趣的文章
Java 面试之技术框架
查看>>
SSM-配置
查看>>
【WiFi】让你的电脑成为无线路由器
查看>>
SQL SERVER 数据库备份
查看>>
日志服务IPython/Jupyter扩展实战:下载数据为Excel文件 ...
查看>>
IBM推出首款独立量子计算机,进一步推动量子计算商业化 ...
查看>>
设置通过Maven创建的工程的JDK版本—一劳永逸
查看>>
蚂蚁金服 SOFAArk 0.6.0 新特性介绍 | 模块化开发容器
查看>>
简单介绍我的开源小工具:SanicDB
查看>>
我做SAP CRM One Order redesign的一些心得体会
查看>>
Confluence 6 升级完成后的检查
查看>>
货拉拉完成D轮3亿美元融资,由高瓴资本、红杉资本领投
查看>>
第二十二章:动画(十)
查看>>
var,let和const深入解析(一)
查看>>
个推微服务网关架构实践
查看>>
分布式系统一致性问题解决实战
查看>>
“十年磨一剑”--有赞的HBase平台实践和应用之路
查看>>
镭速raysync介绍文件传输软件的进史
查看>>
企业可以自己开发OA系统吗?会遇到什么问题?
查看>>
pageadmin CMS网站制作教程:附属表数据列表调用语法
查看>>