Last active
February 24, 2020 03:40
-
-
Save gremito/209cec2e7414f0906c6f4043859ce91f to your computer and use it in GitHub Desktop.
HighAndLow.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Text; | |
| using System.Collections.Generic; | |
| namespace ConsoleProject | |
| { | |
| // memo: ハイアンドローのロジックを作る | |
| class MainClass | |
| { | |
| // カードの代わりとなる1〜13の数 | |
| static List<int> numbers = new List<int>(); | |
| // めくった数字を格納する | |
| static List<int> turnedOver = new List<int>(); | |
| public static void Main(string[] args) | |
| { | |
| // カードの用意 | |
| Init(); | |
| // カードをめくる | |
| var num = CardNext(); | |
| Console.WriteLine("場にある数:" + num); | |
| Console.WriteLine("---"); | |
| // 上か下か当てる | |
| // 1が上で2が下 | |
| var player = int.Parse(Console.ReadLine()); | |
| Console.WriteLine("プレイヤー:" + ((player == 1) ? "上" : "下")); | |
| // カードをめくる | |
| var nextNum = CardNext(); | |
| Console.WriteLine("めくった数:" + nextNum); | |
| // めくられたカードが上か下か当てる | |
| var result = Judge(num, nextNum, player); | |
| // 勝ち負け判定 | |
| if (result) | |
| Console.WriteLine("勝ち!!"); | |
| else | |
| Console.WriteLine("負け..."); | |
| } | |
| // 数の初期化 | |
| static void Init() | |
| { | |
| for(int i= 1; i <= 13; i++) | |
| { | |
| numbers.Add(i); | |
| } | |
| } | |
| // カードをめくるメソッド | |
| static int CardNext() | |
| { | |
| // 数字を取り出す | |
| var index = new Random().Next(0, numbers.Count); | |
| var number = numbers[index]; | |
| // めくった数字を記憶させてデッキから消す | |
| numbers.RemoveAt(index); | |
| turnedOver.Add(number); | |
| return number; | |
| } | |
| // 上か下かの判定を決めるメソッド | |
| // true=勝ち、false=負け | |
| static bool Judge(int card, int nextCard, int player) | |
| { | |
| var result = false; | |
| // めくった数が場に出ている数よりも小さいか | |
| // かつプレイヤーの手が下だったら | |
| if ((card > nextCard) && player == 2) | |
| { | |
| result = true; | |
| } | |
| // めくった数が場に出ている数よりも大きいか | |
| // かつプレイヤーの手が下だったら | |
| else if ((card < nextCard) && player == 1) | |
| { | |
| result = true; | |
| } | |
| return result; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment