namereq

RockPaperScissors201807271313

Jul 26th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.88 KB | None | 0 0
  1. // Program.cs 最初に実行されるファイル
  2. using System;
  3.  
  4. namespace RockPaperScissors
  5. {
  6.     class Program
  7.     {
  8.         public static void Main(string[] arg)
  9.         {
  10.             new Controller().Start();
  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.             control.GetGameMaxCount(ref total);
  125.             while (info.UserWinCnt + info.CpuWinCnt < total)
  126.             {
  127.                 info.CpuCmd = GetCpuCmd();
  128.                 control.GetUserCmd(ref info.UserCmd);
  129.                 PlayJankenGame();
  130.                 view.ShowCurrentGameResult(info);
  131.             }
  132.  
  133.             control.End(info);
  134.            
  135.         }
  136.        
  137.         private void PlayJankenGame()
  138.         {
  139.             if (info.CpuCmd == info.UserCmd)
  140.             {
  141.                 view.UpdateMessage("DRAW");
  142.             }
  143.             else if (info.UserCmd < Hand.GU || info.UserCmd > Hand.PA)
  144.             {
  145.                 info.CpuWinCnt++;
  146.                 view.UpdateMessage("LOSE(反則負け)");
  147.             }
  148.             else if ((info.CpuCmd == Hand.GU && info.UserCmd == Hand.PA) ||
  149.                 (info.CpuCmd == Hand.CHOKI && info.UserCmd == Hand.GU) ||
  150.                 (info.CpuCmd == Hand.PA && info.UserCmd == Hand.CHOKI))
  151.             {
  152.                 info.UserWinCnt++;
  153.                 view.UpdateMessage("WIN");
  154.             }
  155.             else
  156.             {
  157.                 info.CpuWinCnt++;
  158.                 view.UpdateMessage("LOSE");
  159.             }
  160.         }
  161.     }
  162.  
  163.     /// <summary>
  164.     ///  VIEWBASE:画面表示抽象化クラス
  165.     /// </summary>
  166.     public abstract class ViewBase
  167.     {
  168.         /// <summary>
  169.         ///  コンストラクタ
  170.         /// </summary>
  171.         protected ViewBase()
  172.         {
  173.         }
  174.  
  175.         /// <summary>
  176.         ///  1行メッセージ更新
  177.         /// </summary>
  178.         /// <param name="message">メッセージ</param>
  179.         public void UpdateMessage(string message)
  180.         {
  181.             Console.WriteLine(message);
  182.         }
  183.  
  184.         /// <summary>
  185.         ///  文字列メッセージ更新
  186.         /// </summary>
  187.         /// <param name="message">メッセージ</param>
  188.         public void UpdateStringMessage(string message)
  189.         {
  190.             Console.Write(message);
  191.         }
  192.  
  193.         /// <summary>
  194.         ///  1ゲーム結果表示
  195.         /// </summary>
  196.         /// <param name="info">ゲーム情報</param>
  197.         public abstract void ShowCurrentGameResult(GameInformation info);
  198.  
  199.         /// <summary>
  200.         ///  総合結果表示
  201.         /// </summary>
  202.         /// <param name="info">ゲーム情報</param>
  203.         public abstract void ShowTotalGameResult(GameInformation info);
  204.  
  205.     }
  206.  
  207.     //画面に文字列を表示するメソッド群をまとめたクラス
  208.     public class View : ViewBase
  209.     {
  210.         /// <summary>
  211.         ///  1ゲーム結果表示
  212.         /// </summary>
  213.         /// <param name="info">ゲーム情報</param>
  214.         public override void ShowCurrentGameResult(GameInformation info)
  215.         {
  216.             if (info.UserCmd < 0 || info.UserCmd > 3)
  217.                 info.UserCmd = 0;
  218.  
  219.             UpdateMessage(string.Format("CPU:{0}", Hand.hands[info.CpuCmd]));
  220.             UpdateMessage(string.Format("{0}:{1}勝、CPU:{2}勝",
  221.                 info.UserName, info.UserWinCnt, info.CpuWinCnt));
  222.             UpdateMessage("");
  223.         }
  224.  
  225.         /// <summary>
  226.         ///  総合結果表示
  227.         /// </summary>
  228.         /// <param name="info">ゲーム情報</param>
  229.         public override void ShowTotalGameResult(GameInformation info)
  230.         {
  231.             if (info.UserWinCnt > info.CpuWinCnt)
  232.             {
  233.                 UpdateMessage(string.Format("{0}さんの総合勝利です!", info.UserName));
  234.             }
  235.             else if (info.UserWinCnt == info.CpuWinCnt)
  236.             {
  237.                 UpdateMessage("同点です!");
  238.             }
  239.             else
  240.             {
  241.                 UpdateMessage("CPUさんの総合勝利です!");
  242.             }
  243.         }
  244.     }
  245.  
  246.     /// <summary>
  247.     ///  CONTROLLERBASE:ユーザからの入力を受け付ける抽象化クラス
  248.     /// </summary>
  249.     public abstract class ControllerBase
  250.     {
  251.         // メンバ変数
  252.         protected View view;
  253.         protected Model model;
  254.  
  255.         /// <summary>
  256.         ///  コンストラクタ
  257.         /// </summary>
  258.         protected ControllerBase()
  259.         {
  260.             this.view = new View();
  261.         }
  262.  
  263.         /// <summary>
  264.         ///  開始
  265.         /// </summary>
  266.         public abstract void Start();
  267.  
  268.         /// <summary>
  269.         ///  終了処理
  270.         /// </summary>
  271.         public abstract void End(GameInformation info);
  272.  
  273.         /// <summary>
  274.         ///  ユーザ名入力
  275.         /// </summary>
  276.         /// <param name="userName">ユーザ名</param>
  277.         public abstract void GetUserName(ref string userName);
  278.  
  279.         /// <summary>
  280.         ///  勝負回数入力
  281.         /// </summary>
  282.         /// <param name="gameMaxCnt">勝負回数</param>
  283.         public abstract void GetGameMaxCount(ref int gameMaxCnt);
  284.  
  285.         /// <summary>
  286.         ///  ユーザ手入力
  287.         /// </summary>
  288.         /// <param name="userCmd">ユーザ手</param>
  289.         public abstract void GetUserCmd(ref int userCmd);
  290.  
  291.     }
  292.  
  293.     //ユーザから入力を受け付けるメソッド郡をまとめたクラス
  294.     public class Controller : ControllerBase
  295.     {
  296.         /// <summary>
  297.         /// コンストラクタ
  298.         /// </summary>
  299.         public Controller() : base()
  300.         {
  301.             this.model = new Model(this, view);
  302.         }
  303.         /// <summary>
  304.         /// デストラクタ
  305.         /// </summary>
  306.         ~Controller()
  307.         {
  308.             this.model = null;
  309.             this.view = null;
  310.         }
  311.  
  312.         /// <summary>
  313.         /// 開始メソッド
  314.         /// </summary>
  315.         /// <returns>開始するかどうか</returns>
  316.         public override void Start()
  317.         {
  318.             this.model.PlayGame();
  319.            
  320.         }
  321.  
  322.         /// <summary>
  323.         /// 終了メソッド
  324.         /// </summary>
  325.         /// <returns>終了するかどうか</returns>
  326.         public override void End(GameInformation info)
  327.         {
  328.             // 総合結果表示
  329.             view.ShowTotalGameResult(info);
  330.             Environment.Exit(0);
  331.         }
  332.  
  333.         /// <summary>
  334.         /// ユーザ名入力メソッド
  335.         /// </summary>
  336.         /// <param name="userName">ユーザ名</param>
  337.         public override void GetUserName(ref string userName)
  338.         {
  339.             view.UpdateStringMessage("ユーザ名を入力してください:");
  340.             userName = Console.ReadLine();
  341.         }
  342.  
  343.         /// <summary>
  344.         /// 勝負回数入力メソッド
  345.         /// </summary>
  346.         /// <param name="gameMaxCnt">勝負回数</param>
  347.         public override void GetGameMaxCount(ref int gameMaxCnt)
  348.         {
  349.             gameMaxCnt = 1;
  350.             while (true)
  351.             {
  352.                 try
  353.                 {
  354.                     view.UpdateStringMessage("勝負回数を入力してください:");
  355.                     gameMaxCnt = int.Parse(Console.ReadLine());
  356.                     if (gameMaxCnt <= 0) throw new ArithmeticException();
  357.                 }
  358.                 catch
  359.                 {
  360.                     view.UpdateMessage("入力値が正しくありません。");
  361.                     continue;
  362.                 }
  363.                 break;
  364.             }
  365.         }
  366.  
  367.         ///<summary>
  368.         ///ユーザ手入力メソッド
  369.         ///</summary>
  370.         /// <param name="userCmd">ユーザ手</param>
  371.         public override void GetUserCmd(ref int userCmd)
  372.         {
  373.             userCmd = 0;
  374.             while (true)
  375.             {
  376.                 try
  377.                 {
  378.                     view.UpdateStringMessage("手を入力してください(グー1、チョキ2、パー3):");
  379.                     userCmd = int.Parse(Console.ReadLine());
  380.                 }
  381.                 catch
  382.                 {
  383.                     view.UpdateMessage("入力値が正しくありません。");
  384.                     continue;
  385.                 }
  386.                 break;
  387.             }
  388.         }
  389.     }
  390.  
  391.     /// <summary>
  392.     /// じゃんけんの手のクラス
  393.     /// </summary>
  394.     class Hand
  395.     {
  396.         public const int GU = 1, CHOKI = 2, PA = 3;
  397.         public static readonly string[] hands = { "", "グー", "チョキ", "パー" };
  398.     }
  399. }
Advertisement
Add Comment
Please, Sign In to add comment