企画から考えるC#練習チュートリアル

【企画から考える C# 練習チュートリアル】として、3つのサンプルすべてを「企画 → 設計 → 実装 → 改造」まで一つにまとめます。


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

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

🔍 Step1. ゲーム企画

  • どんなゲーム?1〜5の番号の中から好きな番号を選び、当たり番号に一致すると当たり!
  • 当たり番号は?配列に複数設定(例:2と4)
  • 何を保存する?・入力履歴・当たり回数
  • 何回できる?5回

🗂️ Step2. 必要なパーツ

  • 当たり番号 → 配列
  • 入力履歴 → 配列
  • 当たり判定 → forのネスト
  • 回数 → for文

📝 Step3. 実装サンプル

using System;

class Program
{
    static void Main(string[] args)
    {
        int[] winningNumbers = { 2, 4 };
        int[] tries = new int[5];
        int hitCount = 0;

        Console.WriteLine("=== くじ引きゲーム ===");

        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($"当たりは {hitCount} 回でした。");
        Console.WriteLine("=== 履歴 ===");
        for (int i = 0; i < tries.Length; i++)
        {
            Console.WriteLine($"{i + 1}回目: {tries[i]}");
        }
    }
}

✨ Step4. 改造アイデア

  • 当たり番号を1つだけにする
  • 当たり番号をランダムにする
  • 重複入力を無効にする
  • くじ回数を増減してみる

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

🔍 Step1. ゲーム企画

  • どんなゲーム?1~10の正解の数字を当てる
  • 正解は?ランダム
  • 保存するもの?・入力履歴
  • 何回できる?正解するまで or 最大10回

🗂️ Step2. 必要なパーツ

  • 正解 → Random
  • 履歴 → 配列
  • 判定 → if
  • 繰り返し → while

📝 Step3. 実装サンプル

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("=== 数字当てゲーム ===");

        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("=== 履歴 ===");
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine($"{i + 1}回目: {history[i]}");
        }
    }
}

✨ Step4. 改造アイデア

  • 正解の範囲を1〜100にする
  • 挑戦回数制限を変更
  • ヒントの出し方を変える(差を教えるなど)

✅ サンプル3:じゃんけん勝負(履歴付き)

🔍 Step1. ゲーム企画

  • どんなゲーム?プレイヤー vs コンピューターでじゃんけん
  • 保存するもの?勝負結果の履歴
  • 何回勝負?5回

🗂️ Step2. 必要なパーツ

  • 手の種類 → 配列(グー/チョキ/パー)
  • 履歴 → 配列
  • 勝敗判定 → if
  • 繰り返し → for

📝 Step3. 実装サンプル

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]} | CPU: {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]}");
        }
    }
}

✨ Step4. 改造アイデア

  • 勝ち数/負け数/引き分け数をカウント
  • 勝負数を変更
  • CPUの手を固定パターンにする(連続で同じ手を出す)

🎓 まとめ

学べることサンプル1サンプル2サンプル3
配列の初期化
繰り返し (for/while)
条件分岐 if
履歴を配列に保存
企画から設計

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