teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

プログラムを書き換えました

2018/04/29 08:44

投稿

mMs7ScOiWNZzOie
mMs7ScOiWNZzOie

スコア14

title CHANGED
File without changes
body CHANGED
@@ -6,83 +6,66 @@
6
6
  #define _CRT_SECURE_NO_WARNINGS
7
7
  #include <stdio.h>
8
8
 
9
- int main(void){
9
+ int main(void) {
10
+ int a = 0, b = 0;
11
+ int points_a = 0;
12
+ int points_b = 0;
13
+ int turns = 0;
14
+ char inputs[][10] = { "Rock","Scissors","Paper" };
10
15
 
16
+ for (turns = 0; turns < 10; turns++) {
17
+ while (1) {
18
+ printf("Input for game number %d: ", turns + 1);
19
+ scanf("%d %d", &a, &b);
20
+ if (a < 1 || a>3 || b < 1 || b>3)
21
+ printf("Invalid input(Rock:1,Scissors:2,Paper:3),please try again:\n\n");
22
+ else
11
- int a;
23
+ break;
24
+ }
12
- int b;
25
+ printf("A plays %s,B plays %s: ", inputs[a - 1], inputs[b - 1]);
13
26
 
14
- while(1){
27
+ if (a == b) {
15
- printf("Game input: ");
28
+ printf("draw.\n");
16
- scanf("%d %d", &a, &b);
17
-
18
- if ((a==1 || a==2 || a==3) && (b==1 || b==2 || b==3)) {
19
- break;
20
- }
29
+ }
21
-
30
+ else if ((a == 1 && b == 2) || (a == 2 && b == 1)) {
22
-
23
- printf("Invalid input (Rock: 1, Scissors: 2, Paper: 3),please try again:");
31
+ printf("%c wins and gets 1points.\n", (a < b ? 'A' : 'B'));
32
+ if (a < b)
33
+ points_a += 1;
34
+ else
35
+ points_b += 1;
36
+ }
24
37
 
38
+ else if ((a == 1 && b == 3) || (a == 3 && b == 1)) {
39
+ printf("%c wins and gets 5 points.\n", (a > b ? 'A' : 'B'));
40
+ if (a > b)
41
+ points_a = 5;
42
+ else
43
+ points_b = 5;
25
- }
44
+ }
45
+ else if ((a == 2 && b == 3) || (a == 3 && b == 2)) {
46
+ printf("%c wins and gets 2 points.\n", (a < b ? 'A' : 'B'));
47
+ if (a > b)
48
+ points_a += 2;
49
+ else
50
+ points_b += 2;
51
+ }
52
+ else {
53
+ printf("Unknown error\n");
54
+ return -1;
55
+ }
26
56
 
57
+ printf("Current number of points: A - %d, B - %d. \n\n", points_a, points_b);
27
58
 
28
- switch (a) {
29
- case 1:
30
- printf("A: Rock,");
59
+ if (points_a >= 21 || points_b >= 21)
31
- break;
60
+ break;
32
-
33
- case 2:
34
- printf("A: Scissors,");
35
- break;
36
-
37
- case 3:
38
- printf("A: Paper,");
39
- break;
40
61
  }
41
- switch (b) {
42
- case 1:
43
- printf(" B: Rock;");
62
+ if (points_a == points_b) {
44
- break;
45
-
46
- case 2:
47
- printf(" B: Scissors;");
63
+ printf("Draw(points:A %d,B %d)\n", points_a, points_b);
48
- break;
49
-
50
- case 3:
51
- printf(" B: Paper;");
52
- break;
53
64
  }
54
-
55
- if (a == b) {
65
+ else {
56
- printf("Draw (points: A 0, B 0)\n");
66
+ printf("\nWinner: %c (points: A %d, B %d)\n", (points_a > points_b) ? 'A' : 'B', points_a, points_b);
57
67
  }
58
-
59
- else if (a == 1 && b == 2) {
60
- printf(" A wins and gets 1 points.\n");
61
- printf("Winner: A (points: A 1, B 0)\n");
62
- }
63
- else if (a == 2 && b == 3) {
64
- printf(" A wins and gets 2 points.\n");
65
- printf("Winner: A (points: A 2, B 0)\n");
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
- }
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
- }
76
- else if (a == 2 && b == 1) {
77
- printf(" B wins and gets 1 points.\n");
78
- printf("Winner: B (points: A 0, B 1)\n");
79
- }
80
- else if (a == 3 && b == 2) {
81
- printf(" B wins and gets 2 points.\n");
82
- printf("Winner: B (points: A 0, B 2)\n");
83
- }
84
-
85
-
86
68
  return(0);
87
69
  }
70
+
88
71
  ```