Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program.cs 最初に実行されるファイル
- using System;
- namespace RockPaperScissors
- {
- class Program
- {
- public static void Main(string[] arg)
- {
- new Controller().Start();
- }
- }
- // ClassFile.cs MVCクラスがRockPaperScissors名前空間で用意されたクラスファイル
- // ゲーム情報管理用構造体
- public class GameInformation
- {
- // メンバ変数
- public int UserCmd; // ユーザ手
- public int CpuCmd; // CPU手
- public int UserWinCnt; // ユーザ勝利数
- public int CpuWinCnt; // CPU勝利数
- public int GameMaxCnt; // ゲーム回数
- public string UserName; // ユーザ名
- public string ResultMessage; // ゲーム結果メッセージ
- // コンストラクタ
- public GameInformation()
- {
- UserCmd = 0;
- CpuCmd = 0;
- UserWinCnt = 0;
- CpuWinCnt = 0;
- GameMaxCnt = 0;
- UserName = string.Empty;
- ResultMessage = string.Empty;
- }
- }
- /// <summary>
- /// MODELBASE:メインロジック抽象化クラス
- /// </summary>
- public abstract class ModelBase
- {
- // メンバ変数
- /// <summary>
- /// ランダムクラス
- /// </summary>
- private Random Rand;
- /// <summary>
- /// ゲーム情報
- /// </summary>
- protected GameInformation info;
- /// <summary>
- /// コントローラークラス
- /// </summary>
- protected Controller control;
- /// <summary>
- /// 画面表示クラス
- /// </summary>
- protected View view;
- /// <summary>
- /// コンストラクタ
- /// </summary>
- protected ModelBase()
- {
- this.Rand = new System.Random();
- this.info = new GameInformation();
- }
- /// <summary>
- /// コンストラクタ
- /// </summary>
- protected ModelBase(Controller control, View view) : this()
- {
- this.control = control;
- this.view = view;
- }
- /// <summary>
- /// ゲーム実行
- /// </summary>
- public abstract void PlayGame();
- /// <summary>
- /// CPU手取得
- /// </summary>
- /// <return>CPU手</return>
- protected int GetCpuCmd()
- {
- // CPU手の取得P
- int cmd = this.GenerateRandomValue(1, 3);
- return cmd;
- }
- /// <summary>
- /// 最小値(min)~最大値(max)で指定された範囲の乱数を生成する
- /// </summary>
- /// <param name="min">最小値</param>
- /// <param name="max">最大値</param>
- /// <return>乱数</return>
- private int GenerateRandomValue(int min, int max)
- {
- // 乱数の生成
- int value = this.Rand.Next(min, max + 1);
- return value;
- }
- }
- public class Model : ModelBase
- {
- /// <summary>
- /// コンストラクタ
- /// </summary>
- /// <param name="control">コントローラー</param>
- /// <param name="view">ビュー</param>
- public Model(Controller control,View view) : base(control, view)
- {
- }
- /// <summary>
- /// ゲーム実行
- /// </summary>
- public override void PlayGame()
- {
- int total = 0;
- control.GetUserName(ref info.UserName);
- control.GetGameMaxCount(ref total);
- while (info.UserWinCnt + info.CpuWinCnt < total)
- {
- info.CpuCmd = GetCpuCmd();
- control.GetUserCmd(ref info.UserCmd);
- PlayJankenGame();
- view.ShowCurrentGameResult(info);
- }
- control.End(info);
- }
- private void PlayJankenGame()
- {
- if (info.CpuCmd == info.UserCmd)
- {
- view.UpdateMessage("DRAW");
- }
- else if (info.UserCmd < Hand.GU || info.UserCmd > Hand.PA)
- {
- info.CpuWinCnt++;
- view.UpdateMessage("LOSE(反則負け)");
- }
- else if ((info.CpuCmd == Hand.GU && info.UserCmd == Hand.PA) ||
- (info.CpuCmd == Hand.CHOKI && info.UserCmd == Hand.GU) ||
- (info.CpuCmd == Hand.PA && info.UserCmd == Hand.CHOKI))
- {
- info.UserWinCnt++;
- view.UpdateMessage("WIN");
- }
- else
- {
- info.CpuWinCnt++;
- view.UpdateMessage("LOSE");
- }
- }
- }
- /// <summary>
- /// VIEWBASE:画面表示抽象化クラス
- /// </summary>
- public abstract class ViewBase
- {
- /// <summary>
- /// コンストラクタ
- /// </summary>
- protected ViewBase()
- {
- }
- /// <summary>
- /// 1行メッセージ更新
- /// </summary>
- /// <param name="message">メッセージ</param>
- public void UpdateMessage(string message)
- {
- Console.WriteLine(message);
- }
- /// <summary>
- /// 文字列メッセージ更新
- /// </summary>
- /// <param name="message">メッセージ</param>
- public void UpdateStringMessage(string message)
- {
- Console.Write(message);
- }
- /// <summary>
- /// 1ゲーム結果表示
- /// </summary>
- /// <param name="info">ゲーム情報</param>
- public abstract void ShowCurrentGameResult(GameInformation info);
- /// <summary>
- /// 総合結果表示
- /// </summary>
- /// <param name="info">ゲーム情報</param>
- public abstract void ShowTotalGameResult(GameInformation info);
- }
- //画面に文字列を表示するメソッド群をまとめたクラス
- public class View : ViewBase
- {
- /// <summary>
- /// 1ゲーム結果表示
- /// </summary>
- /// <param name="info">ゲーム情報</param>
- public override void ShowCurrentGameResult(GameInformation info)
- {
- if (info.UserCmd < 0 || info.UserCmd > 3)
- info.UserCmd = 0;
- UpdateMessage(string.Format("CPU:{0}", Hand.hands[info.CpuCmd]));
- UpdateMessage(string.Format("{0}:{1}勝、CPU:{2}勝",
- info.UserName, info.UserWinCnt, info.CpuWinCnt));
- UpdateMessage("");
- }
- /// <summary>
- /// 総合結果表示
- /// </summary>
- /// <param name="info">ゲーム情報</param>
- public override void ShowTotalGameResult(GameInformation info)
- {
- if (info.UserWinCnt > info.CpuWinCnt)
- {
- UpdateMessage(string.Format("{0}さんの総合勝利です!", info.UserName));
- }
- else if (info.UserWinCnt == info.CpuWinCnt)
- {
- UpdateMessage("同点です!");
- }
- else
- {
- UpdateMessage("CPUさんの総合勝利です!");
- }
- }
- }
- /// <summary>
- /// CONTROLLERBASE:ユーザからの入力を受け付ける抽象化クラス
- /// </summary>
- public abstract class ControllerBase
- {
- // メンバ変数
- protected View view;
- protected Model model;
- /// <summary>
- /// コンストラクタ
- /// </summary>
- protected ControllerBase()
- {
- this.view = new View();
- }
- /// <summary>
- /// 開始
- /// </summary>
- public abstract void Start();
- /// <summary>
- /// 終了処理
- /// </summary>
- public abstract void End(GameInformation info);
- /// <summary>
- /// ユーザ名入力
- /// </summary>
- /// <param name="userName">ユーザ名</param>
- public abstract void GetUserName(ref string userName);
- /// <summary>
- /// 勝負回数入力
- /// </summary>
- /// <param name="gameMaxCnt">勝負回数</param>
- public abstract void GetGameMaxCount(ref int gameMaxCnt);
- /// <summary>
- /// ユーザ手入力
- /// </summary>
- /// <param name="userCmd">ユーザ手</param>
- public abstract void GetUserCmd(ref int userCmd);
- }
- //ユーザから入力を受け付けるメソッド郡をまとめたクラス
- public class Controller : ControllerBase
- {
- /// <summary>
- /// コンストラクタ
- /// </summary>
- public Controller() : base()
- {
- this.model = new Model(this, view);
- }
- /// <summary>
- /// デストラクタ
- /// </summary>
- ~Controller()
- {
- this.model = null;
- this.view = null;
- }
- /// <summary>
- /// 開始メソッド
- /// </summary>
- /// <returns>開始するかどうか</returns>
- public override void Start()
- {
- this.model.PlayGame();
- }
- /// <summary>
- /// 終了メソッド
- /// </summary>
- /// <returns>終了するかどうか</returns>
- public override void End(GameInformation info)
- {
- // 総合結果表示
- view.ShowTotalGameResult(info);
- Environment.Exit(0);
- }
- /// <summary>
- /// ユーザ名入力メソッド
- /// </summary>
- /// <param name="userName">ユーザ名</param>
- public override void GetUserName(ref string userName)
- {
- view.UpdateStringMessage("ユーザ名を入力してください:");
- userName = Console.ReadLine();
- }
- /// <summary>
- /// 勝負回数入力メソッド
- /// </summary>
- /// <param name="gameMaxCnt">勝負回数</param>
- public override void GetGameMaxCount(ref int gameMaxCnt)
- {
- gameMaxCnt = 1;
- while (true)
- {
- try
- {
- view.UpdateStringMessage("勝負回数を入力してください:");
- gameMaxCnt = int.Parse(Console.ReadLine());
- if (gameMaxCnt <= 0) throw new ArithmeticException();
- }
- catch
- {
- view.UpdateMessage("入力値が正しくありません。");
- continue;
- }
- break;
- }
- }
- ///<summary>
- ///ユーザ手入力メソッド
- ///</summary>
- /// <param name="userCmd">ユーザ手</param>
- public override void GetUserCmd(ref int userCmd)
- {
- userCmd = 0;
- while (true)
- {
- try
- {
- view.UpdateStringMessage("手を入力してください(グー1、チョキ2、パー3):");
- userCmd = int.Parse(Console.ReadLine());
- }
- catch
- {
- view.UpdateMessage("入力値が正しくありません。");
- continue;
- }
- break;
- }
- }
- }
- /// <summary>
- /// じゃんけんの手のクラス
- /// </summary>
- class Hand
- {
- public const int GU = 1, CHOKI = 2, PA = 3;
- public static readonly string[] hands = { "", "グー", "チョキ", "パー" };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment