namereq

RockPaperScissors201807271243

Jul 26th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.06 KB | None | 0 0
  1. using System;
  2.  
  3. namespace RockPaperScissors
  4. {
  5.     class Program
  6.     {
  7.         public static void Main(string[] arg)
  8.         {
  9.             new Controller().Start();
  10.         }
  11.     }
  12.  
  13.  
  14.     // ClassFile.cs MVCクラスがRockPaperScissors名前空間で用意されたクラスファイル
  15.     // ゲーム情報管理用構造体
  16.     public class GameInformation
  17.     {
  18.         // メンバ変数
  19.         public int UserCmd; // ユーザ手
  20.         public int CpuCmd; // CPU手
  21.         public int UserWinCnt; // ユーザ勝利数
  22.         public int CpuWinCnt; // CPU勝利数
  23.         public int GameMaxCnt; // ゲーム回数
  24.         public string UserName; // ユーザ名
  25.         public string ResultMessage; // ゲーム結果メッセージ
  26.         // コンストラクタ
  27.         public GameInformation()
  28.         {
  29.             UserCmd = 0;
  30.             CpuCmd = 0;
  31.             UserWinCnt = 0;
  32.             CpuWinCnt = 0;
  33.             GameMaxCnt = 0;
  34.             UserName = string.Empty;
  35.             ResultMessage = string.Empty;
  36.         }
  37.     }
  38.     /// <summary>
  39.     /// MODELBASE:メインロジック抽象化クラス
  40.     /// </summary>
  41.     public abstract class ModelBase
  42.     {
  43.         // メンバ変数
  44.         /// <summary>
  45.         /// ランダムクラス
  46.         /// </summary>
  47.         private Random Rand;
  48.         /// <summary>
  49.         /// ゲーム情報
  50.         /// </summary>
  51.         protected GameInformation info;
  52.         /// <summary>
  53.         /// コントローラークラス
  54.         /// </summary>
  55.         protected Controller control;
  56.         /// <summary>
  57.         /// 画面表示クラス
  58.         /// </summary>
  59.         protected View view;
  60.         /// <summary>
  61.         /// コンストラクタ
  62.         /// </summary>
  63.         protected ModelBase()
  64.         {
  65.             this.Rand = new System.Random();
  66.             this.info = new GameInformation();
  67.         }
  68.         /// <summary>
  69.         /// コンストラクタ
  70.         /// </summary>
  71.         protected ModelBase(Controller control, View view) : this()
  72.         {
  73.             this.control = control;
  74.             this.view = view;
  75.         }
  76.         /// <summary>
  77.         /// ゲーム実行
  78.         /// </summary>
  79.         public abstract void PlayGame();
  80.         /// <summary>
  81.         /// CPU手取得
  82.         /// </summary>
  83.         /// <return>CPU手</return>
  84.         protected int GetCpuCmd()
  85.         {
  86.             // CPU手の取得P
  87.             int cmd = this.GenerateRandomValue(1, 3);
  88.             return cmd;
  89.         }
  90.         /// <summary>
  91.         /// 最小値(min)~最大値(max)で指定された範囲の乱数を生成する
  92.         /// </summary>
  93.         /// <param name="min">最小値</param>
  94.         /// <param name="max">最大値</param>
  95.         /// <return>乱数</return>
  96.         private int GenerateRandomValue(int min, int max)
  97.         {
  98.             // 乱数の生成
  99.             int value = this.Rand.Next(min, max + 1);
  100.             return value;
  101.         }
  102.     }
  103.  
  104.     public class Model : ModelBase
  105.     {
  106.         /// <summary>
  107.         /// コンストラクタ
  108.         /// </summary>
  109.         /// <param name="control">コントローラー</param>
  110.         /// <param name="view">ビュー</param>
  111.         public Model(Controller control,View view) : base(control, view)
  112.         {
  113.            
  114.         }
  115.         /// <summary>
  116.         /// ゲーム実行
  117.         /// </summary>
  118.         public override void PlayGame()
  119.         {
  120.             int total = 0;
  121.  
  122.             control.GetUserName(ref info.UserName);
  123.  
  124.             while (true)
  125.             {
  126.                 control.GetGameMaxCount(ref total);
  127.                 while (info.UserWinCnt + info.CpuWinCnt < total)
  128.                 {
  129.                     info.CpuCmd = GetCpuCmd();
  130.                     control.GetUserCmd(ref info.UserCmd);
  131.                     PlayJankenGame();
  132.                     view.ShowCurrentGameResult(info);
  133.                 }
  134.  
  135.                 if (control.End(info)) break;
  136.  
  137.                 info.UserWinCnt = 0;
  138.                 info.CpuWinCnt = 0;
  139.             }
  140.         }
  141.        
  142.         private void PlayJankenGame()
  143.         {
  144.             if (info.CpuCmd == info.UserCmd)
  145.             {
  146.                 view.UpdateMessage("DRAW");
  147.             }
  148.             else if (info.UserCmd < Hand.GU || info.UserCmd > Hand.PA)
  149.             {
  150.                 info.CpuWinCnt++;
  151.                 view.UpdateMessage("LOSE(反則負け)");
  152.             }
  153.             else if ((info.CpuCmd == Hand.GU && info.UserCmd == Hand.PA) ||
  154.                 (info.CpuCmd == Hand.CHOKI && info.UserCmd == Hand.GU) ||
  155.                 (info.CpuCmd == Hand.PA && info.UserCmd == Hand.CHOKI))
  156.             {
  157.                 info.UserWinCnt++;
  158.                 view.UpdateMessage("WIN");
  159.             }
  160.             else
  161.             {
  162.                 info.CpuWinCnt++;
  163.                 view.UpdateMessage("LOSE");
  164.             }
  165.         }
  166.     }
  167.  
  168.     /// <summary>
  169.     ///  VIEWBASE:画面表示抽象化クラス
  170.     /// </summary>
  171.     public abstract class ViewBase
  172.     {
  173.         /// <summary>
  174.         ///  コンストラクタ
  175.         /// </summary>
  176.         protected ViewBase()
  177.         {
  178.         }
  179.  
  180.         /// <summary>
  181.         ///  1行メッセージ更新
  182.         /// </summary>
  183.         /// <param name="message">メッセージ</param>
  184.         public void UpdateMessage(string message)
  185.         {
  186.             Console.WriteLine(message);
  187.         }
  188.  
  189.         /// <summary>
  190.         ///  文字列メッセージ更新
  191.         /// </summary>
  192.         /// <param name="message">メッセージ</param>
  193.         public void UpdateStringMessage(string message)
  194.         {
  195.             Console.Write(message);
  196.         }
  197.  
  198.         /// <summary>
  199.         ///  1ゲーム結果表示
  200.         /// </summary>
  201.         /// <param name="info">ゲーム情報</param>
  202.         public abstract void ShowCurrentGameResult(GameInformation info);
  203.  
  204.         /// <summary>
  205.         ///  総合結果表示
  206.         /// </summary>
  207.         /// <param name="info">ゲーム情報</param>
  208.         public abstract void ShowTotalGameResult(GameInformation info);
  209.  
  210.     }
  211.  
  212.     //画面に文字列を表示するメソッド郡をまとめたクラス
  213.     public class View : ViewBase
  214.     {
  215.         /// <summary>
  216.         ///  1ゲーム結果表示
  217.         /// </summary>
  218.         /// <param name="info">ゲーム情報</param>
  219.         public override void ShowCurrentGameResult(GameInformation info)
  220.         {
  221.             if (info.UserCmd < 0 || info.UserCmd > 3)
  222.                 info.UserCmd = 0;
  223.  
  224.             UpdateMessage(string.Format("CPU:{0}", Hand.hands[info.CpuCmd]));
  225.             UpdateMessage(string.Format("{0}:{1}勝、CPU:{2}勝",
  226.                 info.UserName, info.UserWinCnt, info.CpuWinCnt));
  227.             UpdateMessage("");
  228.         }
  229.  
  230.         /// <summary>
  231.         ///  総合結果表示
  232.         /// </summary>
  233.         /// <param name="info">ゲーム情報</param>
  234.         public override void ShowTotalGameResult(GameInformation info)
  235.         {
  236.             if (info.UserWinCnt > info.CpuWinCnt)
  237.             {
  238.                 UpdateMessage(string.Format("{0}さんの総合勝利です!", info.UserName));
  239.             }
  240.             else if (info.UserWinCnt == info.CpuWinCnt)
  241.             {
  242.                 UpdateMessage("同点です!");
  243.             }
  244.             else
  245.             {
  246.                 UpdateMessage("CPUさんの総合勝利です!");
  247.             }
  248.         }
  249.     }
  250.  
  251.     /// <summary>
  252.     ///  CONTROLLERBASE:ユーザからの入力を受け付ける抽象化クラス
  253.     /// </summary>
  254.     public abstract class ControllerBase
  255.     {
  256.         // メンバ変数
  257.         protected View view;
  258.         protected Model model;
  259.  
  260.         /// <summary>
  261.         ///  コンストラクタ
  262.         /// </summary>
  263.         protected ControllerBase()
  264.         {
  265.             this.view = new View();
  266.         }
  267.  
  268.         /// <summary>
  269.         ///  開始
  270.         /// </summary>
  271.         public abstract void Start();
  272.  
  273.         /// <summary>
  274.         ///  終了処理
  275.         /// </summary>
  276.         public abstract bool End(GameInformation info);
  277.  
  278.         /// <summary>
  279.         ///  ユーザ名入力
  280.         /// </summary>
  281.         /// <param name="userName">ユーザ名</param>
  282.         public abstract void GetUserName(ref string userName);
  283.  
  284.         /// <summary>
  285.         ///  勝負回数入力
  286.         /// </summary>
  287.         /// <param name="gameMaxCnt">勝負回数</param>
  288.         public abstract void GetGameMaxCount(ref int gameMaxCnt);
  289.  
  290.         /// <summary>
  291.         ///  ユーザ手入力
  292.         /// </summary>
  293.         /// <param name="userCmd">ユーザ手</param>
  294.         public abstract void GetUserCmd(ref int userCmd);
  295.  
  296.     }
  297.  
  298.     //ユーザから入力を受け付けるメソッド郡をまとめたクラス
  299.     public class Controller : ControllerBase
  300.     {
  301.         /// <summary>
  302.         /// コンストラクタ
  303.         /// </summary>
  304.         public Controller() : base()
  305.         {
  306.             this.model = new Model(this, view);
  307.         }
  308.  
  309.         /// <summary>
  310.         /// 開始メソッド
  311.         /// </summary>
  312.         /// <returns>開始するかどうか</returns>
  313.         public override void Start()
  314.         {
  315.             this.model.PlayGame();
  316.         }
  317.  
  318.         /// <summary>
  319.         /// 終了メソッド
  320.         /// </summary>
  321.         /// <returns>終了するかどうか</returns>
  322.         public override bool End(GameInformation info)
  323.         {
  324.             // 総合結果表示
  325.             view.ShowTotalGameResult(info);
  326.  
  327.             string yesno;
  328.             while (true)
  329.             {
  330.                 view.UpdateStringMessage("終了しますか?(y/n) :");
  331.                 yesno = Console.ReadLine();
  332.                 if (yesno == "y" || yesno == "n") break;
  333.             }
  334.             return yesno == "y";
  335.         }
  336.  
  337.         /// <summary>
  338.         /// ユーザ名入力メソッド
  339.         /// </summary>
  340.         /// <param name="userName">ユーザ名</param>
  341.         public override void GetUserName(ref string userName)
  342.         {
  343.             view.UpdateStringMessage("ユーザ名を入力してください:");
  344.             userName = Console.ReadLine();
  345.         }
  346.  
  347.         /// <summary>
  348.         /// 勝負回数入力メソッド
  349.         /// </summary>
  350.         /// <param name="gameMaxCnt">勝負回数</param>
  351.         public override void GetGameMaxCount(ref int gameMaxCnt)
  352.         {
  353.             gameMaxCnt = 1;
  354.             while (true)
  355.             {
  356.                 try
  357.                 {
  358.                     view.UpdateStringMessage("勝負回数を入力してください:");
  359.                     gameMaxCnt = int.Parse(Console.ReadLine());
  360.                     if (gameMaxCnt <= 0) throw new ArithmeticException();
  361.                 }
  362.                 catch
  363.                 {
  364.                     view.UpdateMessage("入力値が正しくありません。");
  365.                     continue;
  366.                 }
  367.                 break;
  368.             }
  369.         }
  370.  
  371.         ///<summary>
  372.         ///ユーザ手入力メソッド
  373.         ///</summary>
  374.         /// <param name="userCmd">ユーザ手</param>
  375.         public override void GetUserCmd(ref int userCmd)
  376.         {
  377.             userCmd = 0;
  378.             while (true)
  379.             {
  380.                 try
  381.                 {
  382.                     view.UpdateStringMessage("手を入力してください(グー1、チョキ2、パー3):");
  383.                     userCmd = int.Parse(Console.ReadLine());
  384.                 }
  385.                 catch
  386.                 {
  387.                     view.UpdateMessage("入力値が正しくありません。");
  388.                     continue;
  389.                 }
  390.                 break;
  391.             }
  392.         }
  393.     }
  394.  
  395.     /// <summary>
  396.     /// じゃんけんの手のクラス
  397.     /// </summary>
  398.     class Hand
  399.     {
  400.         public const int GU = 1, CHOKI = 2, PA = 3;
  401.         public static readonly string[] hands = { "", "グー", "チョキ", "パー" };
  402.     }
  403. }
Advertisement
Add Comment
Please, Sign In to add comment