回答編集履歴
1
説明の追加
answer
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
こんにちは、
|
2
2
|
プログラムの都合上 Rock: 0, Scissors: 1, Paper: 2にしております。
|
3
|
-
|
3
|
+
|
4
4
|
ジャンケンの判定をするところですが
|
5
|
-
重要になってくるのが(
|
5
|
+
重要になってくるのが(user1の手 - user2の手 + 3) % 3です。この値をResult_numと名づけることにします。例えば、
|
6
|
-
|
|
6
|
+
|user1の手|user2の手|結果|Result_num|
|
7
7
|
|:--|:--:|--:|
|
8
8
|
|Rock|Rock|Draw|0|
|
9
|
-
|Rock|Paper|
|
9
|
+
|Rock|Paper|user1 lose|1|
|
10
|
-
|Scissors|Paper|
|
10
|
+
|Scissors|Paper|user2 win|2|
|
11
11
|
|
12
12
|
のようになります。
|
13
13
|
これを使うとif文やswitch文を使わなくても判定できます。
|
@@ -18,73 +18,67 @@
|
|
18
18
|
ソースが煩雑になるため、このような形にしました。
|
19
19
|
また、判定の文字(win,lose,draw)は
|
20
20
|
```
|
21
|
-
char result_text[3][15] = {"Draw","
|
21
|
+
char result_text[3][15] = {"Draw","user1 Lose","user1 Win!!"};
|
22
22
|
```
|
23
23
|
にすることで番号指定で文字を表示することができます。
|
24
24
|
|
25
25
|
私が書いたソースコードを載せておきます。
|
26
|
+
|
26
27
|
```C
|
27
28
|
#include <stdio.h>
|
28
29
|
#include <stdlib.h>
|
29
|
-
#include <time.h>
|
30
30
|
|
31
31
|
#define BUFSIZE 256
|
32
32
|
#define HAND 3
|
33
33
|
|
34
|
-
void judge(int,int);
|
35
|
-
|
36
34
|
int main(void)
|
37
35
|
{
|
38
36
|
|
39
37
|
char buf[BUFSIZE];
|
40
|
-
char result_text[
|
38
|
+
char result_text[HAND][20] = {"Draw","user1 Lose","user1 Win"};
|
41
|
-
char hand[
|
39
|
+
char hand[HAND][10] = {"Rock","Scissors","Paper"};
|
42
40
|
int i;
|
43
41
|
int count = 0;
|
44
|
-
int
|
42
|
+
int user_hand[2];
|
45
|
-
int
|
43
|
+
int user_point[2] = {0,0};
|
46
44
|
int result_num;
|
47
|
-
int your_point = 0;
|
48
|
-
int cpu_point = 0;
|
49
45
|
int result;
|
50
|
-
|
46
|
+
|
51
|
-
|
52
47
|
while(1){
|
53
48
|
printf("%d回目",count + 1);
|
54
49
|
printf("Rock : 0 , Scissors: 1, Paper : 2\n");
|
50
|
+
for(i = 0; i < 2; i++){
|
55
|
-
|
51
|
+
do{
|
56
|
-
|
52
|
+
fgets(buf,sizeof(buf),stdin);
|
57
|
-
|
53
|
+
user_hand[i] = atoi(buf);
|
58
|
-
|
54
|
+
if(user_hand[i] < 0 || user_hand[i] > 2){
|
59
|
-
|
55
|
+
printf("再入力してください。\n");
|
60
|
-
|
56
|
+
}
|
61
|
-
|
57
|
+
}while(user_hand[i] < 0 || user_hand[i] > 2);
|
62
|
-
|
58
|
+
}
|
63
|
-
result = (your_hand - cpu_hand + 3) % 3;
|
64
|
-
printf("あなた:%s cpu:%s",hand[your_hand],hand[cpu_hand]);
|
65
|
-
printf("%s\n",result_text[result]);
|
66
59
|
|
60
|
+
result = (user_hand[0] - user_hand[1] + 3) % HAND;
|
61
|
+
printf("user1:%s user2:%s\n",hand[user_hand[0]],hand[user_hand[1]]);
|
62
|
+
|
63
|
+
printf("%s \n",result_text[result]);
|
64
|
+
|
67
|
-
|
65
|
+
if(result != 0){
|
68
|
-
case 1:
|
69
|
-
cpu_point += (cpu_hand + 1) * 3;
|
70
|
-
break;
|
71
|
-
case 2:
|
72
|
-
|
66
|
+
user_point[result - 1] += (user_hand[result - 1] + 1) * 3;
|
73
|
-
break;
|
74
|
-
default:
|
75
|
-
break;
|
76
67
|
}
|
68
|
+
|
77
|
-
printf("
|
69
|
+
printf("user1:%d user2:%d\n",user_point[0],user_point[1]);
|
78
70
|
count++;
|
79
71
|
if(count >= 10){
|
80
72
|
break;
|
81
73
|
}
|
82
|
-
else if(
|
74
|
+
else if(user_point[0] >= 21 || user_point[1] >= 21){
|
83
75
|
break;
|
84
76
|
}
|
85
77
|
}
|
86
78
|
printf("終了\n");
|
87
|
-
printf("
|
79
|
+
printf("user1の点数:%d user2の点数:%d\n",user_point[0],user_point[1]);
|
88
80
|
return 0;
|
89
81
|
}
|
82
|
+
|
83
|
+
|
90
84
|
```
|