じゃんけんをするコード。C言語で書いて
- User
じゃんけんをするコード。C言語で書いて
Assistant
じゃんけんをするC言語のコードは以下の通りです。このコードでは、ユーザーが0(グー)、1(チョキ)、2(パー)を入力し、コンピュータはランダムに選択します。勝敗はじゃんけんのルールに基づいて判定され、結果が表示されます。また、ゲームを続けるかどうかをユーザーに尋ねる機能も含まれています。
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(NULL)); int player_choice; int continue_game = 1; while (continue_game) { printf("じゃんけんをしましょう!\n"); printf("0: グー, 1: チョキ, 2: パー\n"); printf("あなたの選択を入力してください (0-2): "); scanf("%d", &player_choice); if (player_choice < 0 || player_choice > 2) { printf("無効な選択です。0, 1, 2のいずれかを入力してください。\n"); continue; } int computer_choice = rand() % 3; // 勝敗の判定 if (player_choice == computer_choice) { printf("引き分けです!\n"); } else if ((player_choice == 0 && computer_choice == 1) || (player_choice == 1 && computer_choice == 2) || (player_choice == 2 && computer_choice == 0)) { printf("あなたの勝ちです!\n"); } else { printf("コンピュータの勝ちです!\n"); } // 選択の表示 printf("あなたの選択: "); switch (player_choice) { case 0: printf("グー\n"); break; case 1: printf("チョキ\n"); break; case 2: printf("パー\n"); break; } printf("コンピュータの選択: "); switch (computer_choice) { case 0: printf("グー\n"); break; case 1: printf("チョキ\n"); break; case 2: printf("パー\n"); break; } printf("続けますか? (1: 続ける, 0: 終了): "); scanf("%d", &continue_game); } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.