LoginSignup
0
0

More than 3 years have passed since last update.

C#で何桁でもできるOver&Underゲーム

Last updated at Posted at 2019-08-15

ヒントをもとに数字を当てるゲームであるOver&Under.
それを好みの桁数でできるようにトレーニング代わりにしてみました.

3桁だと、0から999までのどれかが正解.
正解の数字より大きいと "Over"
       小さいと "Under"
3桁より大きいと "Too Over!!"
と表示されます.

しくみとしては、各桁ごとにint[]で頭から判断しています
そのため、longなどに捕らわれず何桁でもできます

コメントの助言により、stringによる判断から、int[]による判断へ変更しました
ありがとうございます

コード

Program.csと、タイトルなどが書いてあるTitle.txtがあります

Title.txt
Over & Under Game
-----
n桁の数字を当てよう!

正解の数字より大きいと Over
       小さいと Under
n桁より大きいと     Too Over!!
とヒントが出るよ!
-----
Program.cs
using System;
using System.IO;

namespace Game_OverUnder
{
    class Program
    {
        static void Main()
        {
            // ランダムな数字を作成するやつ
            var rnd = new Random();

            // Title.txtの読込みとその表示
            var path = "Title.txt";
            try
            {
                using (var reader = new StreamReader(path))
                {
                    while (!reader.EndOfStream)
                    {
                        Console.WriteLine(reader.ReadLine());
                    }
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("{0}が見つかりません.", path);
            }
            catch
            {
                Console.WriteLine("読込みエラーが起きました.");
            }

            while (true)
            {
                // 桁数の入力
                Console.WriteLine("何桁に挑戦しますか");
                int dight;
                while (!int.TryParse(Console.ReadLine(), out dight) || dight < 1)
                {
                    Console.WriteLine("何桁に挑戦しますか");
                }
                Console.WriteLine("-----");

                // 正解の作成
                var correct = new int[dight];
                for (int i = 0; i < dight; i++)
                {
                    correct[i] = rnd.Next(10);
                }
                /*Console.Write("correct = ");
                foreach (var num in correct)
                    Console.Write(num);
                Console.WriteLine();*/

                // 正解を当てるループ
                bool isWrong = true;
                for (int time = 1; isWrong; time++)
                {
                    var answer = new int[dight];

                    // 予想の入力
                    Console.Write("{0,3} > ", time);
                    var answerTxt = Console.ReadLine();

                    // 予想のstringをint[]に変換する
                    var isCheck = true;
                    for (int i = answerTxt.Length - 1, put = dight - 1; i >= 0; i--)
                    {
                        if (put < 0)
                        {
                            Console.Write("      ");
                            Console.WriteLine("Too Over!!");
                            isCheck = false;
                            break;
                        }
                        if ('0' <= answerTxt[i] && answerTxt[i] <= '9')
                        {
                            answer[put] = answerTxt[i] - '0';
                            put--;
                        }
                    }
                    /*Console.Write("answer = ");
                    foreach (var num in answer)
                        Console.Write(num);
                    Console.WriteLine();*/
                    if (!isCheck)
                    {
                        continue;
                    }

                    // 大小のチェック
                    var a = Check(answer, correct, 0);
                    switch (a)
                    {
                        case 0:
                            Console.Write("      ");
                            Console.WriteLine("Correct!");
                            isWrong = false;
                            break;

                        case 1:
                            Console.Write("      ");
                            Console.WriteLine("Over");
                            break;

                        case -1:
                            Console.Write("      ");
                            Console.WriteLine("Under");
                            break;
                    }
                }
                Console.WriteLine("-----");
            }
        }

        // 再帰による大小のチェック
        static int Check(int[] answer, int[] correct, int num)
        {
            // 終点まで同じだったら0を返す
            if (num == correct.Length)
            {
                return 0;
            }

            // 同じだったら次へ
            if (answer[num] == correct[num])
            {
                return Check(answer, correct, num + 1);
            }
            // 大きいと1を返す
            else if (answer[num] > correct[num])
            {
                return 1;
            }
            // 小さいと-1を返す
            else
            {
                return -1;
            }
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0