[C言語] じゃんけんの勝敗を出すプログラムの作り方
C言語でじゃんけんの勝敗を判定するプログラムを作成するには、ユーザーとコンピュータの選択を比較し、勝敗を決定するロジックを実装します。
ユーザーの入力を受け取り、ランダムにコンピュータの選択を生成するために、scanf関数とrand関数を使用します。
選択肢は通常、グー、チョキ、パーの3つで、それぞれに数値を割り当てて比較します。
条件分岐にはif文やswitch文を用いて、勝敗の判定を行います。
このプログラムは、基本的なC言語の構文理解と条件分岐の練習に最適です。
じゃんけんプログラムの設計
じゃんけんプログラムを作成するためには、まず基本的な設計を考える必要があります。
このセクションでは、入力の取得方法、勝敗判定のロジック、そして結果の表示について詳しく説明します。
入力の取得方法
じゃんけんプログラムでは、ユーザーとコンピュータの両方の手を取得する必要があります。
以下にそれぞれの方法を説明します。
ユーザーからの入力
ユーザーからの入力は、通常キーボードを通じて取得します。
C言語では、scanf関数を使用してユーザーの入力を受け取ることが一般的です。
以下にサンプルコードを示します。
#include <stdio.h>
int main() {
    int userChoice;
    printf("じゃんけんの手を選んでください (0: グー, 1: チョキ, 2: パー): ");
    scanf("%d", &userChoice);
    // ユーザーの選択を処理する
    return 0;
}コンピュータの手の生成
コンピュータの手はランダムに生成する必要があります。
C言語では、rand関数を使用してランダムな数値を生成します。
以下にサンプルコードを示します。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    srand(time(NULL)); // ランダムシードを設定
    int computerChoice = rand() % 3; // 0から2のランダムな数を生成
    // コンピュータの選択を処理する
    return 0;
}勝敗判定のロジック
じゃんけんの勝敗を判定するためには、ユーザーとコンピュータの手を比較するロジックが必要です。
ここでは、if文とswitch文を使った方法を紹介します。
if文を使った判定
if文を使用して勝敗を判定する方法は、条件を順番にチェックしていく方法です。
以下にサンプルコードを示します。
#include <stdio.h>
void determineWinner(int userChoice, int computerChoice) {
    if (userChoice == computerChoice) {
        printf("引き分けです。\n");
    } else if ((userChoice == 0 && computerChoice == 1) ||
               (userChoice == 1 && computerChoice == 2) ||
               (userChoice == 2 && computerChoice == 0)) {
        printf("あなたの勝ちです!\n");
    } else {
        printf("コンピュータの勝ちです。\n");
    }
}switch文を使った判定
switch文を使用すると、コードがより見やすくなる場合があります。
以下にサンプルコードを示します。
#include <stdio.h>
void determineWinner(int userChoice, int computerChoice) {
    switch (userChoice) {
        case 0: // ユーザーがグーを選んだ場合
            if (computerChoice == 0) printf("引き分けです。\n");
            else if (computerChoice == 1) printf("あなたの勝ちです!\n");
            else printf("コンピュータの勝ちです。\n");
            break;
        case 1: // ユーザーがチョキを選んだ場合
            if (computerChoice == 0) printf("コンピュータの勝ちです。\n");
            else if (computerChoice == 1) printf("引き分けです。\n");
            else printf("あなたの勝ちです!\n");
            break;
        case 2: // ユーザーがパーを選んだ場合
            if (computerChoice == 0) printf("あなたの勝ちです!\n");
            else if (computerChoice == 1) printf("コンピュータの勝ちです。\n");
            else printf("引き分けです。\n");
            break;
    }
}結果の表示
勝敗が判定されたら、その結果をユーザーに表示します。
printf関数を使用して、結果をコンソールに出力します。
以下にサンプルコードを示します。
#include <stdio.h>
void displayResult(int userChoice, int computerChoice) {
    printf("あなたの選択: %d, コンピュータの選択: %d\n", userChoice, computerChoice);
    determineWinner(userChoice, computerChoice);
}このようにして、じゃんけんプログラムの基本的な設計を行うことができます。
次のステップでは、これらの要素を組み合わせて、完全なプログラムを作成します。
完成したプログラム
ここでは、これまでに説明した各要素を組み合わせて、じゃんけんの勝敗を判定する完全なプログラムを紹介します。
このプログラムは、ユーザーからの入力を受け取り、コンピュータの手をランダムに生成し、勝敗を判定して結果を表示します。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 勝敗を判定する関数
void determineWinner(int userChoice, int computerChoice) {
    if (userChoice == computerChoice) {
        printf("引き分けです。\n");
    } else if ((userChoice == 0 && computerChoice == 1) ||
               (userChoice == 1 && computerChoice == 2) ||
               (userChoice == 2 && computerChoice == 0)) {
        printf("あなたの勝ちです!\n");
    } else {
        printf("コンピュータの勝ちです。\n");
    }
}
// 結果を表示する関数
void displayResult(int userChoice, int computerChoice) {
    const char *choices[] = {"グー", "チョキ", "パー"};
    printf("あなたの選択: %s, コンピュータの選択: %s\n", choices[userChoice], choices[computerChoice]);
    determineWinner(userChoice, computerChoice);
}
int main() {
    int userChoice;
    srand(time(NULL)); // ランダムシードを設定
    // ユーザーからの入力を取得
    printf("じゃんけんの手を選んでください (0: グー, 1: チョキ, 2: パー): ");
    scanf("%d", &userChoice);
    // コンピュータの手をランダムに生成
    int computerChoice = rand() % 3;
    // 結果を表示
    displayResult(userChoice, computerChoice);
    return 0;
}プログラムの実行例
じゃんけんの手を選んでください (0: グー, 1: チョキ, 2: パー): 0
あなたの選択: グー, コンピュータの選択: チョキ
あなたの勝ちです!このプログラムは、ユーザーが選択した手とコンピュータがランダムに選んだ手を比較し、勝敗を判定して結果を表示します。
ユーザーが入力した手に応じて、結果が異なるため、何度でも楽しむことができます。
プログラムの構造はシンプルで、C言語の基本的な機能を活用しています。
応用例
じゃんけんプログラムは基本的な形からさまざまな応用が可能です。
ここでは、複数回のじゃんけんを行うプログラム、スコアを記録するプログラム、そしてGUIを使ったじゃんけんプログラムの3つの応用例を紹介します。
複数回のじゃんけんを行うプログラム
複数回のじゃんけんを行うプログラムでは、ループを使用してゲームを繰り返し実行します。
以下にサンプルコードを示します。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void determineWinner(int userChoice, int computerChoice) {
    if (userChoice == computerChoice) {
        printf("引き分けです。\n");
    } else if ((userChoice == 0 && computerChoice == 1) ||
               (userChoice == 1 && computerChoice == 2) ||
               (userChoice == 2 && computerChoice == 0)) {
        printf("あなたの勝ちです!\n");
    } else {
        printf("コンピュータの勝ちです。\n");
    }
}
void displayResult(int userChoice, int computerChoice) {
    const char *choices[] = {"グー", "チョキ", "パー"};
    printf("あなたの選択: %s, コンピュータの選択: %s\n", choices[userChoice], choices[computerChoice]);
    determineWinner(userChoice, computerChoice);
}
int main() {
    int userChoice;
    char playAgain;
    srand(time(NULL));
    do {
        printf("じゃんけんの手を選んでください (0: グー, 1: チョキ, 2: パー): ");
        scanf("%d", &userChoice);
        int computerChoice = rand() % 3;
        displayResult(userChoice, computerChoice);
        printf("もう一度プレイしますか? (y/n): ");
        scanf(" %c", &playAgain);
    } while (playAgain == 'y');
    return 0;
}このプログラムでは、ユーザーが y を入力する限り、じゃんけんを繰り返し行います。
スコアを記録するプログラム
スコアを記録するプログラムでは、ユーザーとコンピュータの勝利数をカウントします。
以下にサンプルコードを示します。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void determineWinner(int userChoice, int computerChoice, int *userScore, int *computerScore) {
    if (userChoice == computerChoice) {
        printf("引き分けです。\n");
    } else if ((userChoice == 0 && computerChoice == 1) ||
               (userChoice == 1 && computerChoice == 2) ||
               (userChoice == 2 && computerChoice == 0)) {
        printf("あなたの勝ちです!\n");
        (*userScore)++;
    } else {
        printf("コンピュータの勝ちです。\n");
        (*computerScore)++;
    }
}
void displayResult(int userChoice, int computerChoice, int userScore, int computerScore) {
    const char *choices[] = {"グー", "チョキ", "パー"};
    printf("あなたの選択: %s, コンピュータの選択: %s\n", choices[userChoice], choices[computerChoice]);
    determineWinner(userChoice, computerChoice, &userScore, &computerScore);
    printf("スコア - あなた: %d, コンピュータ: %d\n", userScore, computerScore);
}
int main() {
    int userChoice;
    char playAgain;
    int userScore = 0, computerScore = 0;
    srand(time(NULL));
    do {
        printf("じゃんけんの手を選んでください (0: グー, 1: チョキ, 2: パー): ");
        scanf("%d", &userChoice);
        int computerChoice = rand() % 3;
        displayResult(userChoice, computerChoice, userScore, computerScore);
        printf("もう一度プレイしますか? (y/n): ");
        scanf(" %c", &playAgain);
    } while (playAgain == 'y');
    return 0;
}このプログラムでは、各ゲームの結果に応じてスコアが更新され、ユーザーとコンピュータのスコアが表示されます。
GUIを使ったじゃんけんプログラム
GUIを使ったじゃんけんプログラムでは、グラフィカルなインターフェースを用いてユーザーが操作できるようにします。
C言語でGUIを実装するには、GTKやQtなどのライブラリを使用することが一般的です。
ここでは、具体的なコードは示しませんが、以下のような手順で実装します。
- GUIライブラリをインストールし、プロジェクトに追加します。
- ウィンドウを作成し、じゃんけんの手を選択するボタンを配置します。
- ボタンがクリックされたときのイベントハンドラを設定し、勝敗を判定します。
- 結果を表示するラベルやダイアログを用意します。
GUIを使うことで、より直感的で視覚的に楽しめるじゃんけんプログラムを作成することができます。
まとめ
じゃんけんプログラムの作成を通じて、C言語の基本的なプログラミング技術を学ぶことができます。
入力の取得、ランダムな手の生成、勝敗判定のロジック、そして結果の表示といった要素を組み合わせることで、シンプルながらも実用的なプログラムを作成できることを振り返りました。
この記事を参考に、さらに複雑なプログラムや応用例に挑戦してみてください。
 
![[C言語] 勝つまでじゃんけんするプログラムの書き方](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3075.png)
![[C言語] じゃんけんを繰り返し行うプログラムの書き方](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3074.png)
![[C言語] switch文でじゃんけんの判定をする方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3073.png)
![[C言語] 5回勝負で勝敗を決めるじゃんけんプログラムの書き方](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3072.png)
![[C言語] 3人でじゃんけんするプログラムの書き方](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3071.png)
![[C言語] オセロをするプログラムの書き方](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3068.png)
![[C言語] クイズを出すプログラムの作り方](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3070.png)
![[C言語] キーボードから文字や数字を入力する方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3069.png)
![[C言語] アスタリスクを画面に表示する方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3067.png)
![[C言語] アスタリスクで図形(三角形や四角形)を描く方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3066.png)
![[C言語] じゃんけんの判定を行うプログラムの書き方](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3077.png)
![[C言語] ひし形を作成するプログラムの書き方](https://af-e.net/wp-content/uploads/2024/08/thumbnail-3078.png)