Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuhei Nomura committed Apr 5, 2017
1 parent 66c32dc commit 94cf096
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 0 deletions.
20 changes: 20 additions & 0 deletions RPS.sln
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions RPS/AIEnemy.cs
Original file line number Diff line number Diff line change
@@ -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)];
}
}
}
46 changes: 46 additions & 0 deletions RPS/AIPlayer.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
23 changes: 23 additions & 0 deletions RPS/Hand.cs
Original file line number Diff line number Diff line change
@@ -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];
}
}
}
9 changes: 9 additions & 0 deletions RPS/IAI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace RPS
{
// 思考パターンのインターフェイス
interface IAI
{
// 考える
void think(Player player);
}
}
65 changes: 65 additions & 0 deletions RPS/Judge.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
21 changes: 21 additions & 0 deletions RPS/Player.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
93 changes: 93 additions & 0 deletions RPS/Program.cs
Original file line number Diff line number Diff line change
@@ -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("が勝ちました");
}
}
}
}
36 changes: 36 additions & 0 deletions RPS/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
19 changes: 19 additions & 0 deletions RPS/RPS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace RPS
{
// グーチョキパーを示す定数
enum RPS
{

// グー
rock,

// パー
paper,

// チョキ
scissor,

// 予約
__reserved,
}
}
Loading

0 comments on commit 94cf096

Please sign in to comment.