Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace RockPaperScissors
- {
- class Hand
- {
- public const int GU = 1, CHOKI = 2, PA = 3;
- public static readonly string[] hands = { "", "グー", "チョキ", "パー" };
- }
- // ゲーム情報管理用構造体
- 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
- {
- // メンバ変数
- private Random Rand;
- protected View ViewObj;
- /// <summary>
- /// コンストラクタ
- /// </summary>
- protected ModelBase()
- {
- this.Rand = new System.Random();
- }
- /// <summary>
- /// ゲーム実行
- /// </summary>
- /// <param name="info">ゲーム情報</param>
- public abstract void PlayGame(ref GameInformation info);
- /// <summary>
- /// CPU手取得
- /// </summary>
- /// <return>CPU手</return>
- protected int GetCpuCmd()
- {
- // CPU手の取得
- 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
- {
- public Model()
- {
- this.ViewObj = new View();
- }
- /// <summary>
- /// ゲーム実行
- /// </summary>
- /// <param name="info">ゲーム情報管理用構造体</param>
- public override void PlayGame(ref GameInformation info)
- {
- Controller controll = new Controller();
- int total = 0;
- info.UserName = controll.UserName();
- total = controll.BattleNumCount();
- while (info.UserWinCnt + info.CpuWinCnt < total)
- {
- info.CpuCmd = GetCpuCmd();
- info.UserCmd = controll.UserJyankenHand();
- PlayJankenGame(ref info);
- ViewObj.ShowJyankenResult(info.UserName,
- info.UserCmd, info.CpuCmd, info.UserWinCnt, info.CpuWinCnt);
- }
- ViewObj.ShowFinalResult(info.UserWinCnt, info.CpuWinCnt, info.UserName);
- controll.Wait();
- }
- private void PlayJankenGame(ref GameInformation info)
- {
- if (info.CpuCmd == info.UserCmd)
- {
- ViewObj.ResultDraw();
- }
- else if (info.UserCmd < Hand.GU || info.UserCmd > Hand.PA)
- {
- info.CpuWinCnt++;
- ViewObj.ResultFoul(info.UserName, info.UserWinCnt, info.CpuWinCnt);
- }
- 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++;
- ViewObj.ResultWin(info.UserName, info.UserWinCnt, info.CpuWinCnt);
- }
- else
- {
- info.CpuWinCnt++;
- ViewObj.ResultLose(info.UserName, info.UserWinCnt, info.CpuWinCnt);
- }
- }
- }
- public class View
- {
- public void UserNameDisp()
- {
- Console.Write("ユーザ名を入力してください:");
- }
- public void BattleNumInput()
- {
- Console.Write("勝負回数を入力してください:");
- }
- public void BattleNumInputWrong()
- {
- Console.WriteLine("入力値が正しくありません。");
- }
- public void UserJankenInputRetry()
- {
- Console.Write("再度入力してください。");
- }
- public void UserJankenInputHand()
- {
- Console.Write("手を入力してください。(1:グー、2:チョキ、3:パー):");
- }
- public void Jyanken(string player, int user, int cpu)
- {
- if (user < 0 || user > 3) user = 0;
- string[] hands = { "", "グー", "チョキ", "パー" };
- Console.WriteLine("{0}:{1} CPU:{2}", player, hands[user], hands[cpu]);
- }
- public void ResultDraw()
- {
- Console.WriteLine("DRAW");
- }
- public void ResultFoul(string player, int win, int lose)
- {
- Console.WriteLine("LOSE(反則負け)");
- }
- public void ResultWin(string player, int win, int lose)
- {
- Console.WriteLine("WIN");
- }
- public void ResultLose(string player, int win, int lose)
- {
- Console.WriteLine("LOSE");
- }
- public void ShowJyankenResult(string player, int user, int cpu, int win, int lose)
- {
- Console.WriteLine("{0}:{1}勝、CPU:{2}勝", player, win, lose);
- Console.WriteLine();
- }
- public void ShowFinalResult(int win, int lose, string player)
- {
- if (win > lose)
- {
- Console.WriteLine("{0}さんの総合勝利です!", player);
- }
- else if (win == lose)
- {
- Console.WriteLine("同点です!");
- }
- else
- {
- Console.WriteLine("CPUさんの総合勝利です!");
- }
- }
- }
- class Controller
- {
- private View ViewObj = new View();
- public string UserName()
- {
- ViewObj.UserNameDisp();
- string player = Console.ReadLine();
- return player;
- }
- public int BattleNumCount()
- {
- int n;
- for (; ; )
- {
- try
- {
- ViewObj.BattleNumInput();
- n = int.Parse(Console.ReadLine());
- }
- catch
- {
- ViewObj.BattleNumInputWrong();
- continue;
- }
- break;
- }
- return n;
- }
- public int UserJyankenHand()
- {
- int user;
- string hand = null;
- do
- {
- if (hand != null)
- {
- ViewObj.UserJankenInputRetry();
- }
- ViewObj.UserJankenInputHand();
- hand = Console.ReadLine();
- } while (!int.TryParse(hand, out user));
- return user;
- }
- public void Wait()
- {
- Console.ReadLine();
- }
- }
- }
- namespace jyanken
- {
- class Program
- {
- public static void Main(string[] arg)
- {
- RockPaperScissors.Model model = new RockPaperScissors.Model();
- RockPaperScissors.GameInformation info
- = new RockPaperScissors.GameInformation();
- model.PlayGame(ref info);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement