-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp05ex14.c
More file actions
151 lines (141 loc) · 2.48 KB
/
Copy pathp05ex14.c
File metadata and controls
151 lines (141 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*** p05ex14.c ***/
/*** ps20 ***/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
int countBlow(char s1[], char s2[])
{
int blow = 0;
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (s1[i] == s2[j] && i != j)
{
blow++;
}
}
}
return blow;
}
int countHit(char s1[], char s2[])
{
int hit = 0;
int i;
for (i = 0; i < 4; i++)
{
if (s1[i] == s2[i])
{
hit++;
}
}
return hit;
}
int checkString(char s[])
{
int i, j;
int len = 0;
while (s[len] != '\0')
{
++len;
}
if (len != 4)
{
return 0;
}
for (i = 0; i < len; i++)
{
if (!isdigit(s[i]))
{
return 0;
}
}
for (i = 0; i < len; i++)
{
for (j = i + 1; j < len; j++)
{
if (s[i] == s[j])
{
return 0;
}
}
}
return 1;
}
void generateNumber(char theNumber[])
{
int i;
int j = 0;
char tmp[30];
do
{
for (i = 0; i < 10; i++)
{
tmp[j] = i + '0';
++j;
}
tmp[j] = '\0';
for (i = 0; i < 4; i++)
{
theNumber[i] = tmp[rand() % 10];
}
theNumber[i] = '\0';
} while (checkString(theNumber) == 0);
}
int main()
{
char theNumber[5];
char guess[5];
int hit, blow;
int i;
int count = 0;
srand(time(NULL));
generateNumber(theNumber);
do
{
printf("number: ");
gets(guess);
if (checkString(guess) == 0)
{
printf("input error.\n");
continue;
}
hit = countHit(theNumber, guess);
blow = countBlow(theNumber, guess);
printf(" Hit: %d, Blow: %d\n", hit, blow);
count++;
} while (hit != 4);
printf("あなたは %d 回の試行で勝利しました\n", count);
return 0;
}
/*** 結果 ***
number: 1234
Hit: 1, Blow: 0
number: 4561
Hit: 0, Blow: 1
number: 1789
Hit: 0, Blow: 2
number: 7805
Hit: 0, Blow: 1
number: 7298
Hit: 2, Blow: 1
number: 2798
Hit: 1, Blow: 2
number: 7489
Hit: 0, Blow: 2
number: 9258
Hit: 1, Blow: 1
number: 9257
Hit: 2, Blow: 1
number: 6927
Hit: 2, Blow: 2
number: 2697
Hit: 2, Blow: 2
number: 2967
Hit: 1, Blow: 3
number: 6297
Hit: 4, Blow: 0
あなたは 13 回の試行で勝利しました
*************/