合計得点が21点以上の時か10回じゃんけんをしたときにプログラムを終了する。という条件を付けくわえたいです。
どのように表現すればいいのかアドバイスください。
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main(void) { int a = 0, b = 0; int points_a = 0; int points_b = 0; int turns = 0; char inputs[][10] = { "Rock","Scissors","Paper" }; for (turns = 0; turns < 10; turns++) { while (1) { printf("Input for game number %d: ", turns + 1); scanf("%d %d", &a, &b); if (a < 1 || a>3 || b < 1 || b>3) printf("Invalid input(Rock:1,Scissors:2,Paper:3),please try again:\n\n"); else break; } printf("A plays %s,B plays %s: ", inputs[a - 1], inputs[b - 1]); if (a == b) { printf("draw.\n"); } else if ((a == 1 && b == 2) || (a == 2 && b == 1)) { printf("%c wins and gets 1points.\n", (a < b ? 'A' : 'B')); if (a < b) points_a += 1; else points_b += 1; } else if ((a == 1 && b == 3) || (a == 3 && b == 1)) { printf("%c wins and gets 5 points.\n", (a > b ? 'A' : 'B')); if (a > b) points_a = 5; else points_b = 5; } else if ((a == 2 && b == 3) || (a == 3 && b == 2)) { printf("%c wins and gets 2 points.\n", (a < b ? 'A' : 'B')); if (a > b) points_a += 2; else points_b += 2; } else { printf("Unknown error\n"); return -1; } printf("Current number of points: A - %d, B - %d. \n\n", points_a, points_b); if (points_a >= 21 || points_b >= 21) break; } if (points_a == points_b) { printf("Draw(points:A %d,B %d)\n", points_a, points_b); } else { printf("\nWinner: %c (points: A %d, B %d)\n", (points_a > points_b) ? 'A' : 'B', points_a, points_b); } return(0); }
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答5件
0
「合計得点が21点以上の時か10回じゃんけんをした」時にプログラムを終了するのですから、それぞれの条件がどうなるかを書き出しましょう。つまり、
- 合計得点が21点以上
- じゃんけんを10回した
という条件を書き出してください。そのためには、合計得点
とじゃんけんの回数
を保持するようにしましょう。今のところ、これを保持する変数や処理はないですよね。まずはそれを用意しましょう。
そして、どちらかの条件が満たされた時にループを抜けるようにすればよいです。
投稿2018/04/29 02:22
総合スコア1557
0
こんにちは、
プログラムの都合上 Rock: 0, Scissors: 1, Paper: 2にしております。
ジャンケンの判定をするところですが
重要になってくるのが(user1の手 - user2の手 + 3) % 3です。この値をResult_numと名づけることにします。例えば、
|user1の手|user2の手|結果|Result_num|
|:--|:--:|--:|
|Rock|Rock|Draw|0|
|Rock|Paper|user1 lose|1|
|Scissors|Paper|user2 win|2|
のようになります。
これを使うとif文やswitch文を使わなくても判定できます。
質問欄に書いてあるソースには与えられる点数が
Rockで勝ったら1点、Scissorsで勝ったら2点、Paperで勝ったら5点にしてあるますが、
今回の説明では(それぞれの手 + 1) * 2にしております。(これでif文は不要)
適宜if文などで変更こともできますが、
ソースが煩雑になるため、このような形にしました。
また、判定の文字(win,lose,draw)は
char result_text[3][15] = {"Draw","user1 Lose","user1 Win!!"};
にすることで番号指定で文字を表示することができます。
私が書いたソースコードを載せておきます。
C
1#include <stdio.h> 2#include <stdlib.h> 3 4#define BUFSIZE 256 5#define HAND 3 6 7int main(void) 8{ 9 10 char buf[BUFSIZE]; 11 char result_text[HAND][20] = {"Draw","user1 Lose","user1 Win"}; 12 char hand[HAND][10] = {"Rock","Scissors","Paper"}; 13 int i; 14 int count = 0; 15 int user_hand[2]; 16 int user_point[2] = {0,0}; 17 int result_num; 18 int result; 19 20 while(1){ 21 printf("%d回目",count + 1); 22 printf("Rock : 0 , Scissors: 1, Paper : 2\n"); 23 for(i = 0; i < 2; i++){ 24 do{ 25 fgets(buf,sizeof(buf),stdin); 26 user_hand[i] = atoi(buf); 27 if(user_hand[i] < 0 || user_hand[i] > 2){ 28 printf("再入力してください。\n"); 29 } 30 }while(user_hand[i] < 0 || user_hand[i] > 2); 31 } 32 33 result = (user_hand[0] - user_hand[1] + 3) % HAND; 34 printf("user1:%s user2:%s\n",hand[user_hand[0]],hand[user_hand[1]]); 35 36 printf("%s \n",result_text[result]); 37 38 if(result != 0){ 39 user_point[result - 1] += (user_hand[result - 1] + 1) * 3; 40 } 41 42 printf("user1:%d user2:%d\n",user_point[0],user_point[1]); 43 count++; 44 if(count >= 10){ 45 break; 46 } 47 else if(user_point[0] >= 21 || user_point[1] >= 21){ 48 break; 49 } 50 } 51 printf("終了\n"); 52 printf("user1の点数:%d user2の点数:%d\n",user_point[0],user_point[1]); 53 return 0; 54} 55 56
投稿2018/04/29 02:14
編集2018/04/29 03:27退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/29 02:31
退会済みユーザー
2018/04/29 03:28
退会済みユーザー
2018/04/29 03:29
0
コードを作成しました。
実行環境 Linux
コンパイラ gcc
C
1#include <stdio.h> 2 3int main(void){ 4 int i=1; 5 int a; 6 int b; 7 int total_a = 0; 8 int total_b = 0; 9 10 do { 11 while(1) { 12 printf("Game input(EXIT= press 9) :\n"); 13 scanf("%d %d", &a, &b); 14 15 if (a==9 || b==9) { 16 printf("EXIT!!\n"); 17 return 0; 18 } 19 if ( (a>=4) || (a<=0) || (b>=4) || (b<=0) ) { 20 printf("Invalid input (Rock: 1, Scissors: 2, Paper: 3),please try again:\n"); 21 } else { 22 break; 23 } 24 } 25 26 switch (a) { 27 case 1: 28 printf("A: Rock,"); 29 break; 30 31 case 2: 32 printf("A: Scissors,"); 33 break; 34 35 case 3: 36 printf("A: Paper,"); 37 break; 38 } 39 40 switch (b) { 41 case 1: 42 printf(" B: Rock;"); 43 break; 44 45 case 2: 46 printf(" B: Scissors;"); 47 break; 48 49 case 3: 50 printf(" B: Paper;"); 51 break; 52 } 53 54 if (a == b) { 55 printf("Draw (points: A 0, B 0)\n"); 56 } 57 else if (a == 1 && b == 2) { 58 printf(" A wins and gets 1 points.\n"); 59 printf("Winner: A (points: A 1, B 0)\n"); 60 total_a = total_a + 1; 61 } 62 else if (a == 2 && b == 3) { 63 printf(" A wins and gets 2 points.\n"); 64 printf("Winner: A (points: A 2, B 0)\n"); 65 total_a = total_a + 2; 66 } 67 else if (a == 3 && b == 1) { 68 printf(" A wins and gets 5 points.\n"); 69 printf("Winner: A (points: A 5, B 0)\n"); 70 total_a = total_a + 5; 71 } 72 else if (a == 1 && b == 3) { 73 printf(" B wins and gets 1 points.\n"); 74 printf("Winner: B (points: A 0, B 5)\n"); 75 total_b = total_b + 5; 76 } 77 else if (a == 2 && b == 1) { 78 printf(" B wins and gets 1 points.\n"); 79 printf("Winner: B (points: A 0, B 1)\n"); 80 total_b = total_b + 1; 81 } 82 else if (a == 3 && b == 2) { 83 printf(" B wins and gets 2 points.\n"); 84 printf("Winner: B (points: A 0, B 2)\n"); 85 total_b = total_b + 2; 86 } 87 88 printf("%2d %2d %2d\n", total_a, total_b, i); 89 printf("\n"); 90 91 i++; 92 93 } while((total_a < 21) && (total_b < 21) && (i <= 10)); 94 95 printf("\n*****GAME OVER *****\n"); 96 97 if (i == 11) { 98 printf("10 times. Game is over.\n"); 99 return 0; 100 } 101 if (total_a > total_b) { 102 printf("A wins. \n"); 103 } 104 else if (total_a < total_b) { 105 printf("B wins. \n"); 106 } 107 return 0; 108}
投稿2018/04/29 09:03
総合スコア1195
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
ベストアンサー
元のコードを一部修正する形で書きました。
do whileで後判定することと
対戦者の点数の保持を追加です。
動作確認はしていません。
質問者様のコードは関数化や
勝ち負け判定テーブル(じゃんけんの勝ち¥あいこ/負けと
点数のテーブルを配列に定義する あとで簡単に点数やルールを修正できます)
などしていけますね。
C
1int main(void){ 2 3 int a; 4 int b; 5 int i = 0; 6 int total_a = 0; 7 int total_b = 0; 8 9 do { 10 printf("Game input: "); 11 scanf("%d %d", &a, &b); 12 13 if ((a==1 || a==2 || a==3) && (b==1 || b==2 || b==3)) { 14 break; 15 } 16 17 printf("Invalid input (Rock: 1, Scissors: 2, Paper: 3),please try again:"); 18 19 } 20 21 switch (a) { 22 case 1: 23 printf("A: Rock,"); 24 break; 25 26 case 2: 27 printf("A: Scissors,"); 28 break; 29 30 case 3: 31 printf("A: Paper,"); 32 break; 33 } 34 switch (b) { 35 case 1: 36 printf(" B: Rock;"); 37 break; 38 39 case 2: 40 printf(" B: Scissors;"); 41 break; 42 43 case 3: 44 printf(" B: Paper;"); 45 break; 46 } 47 48 if (a == b) { 49 printf("Draw (points: A 0, B 0)\n"); 50 } 51 52 else if (a == 1 && b == 2) { 53 printf(" A wins and gets 1 points.\n"); 54 printf("Winner: A (points: A 1, B 0)\n"); 55 total_a = total_a + 1 56 } 57 else if (a == 2 && b == 3) { 58 printf(" A wins and gets 2 points.\n"); 59 printf("Winner: A (points: A 2, B 0)\n"); 60 total_a = total_a + 2 61 } 62 else if (a == 3 && b == 1) { 63 printf(" A wins and gets 5 points.\n"); 64 printf("Winner: A (points: A 5, B 0)\n"); 65 total_a = total_a + 5 66 } 67 68 else if (a == 1 && b == 3) { 69 printf(" B wins and gets 1 points.\n"); 70 printf("Winner: B (points: A 0, B 5)\n"); 71 total_b = total_b + 5 72 } 73 else if (a == 2 && b == 1) { 74 printf(" B wins and gets 1 points.\n"); 75 printf("Winner: B (points: A 0, B 1)\n"); 76 total_b = total_b + 1 77 } 78 else if (a == 3 && b == 2) { 79 printf(" B wins and gets 2 points.\n"); 80 printf("Winner: B (points: A 0, B 2)\n"); 81 total_b = total_b + 2 82 } 83 } while(total_a >= 21 || total_b >= 21 || i >= 10); 84 85 printf("*****GAME OVER *****\n"); 86 87 if (total_a > total_b) { 88 printf("A wins. \n"); 89 } 90 else if (total_a total_b) 91 printf("B wins. \n"); 92 } 93 return(0); 94}
投稿2018/04/29 03:47
総合スコア1195
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/29 04:36 編集
2018/04/29 04:46
2018/04/29 07:15
2018/04/29 08:46
2018/04/29 09:28
2018/04/29 09:45
2018/04/29 09:48
2018/04/29 09:59
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/29 02:44
2018/04/29 02:47