fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main() {
  6. int playerChoice, computerchoice;
  7. srand(time(NULL));
  8.  
  9. printf("じゃんけんゲームを始めます!\n");
  10. printf("0: グー, 1: チョキ, 2: パー\n");
  11.  
  12. // プレイヤーの手を入力
  13. printf("あなたの手を選んでください (0-2): ");
  14. scanf("%d", &playerChoice);
  15.  
  16. // プレイヤーの手を表示
  17. switch (playerChoice) {
  18. case 0:
  19. printf("あなたの手: グー\n");
  20. break;
  21. case 1:
  22. printf("あなたの手: チョキ\n");
  23. break;
  24. case 2:
  25. printf("あなたの手: パー\n");
  26. break;
  27. default:
  28. printf("無効な選択です。\n");
  29. return 1;
  30. }
  31.  
  32. // コンピュータの手をランダムに選択
  33. computerchoice = rand() % 3;
  34. switch (computerchoice) {
  35. case 0:
  36. printf("コンピュータの手: グー\n");
  37. break;
  38. case 1:
  39. printf("コンピュータの手: チョキ\n");
  40. break;
  41. case 2:
  42. printf("コンピュータの手: パー\n");
  43. break;
  44. }
  45.  
  46. // 勝敗の判定
  47. if ((playerChoice == 0 && computerchoice == 1) ||
  48. (playerChoice == 1 && computerchoice == 2) ||
  49. (playerChoice == 2 && computerchoice == 0)) {
  50. printf("あなたの勝ちです!\n");
  51. } else if ((computerchoice == 0 && playerChoice == 1) ||
  52. (computerchoice == 1 && playerChoice == 2) ||
  53. (computerchoice == 2 && playerChoice == 0)) {
  54. printf("コンピュータの勝ちです!\n");
  55. } else {
  56. printf("引き分けです!\n");
  57. }
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0s 5296KB
stdin
0
stdout
じゃんけんゲームを始めます!
0: グー, 1: チョキ, 2: パー
あなたの手を選んでください (0-2): あなたの手: グー
コンピュータの手: チョキ
あなたの勝ちです!