From 94cf096d4fab6e7e46675d18f65f47cc2acdf223 Mon Sep 17 00:00:00 2001 From: Shuhei Nomura Date: Wed, 5 Apr 2017 09:42:42 +0900 Subject: [PATCH] Initial commit. --- RPS.sln | 20 ++++++++ RPS/AIEnemy.cs | 28 ++++++++++ RPS/AIPlayer.cs | 46 +++++++++++++++++ RPS/Hand.cs | 23 +++++++++ RPS/IAI.cs | 9 ++++ RPS/Judge.cs | 65 ++++++++++++++++++++++++ RPS/Player.cs | 21 ++++++++ RPS/Program.cs | 93 ++++++++++++++++++++++++++++++++++ RPS/Properties/AssemblyInfo.cs | 36 +++++++++++++ RPS/RPS.cs | 19 +++++++ RPS/RPS.csproj | 67 ++++++++++++++++++++++++ 11 files changed, 427 insertions(+) create mode 100644 RPS.sln create mode 100644 RPS/AIEnemy.cs create mode 100644 RPS/AIPlayer.cs create mode 100644 RPS/Hand.cs create mode 100644 RPS/IAI.cs create mode 100644 RPS/Judge.cs create mode 100644 RPS/Player.cs create mode 100644 RPS/Program.cs create mode 100644 RPS/Properties/AssemblyInfo.cs create mode 100644 RPS/RPS.cs create mode 100644 RPS/RPS.csproj diff --git a/RPS.sln b/RPS.sln new file mode 100644 index 0000000..c021d27 --- /dev/null +++ b/RPS.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RPS", "RPS\RPS.csproj", "{43F682FF-0A31-47CB-95A9-5ECD5EB4432F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {43F682FF-0A31-47CB-95A9-5ECD5EB4432F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {43F682FF-0A31-47CB-95A9-5ECD5EB4432F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {43F682FF-0A31-47CB-95A9-5ECD5EB4432F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {43F682FF-0A31-47CB-95A9-5ECD5EB4432F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/RPS/AIEnemy.cs b/RPS/AIEnemy.cs new file mode 100644 index 0000000..b504117 --- /dev/null +++ b/RPS/AIEnemy.cs @@ -0,0 +1,28 @@ +using System; + +namespace RPS +{ + // 敵の思考パターン + class AIEnemy : IAI + { + // オブジェクト + public static readonly IAI instance = new AIEnemy(); + + // パターン一覧 + RPS[] rps = { RPS.rock, RPS.paper, RPS.scissor }; + + // 乱数 + Random rnd = new Random(); + + // コンストラクタ + private AIEnemy() + { + } + + // 考える + public void think(Player player) + { + player.pattern = rps[rnd.Next(rps.Length)]; + } + } +} diff --git a/RPS/AIPlayer.cs b/RPS/AIPlayer.cs new file mode 100644 index 0000000..ea36501 --- /dev/null +++ b/RPS/AIPlayer.cs @@ -0,0 +1,46 @@ +using System; + +namespace RPS +{ + // プレイヤーの思考パターン + class AIPlayer : IAI + { + // オブジェクト + public static readonly IAI instance = new AIPlayer(); + + // パターン一覧 + RPS[] rps = { RPS.rock, RPS.scissor, RPS.paper }; + + // コンストラクタ + private AIPlayer() + { + } + + // 考える + public void think(Player player) + { + Console.WriteLine("右から選ぶ: 1.グー, 2.チョキ, 3.パー 0.終了"); + bool commied = false; + do + { + char c = Console.ReadKey().KeyChar; + switch (c) + { + case '0': + Environment.Exit(0); + break; + case '1': + case '2': + case '3': + commied = true; + player.pattern = rps[(int)char.GetNumericValue(c) - 1]; + break; + default: + Console.WriteLine("そのキーは使えない"); + break; + } + } + while (!commied); + } + } +} diff --git a/RPS/Hand.cs b/RPS/Hand.cs new file mode 100644 index 0000000..48c6a01 --- /dev/null +++ b/RPS/Hand.cs @@ -0,0 +1,23 @@ +namespace RPS +{ + // 手のAAを作るだけ + static class Hand + { + // AA + static string[] aa = new string[(int)RPS.__reserved]; + + // コンストラクタ + static Hand() + { + aa[(int)RPS.rock] = " ___\n|___フ"; + aa[(int)RPS.paper] = "||||\n|___/"; + aa[(int)RPS.scissor] = " _||\n|___フ"; + } + + // AA取得 + public static string getAA(RPS rps) + { + return aa[(int)rps]; + } + } +} diff --git a/RPS/IAI.cs b/RPS/IAI.cs new file mode 100644 index 0000000..8268b63 --- /dev/null +++ b/RPS/IAI.cs @@ -0,0 +1,9 @@ +namespace RPS +{ + // 思考パターンのインターフェイス + interface IAI + { + // 考える + void think(Player player); + } +} diff --git a/RPS/Judge.cs b/RPS/Judge.cs new file mode 100644 index 0000000..14920c7 --- /dev/null +++ b/RPS/Judge.cs @@ -0,0 +1,65 @@ +namespace RPS +{ + // 判定 + static class Judge + { + + // 複数の手で判定 + static public RPS judge(int rock, int paper, int scissor) + { + RPS result = RPS.__reserved; + // グーチョキパーが全部出ていたら強制あいこ + if (!(rock > 0 && paper > 0 && scissor > 0)) + { + RPS expr1 = RPS.__reserved; + RPS expr2 = RPS.__reserved; + // グーとパーで判定 + if (rock > 0 && paper > 0) + { + expr1 = RPS.rock; + expr2 = RPS.paper; + } + // パーとチョキで判定 + if (paper > 0 && scissor > 0) + { + expr1 = RPS.paper; + expr2 = RPS.scissor; + } + // チョキとグーで判定 + if (scissor > 0 && rock > 0) + { + expr1 = RPS.scissor; + expr2 = RPS.rock; + } + result = judge(expr1, expr2); + } + return result; + } + + // 一対一で判定 + static public RPS judge(RPS expr1, RPS expr2) + { + // 下の判定に引っかからなければあいこ + RPS result = RPS.__reserved; + // グー対パーはパーの勝ち + if ((expr1 == RPS.rock && expr2 == RPS.paper) || + (expr2 == RPS.rock && expr1 == RPS.paper)) + { + result = RPS.paper; + } + // パー対チョキはチョキの勝ち + if ((expr1 == RPS.paper && expr2 == RPS.scissor) || + (expr2 == RPS.paper && expr1 == RPS.scissor)) + { + result = RPS.scissor; + } + // チョキ対グーはグーの勝ち + if ((expr1 == RPS.scissor && expr2 == RPS.rock) || + (expr2 == RPS.scissor && expr1 == RPS.rock)) + { + result = RPS.rock; + } + return result; + } + } +} diff --git a/RPS/Player.cs b/RPS/Player.cs new file mode 100644 index 0000000..478b8f9 --- /dev/null +++ b/RPS/Player.cs @@ -0,0 +1,21 @@ +namespace RPS +{ + // 対戦相手 + class Player + { + // 名前 + public string name = "Anonymous"; + + // 思考パターン + public IAI ai = AIEnemy.instance; + + // 手 + public RPS pattern; + + // 考える + public void think() + { + ai.think(this); + } + } +} diff --git a/RPS/Program.cs b/RPS/Program.cs new file mode 100644 index 0000000..3118eca --- /dev/null +++ b/RPS/Program.cs @@ -0,0 +1,93 @@ +using System; + +namespace RPS +{ + // メインプログラム + class Program + { + // 人数 + const int PLAYER_COUNT = 5; + + // 相手一覧 + Player[] players = new Player[PLAYER_COUNT]; + + // ターン数 + int turn; + + // ここから始め + static void Main(string[] args) + { + // タイトルを表示 + Console.WriteLine("じゃんけん"); + new Program().run(); + } + + // コンストラクタでゲームの場を作る + Program() + { + // 対戦相手を作る + for (int i = PLAYER_COUNT; --i >= 0; ) + { + Player player = new Player(); + player.name = string.Format("敵番号 {0}", i); + players[i] = player; + } + // 零番が俺だ + players[0].name = "あなた"; + players[0].ai = AIPlayer.instance; + } + + // メインループ + void run() + { + while (true) + { + // ターン表示→考える→手を表示→判定の繰り返し + Console.WriteLine("=================="); + Console.WriteLine("ターン {0}", turn++); + think(); + int[] result = open(); + judge(result[(int)RPS.rock], result[(int)RPS.paper], result[(int)RPS.scissor]); + } + } + + // 考える + private void think() + { + for (int i = players.Length; --i >= 0; ) + { + players[i].think(); + } + } + + // 全員の手を表示 + private int[] open() + { + int[] result = new int[(int)RPS.__reserved]; + for (int i = players.Length; --i >= 0; ) + { + RPS pattern = players[i].pattern; + Console.WriteLine("\n{0} の出した手:", players[i].name); + Console.WriteLine(Hand.getAA(pattern)); + result[(int)pattern]++; + } + return result; + } + + // 勝敗判定 + private void judge(int rock, int paper, int scissor) + { + Console.WriteLine(" - - - - - - - "); + RPS won = Judge.judge(rock, paper, scissor); + if (won == RPS.__reserved) + { + Console.WriteLine("あいこで!"); + } + else + { + Console.WriteLine(Hand.getAA(won)); + Console.WriteLine("が勝ちました"); + } + } + } +} diff --git a/RPS/Properties/AssemblyInfo.cs b/RPS/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..40c1d12 --- /dev/null +++ b/RPS/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// アセンブリに関する一般情報は以下の属性セットをとおして制御されます。 +// アセンブリに関連付けられている情報を変更するには、 +// これらの属性値を変更してください。 +[assembly: AssemblyTitle("ROCK-PAPER-SCISSOR")] +[assembly: AssemblyDescription("ROCK-PAPER-SCISSOR")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ROCK-PAPER-SCISSOR")] +[assembly: AssemblyCopyright("Copyright © 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible を false に設定すると、その型はこのアセンブリ内で COM コンポーネントから +// 参照不可能になります。COM からこのアセンブリ内の型にアクセスする場合は、 +// その型の ComVisible 属性を true に設定してください。 +[assembly: ComVisible(false)] + +// 次の GUID は、このプロジェクトが COM に公開される場合の、typelib の ID です +[assembly: Guid("42facf45-0eb6-400c-aabf-937b4daf1f70")] + +// アセンブリのバージョン情報は、以下の 4 つの値で構成されています: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を +// 既定値にすることができます: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/RPS/RPS.cs b/RPS/RPS.cs new file mode 100644 index 0000000..19e7993 --- /dev/null +++ b/RPS/RPS.cs @@ -0,0 +1,19 @@ +namespace RPS +{ + // グーチョキパーを示す定数 + enum RPS + { + + // グー + rock, + + // パー + paper, + + // チョキ + scissor, + + // 予約 + __reserved, + } +} diff --git a/RPS/RPS.csproj b/RPS/RPS.csproj new file mode 100644 index 0000000..7b17e28 --- /dev/null +++ b/RPS/RPS.csproj @@ -0,0 +1,67 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {43F682FF-0A31-47CB-95A9-5ECD5EB4432F} + Exe + Properties + RPS + RPS + v3.5 + 512 + RPS.Program + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + + + + + + + + \ No newline at end of file