C#配列まで学んだ人のためのミニゲーム3選

「なるほどなっとくC#」の配列まで終わった生徒さん向けに、条件分岐・ループ・配列の理解を深めるシンプルなミニゲームサンプルを【サンプル1~3】としてセットでまとめます

✅ サンプル1:くじ引きシミュレーター

🎮 ゲームの内容

  • 1~5の番号から好きな番号を選んでくじを引く
  • 当たり番号は配列で管理(例:2と4)
  • 最大5回チャレンジ可能
  • 当たりが何回出たか履歴を配列に保存して最後に表示

📝 サンプルコード

using System;

class Program
{
    static void Main(string[] args)
    {
        int[] winningNumbers = { 2, 4 }; // 当たり番号
        int[] tries = new int[5]; // 入力履歴
        int hitCount = 0;

        Console.WriteLine("=== くじ引きゲーム ===");
        Console.WriteLine("1~5の番号から選んで当たりを狙おう!");

        for (int i = 0; i < tries.Length; i++)
        {
            Console.Write($"{i + 1}回目の番号を入力: ");
            int pick = int.Parse(Console.ReadLine());
            tries[i] = pick;

            bool isHit = false;
            for (int j = 0; j < winningNumbers.Length; j++)
            {
                if (pick == winningNumbers[j])
                {
                    isHit = true;
                    break;
                }
            }

            if (isHit)
            {
                Console.WriteLine("当たり!おめでとう!");
                hitCount++;
            }
            else
            {
                Console.WriteLine("はずれ!");
            }
        }

        Console.WriteLine($"\n当たりは {hitCount} 回でした。");

        Console.WriteLine("=== 入力履歴 ===");
        for (int i = 0; i < tries.Length; i++)
        {
            Console.WriteLine($"{i + 1}回目: {tries[i]}");
        }
    }
}

✅ サンプル2:数字当てゲーム(履歴付き)

🎮 ゲームの内容

  • 1~10のランダムな正解をプログラムが決める
  • プレイヤーは数字を当てるまで何度も挑戦できる
  • 入力履歴を配列で保存し、最後に一覧表示する

📝 サンプルコード

using System;

class Program
{
    static void Main(string[] args)
    {
        Random rand = new Random();
        int answer = rand.Next(1, 11);

        int[] history = new int[10];
        int count = 0;

        Console.WriteLine("=== 数字当てゲーム ===");
        Console.WriteLine("1~10の数字を当ててください!");

        bool isCorrect = false;

        while (!isCorrect && count < history.Length)
        {
            Console.Write("数字を入力してください: ");
            int guess = int.Parse(Console.ReadLine());

            history[count] = guess;
            count++;

            if (guess == answer)
            {
                Console.WriteLine("正解です!おめでとう!");
                isCorrect = true;
            }
            else if (guess < answer)
            {
                Console.WriteLine("もっと大きい数字です!");
            }
            else
            {
                Console.WriteLine("もっと小さい数字です!");
            }
        }

        Console.WriteLine("\n=== 入力履歴 ===");
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine($"{i + 1}回目: {history[i]}");
        }

        Console.WriteLine("\nゲーム終了!");
    }
}

✅ サンプル3:簡易じゃんけん履歴付き

🎮 ゲームの内容

  • プレイヤーとコンピューターでじゃんけん
  • 1: グー / 2: チョキ / 3: パー
  • 勝敗を判定して配列に履歴保存
  • 5回勝負の後、結果一覧を表示

📝 サンプルコード

using System;

class Program
{
    static void Main(string[] args)
    {
        string[] hands = { "グー", "チョキ", "パー" };
        string[] results = new string[5];

        Random rand = new Random();

        for (int i = 0; i < results.Length; i++)
        {
            Console.Write("1:グー 2:チョキ 3:パー >> ");
            int player = int.Parse(Console.ReadLine()) - 1;
            int cpu = rand.Next(0, 3);

            Console.WriteLine($"あなた: {hands[player]} | コンピューター: {hands[cpu]}");

            if (player == cpu)
            {
                Console.WriteLine("あいこ!");
                results[i] = "引き分け";
            }
            else if ((player == 0 && cpu == 1) || (player == 1 && cpu == 2) || (player == 2 && cpu == 0))
            {
                Console.WriteLine("あなたの勝ち!");
                results[i] = "勝ち";
            }
            else
            {
                Console.WriteLine("あなたの負け!");
                results[i] = "負け";
            }
            Console.WriteLine();
        }

        Console.WriteLine("=== 勝負履歴 ===");
        for (int i = 0; i < results.Length; i++)
        {
            Console.WriteLine($"{i + 1}回目: {results[i]}");
        }
    }
}

🎓 3つのサンプルで学べること

  • 配列に値を保存して履歴を残す方法
  • 繰り返し処理で配列に値を追加する流れ
  • 条件分岐を組み合わせて判定を行う基本形
  • シンプルなゲームのロジックを自分で改造する力

訪問数 2 回, 今日の訪問数 2回