LoginSignup
2
0

More than 3 years have passed since last update.

C#でHit&Blowゲーム

Last updated at Posted at 2019-08-15

C#でなにかゲームをつくるシリーズ2弾

ヒントをもとに数字を当てるゲームであるHit&Over.
トレーニング代わりに作ってみました.

数字も桁も合っていると  Hit
数字のみ合っていると   Blow
与えられた桁が違うと   Not Match Dight!!
と表示されます.

3桁で、正解が137としたとき、
179と予想したら、1 hit 1 blowとなります.

ちなみに正解は777や099のような数字の重複はありません

stringで判断するのではなく、int[]で判断するように変更

コード

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

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

数字も桁も合っていると  Hit
数字のみ合っていると   Blow
与えられた桁が違うと   Not Match Dight!!
とヒントが出るよ!
-----
Program.cs
using System;
using System.IO;

namespace Game_HitBlow
{
    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("1~10のどの桁に挑戦しますか");
                int dight;
                while (!int.TryParse(Console.ReadLine(), out dight) || dight < 1 || dight > 10)
                {
                    Console.WriteLine("1~10のどの桁に挑戦しますか");
                }
                Console.WriteLine("-----");

                // 正解の作成
                //  数字が重なることはない
                var useNum = new bool[10];
                var correct = new int[dight];
                for (int i = 0; i < dight; i++)
                {
                    var next = rnd.Next(10);
                    while (useNum[next])
                    {
                        next = rnd.Next(10);
                    }
                    correct[i] = next;
                    useNum[next] = true;
                }
                /*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;
                    int put = dight - 1;
                    for (int i = answerTxt.Length - 1; i >= 0; i--)
                    {
                        if (put < 0)
                        {
                            Console.Write("      ");
                            Console.WriteLine("Not Match Dight!!");
                            isCheck = false;
                            break;
                        }
                        if ('0' <= answerTxt[i] && answerTxt[i] <= '9')
                        {
                            answer[put] = answerTxt[i] - '0';
                            put--;
                        }
                    }
                    if (put > 0)
                    {
                        Console.Write("      ");
                        Console.WriteLine("Not Match Dight!!");
                        isCheck = false;
                    }
                    /*Console.Write("answer = ");
                    foreach (var num in answer)
                        Console.Write(num);
                    Console.WriteLine();*/

                    if (!isCheck)
                    {
                        time--;
                        continue;
                    }

                    // hitとblowを数える
                    var hit = 0;
                    var blow = 0;
                    for (int i = 0; i < dight; i++)
                    {
                        if (answer[i] == correct[i])
                        {
                            hit++;
                        }
                        else if (useNum[answer[i]])
                        {
                            blow++;
                        }
                    }

                    // 正解
                    if (hit == dight)
                    {
                        Console.Write("      ");
                        Console.WriteLine("Correct!");
                        isWrong = false;
                        continue;
                    }

                    // 表示
                    Console.Write("      ");
                    Console.WriteLine("{0} hit", hit);
                    Console.Write("      ");
                    Console.WriteLine("{0} blow", blow);
                }
                Console.WriteLine("-----");
            }
        }
    }
}
2
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
2
0