Advertisement
namereq

Untitled

Jun 8th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.12 KB | None | 0 0
  1. using System;
  2. namespace RockPaperScissors
  3. {
  4.     class Hand
  5.     {
  6.         public const int GU = 1, CHOKI = 2, PA = 3;
  7.         public static readonly string[] hands = { "", "グー", "チョキ", "パー" };
  8.     }
  9.     // ゲーム情報管理用構造体
  10.     public class GameInformation
  11.     {
  12.         // メンバ変数
  13.         public int UserCmd; // ユーザ手
  14.         public int CpuCmd; // CPU手
  15.         public int UserWinCnt; // ユーザ勝利数
  16.         public int CpuWinCnt; // CPU勝利数
  17.         public int GameMaxCnt; // ゲーム回数
  18.         public string UserName; // ユーザ名
  19.         public string ResultMessage; // ゲーム結果メッセージ
  20.                                      // コンストラクタ
  21.         public GameInformation()
  22.         {
  23.             UserCmd = 0;
  24.             CpuCmd = 0;
  25.             UserWinCnt = 0;
  26.             CpuWinCnt = 0;
  27.             GameMaxCnt = 0;
  28.             UserName = string.Empty;
  29.             ResultMessage = string.Empty;
  30.         }
  31.     }
  32.     /// <summary>
  33.     /// MODELBASE:メインロジック抽象化クラス
  34.     /// </summary>
  35.     public abstract class ModelBase
  36.     {
  37.         // メンバ変数
  38.         private Random Rand;
  39.         protected View ViewObj;
  40.         /// <summary>
  41.         /// コンストラクタ
  42.         /// </summary>
  43.         protected ModelBase()
  44.         {
  45.             this.Rand = new System.Random();
  46.         }
  47.         /// <summary>
  48.         /// ゲーム実行
  49.         /// </summary>
  50.         /// <param name="info">ゲーム情報</param>
  51.         public abstract void PlayGame(ref GameInformation info);
  52.         /// <summary>
  53.         /// CPU手取得
  54.         /// </summary>
  55.         /// <return>CPU手</return>
  56.         protected int GetCpuCmd()
  57.         {
  58.             // CPU手の取得
  59.             int cmd = this.GenerateRandomValue(1, 3);
  60.             return cmd;
  61.         }
  62.         /// <summary>
  63.         /// 最小値(min)~最大値(max)で指定された範囲の乱数を生成する
  64.         /// </summary>
  65.         /// <param name="min">最小値</param>
  66.         /// <param name="max">最大値</param>
  67.         /// <return>乱数</return>
  68.         private int GenerateRandomValue(int min, int max)
  69.         {
  70.             // 乱数の生成
  71.             int value = this.Rand.Next(min, max + 1);
  72.             return value;
  73.         }
  74.     }
  75.  
  76.     public class Model : ModelBase
  77.     {
  78.         public Model()
  79.         {
  80.             this.ViewObj = new View();
  81.         }
  82.         /// <summary>
  83.         /// ゲーム実行
  84.         /// </summary>
  85.         /// <param name="info">ゲーム情報管理用構造体</param>
  86.         public override void PlayGame(ref GameInformation info)
  87.         {
  88.             Controller controll = new Controller();
  89.            
  90.             int total = 0;
  91.             info.UserName = controll.UserName();
  92.             total = controll.BattleNumCount();
  93.             while (info.UserWinCnt + info.CpuWinCnt < total)
  94.             {
  95.                 info.CpuCmd = GetCpuCmd();
  96.                 info.UserCmd = controll.UserJyankenHand();
  97.                 PlayJankenGame(ref info);
  98.                 ViewObj.ShowJyankenResult(info.UserName,
  99.                     info.UserCmd, info.CpuCmd, info.UserWinCnt, info.CpuWinCnt);
  100.             }
  101.             ViewObj.ShowFinalResult(info.UserWinCnt, info.CpuWinCnt, info.UserName);
  102.             controll.Wait();
  103.         }
  104.  
  105.         private void PlayJankenGame(ref GameInformation info)
  106.         {
  107.             if (info.CpuCmd == info.UserCmd)
  108.             {
  109.                 ViewObj.ResultDraw();
  110.             }
  111.             else if (info.UserCmd < Hand.GU || info.UserCmd > Hand.PA)
  112.             {
  113.                 info.CpuWinCnt++;
  114.                 ViewObj.ResultFoul(info.UserName, info.UserWinCnt, info.CpuWinCnt);
  115.             }
  116.             else if ((info.CpuCmd == Hand.GU && info.UserCmd == Hand.PA) ||
  117.                 (info.CpuCmd == Hand.CHOKI && info.UserCmd == Hand.GU) ||
  118.                 (info.CpuCmd == Hand.PA && info.UserCmd == Hand.CHOKI))
  119.             {
  120.                 info.UserWinCnt++;
  121.                 ViewObj.ResultWin(info.UserName, info.UserWinCnt, info.CpuWinCnt);
  122.             }
  123.             else
  124.             {
  125.                 info.CpuWinCnt++;
  126.                 ViewObj.ResultLose(info.UserName, info.UserWinCnt, info.CpuWinCnt);
  127.             }
  128.         }
  129.  
  130.     }
  131.  
  132.     public class View
  133.     {
  134.         public void UserNameDisp()
  135.         {
  136.             Console.Write("ユーザ名を入力してください:");
  137.         }
  138.  
  139.         public void BattleNumInput()
  140.         {
  141.             Console.Write("勝負回数を入力してください:");
  142.         }
  143.         public void BattleNumInputWrong()
  144.         {
  145.             Console.WriteLine("入力値が正しくありません。");
  146.         }
  147.  
  148.         public void UserJankenInputRetry()
  149.         {
  150.             Console.Write("再度入力してください。");
  151.         }
  152.  
  153.         public void UserJankenInputHand()
  154.         {
  155.             Console.Write("手を入力してください。(1:グー、2:チョキ、3:パー):");
  156.         }
  157.  
  158.         public void Jyanken(string player, int user, int cpu)
  159.         {
  160.             if (user < 0 || user > 3) user = 0;
  161.             string[] hands = { "", "グー", "チョキ", "パー" };
  162.             Console.WriteLine("{0}:{1} CPU:{2}", player, hands[user], hands[cpu]);
  163.         }
  164.  
  165.         public void ResultDraw()
  166.         {
  167.             Console.WriteLine("DRAW");
  168.         }
  169.         public void ResultFoul(string player, int win, int lose)
  170.         {
  171.             Console.WriteLine("LOSE(反則負け)");
  172.         }
  173.         public void ResultWin(string player, int win, int lose)
  174.         {
  175.             Console.WriteLine("WIN");
  176.         }
  177.         public void ResultLose(string player, int win, int lose)
  178.         {
  179.             Console.WriteLine("LOSE");
  180.         }
  181.  
  182.         public void ShowJyankenResult(string player, int user, int cpu, int win, int lose)
  183.         {
  184.             Console.WriteLine("{0}:{1}勝、CPU:{2}勝", player, win, lose);
  185.             Console.WriteLine();
  186.         }
  187.  
  188.         public void ShowFinalResult(int win, int lose, string player)
  189.         {
  190.             if (win > lose)
  191.             {
  192.                 Console.WriteLine("{0}さんの総合勝利です!", player);
  193.             }
  194.             else if (win == lose)
  195.             {
  196.                 Console.WriteLine("同点です!");
  197.             }
  198.             else
  199.             {
  200.                 Console.WriteLine("CPUさんの総合勝利です!");
  201.             }
  202.         }
  203.     }
  204.  
  205.     class Controller
  206.     {
  207.         private View ViewObj = new View();
  208.  
  209.         public string UserName()
  210.         {
  211.             ViewObj.UserNameDisp();
  212.             string player = Console.ReadLine();
  213.             return player;
  214.         }
  215.  
  216.         public int BattleNumCount()
  217.         {
  218.             int n;
  219.             for (; ; )
  220.             {
  221.                 try
  222.                 {
  223.                     ViewObj.BattleNumInput();
  224.                     n = int.Parse(Console.ReadLine());
  225.                 }
  226.                 catch
  227.                 {
  228.                     ViewObj.BattleNumInputWrong();
  229.                     continue;
  230.                 }
  231.                 break;
  232.             }
  233.             return n;
  234.         }
  235.  
  236.         public int UserJyankenHand()
  237.         {
  238.             int user;
  239.             string hand = null;
  240.             do
  241.             {
  242.                 if (hand != null)
  243.                 {
  244.                     ViewObj.UserJankenInputRetry();
  245.                 }
  246.                 ViewObj.UserJankenInputHand();
  247.                 hand = Console.ReadLine();
  248.             } while (!int.TryParse(hand, out user));
  249.  
  250.             return user;
  251.         }
  252.  
  253.         public void Wait()
  254.         {
  255.             Console.ReadLine();
  256.         }
  257.     }
  258. }
  259.  
  260. namespace jyanken
  261. {
  262.     class Program
  263.     {
  264.         public static void Main(string[] arg)
  265.         {
  266.             RockPaperScissors.Model model = new RockPaperScissors.Model();
  267.             RockPaperScissors.GameInformation info
  268.                 = new RockPaperScissors.GameInformation();
  269.             model.PlayGame(ref info);
  270.         }
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement