-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_number.m
102 lines (82 loc) · 2.09 KB
/
guess_number.m
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
answer = [0 0 0 0];
number_table = [0 0 0 0];
message = 'set answer number: ';
while true
x = input(message);
%seperate the number individually
for i = 4:-1:1
answer(i) = mod(x,10);
x = fix(x/10);
end
%check is there any same number
same_number = 0;
for i=1:4
temp = answer(i);
if i<4 && same_number==0
for j = i+1:4
if temp==answer(j)
same_number = 1;
fprintf('Your input has same number.\n');
fprintf('Please input again.\n');
break;
end
end
else
break;
end
end
if same_number==0
break;
end
end
%start guessing
guess = [0 0 0 0];
check = ['c' 'c' 'c' 'c'];
while ~isequal(check,['a' 'a' 'a' 'a'])
x = input('Your guess is:');
%seperate the number individually
for i = 4:-1:1
guess(i) = mod(x,10);
x = fix(x/10);
end
same_number = 0;
%check is there same number
for i=1:4
temp = guess(i);
if i<4 && same_number==0
for j = i+1:4
if temp==guess(j)
same_number = 1;
fprintf('Your input has same number.\n');
fprintf('Please input again.\n\n');
break;
end
end
else
break;
end
end
if same_number==0
%compare his input with answer
for i=1:4
if guess(i)==answer(i)
check(i) = 'a';
else
for j=1:4
if guess(i)==answer(j) && i~=j
check(i) = 'b';
break;
else
check(i) = 'c';
end
end
end
end
fprintf('compare solusion:');
for i = 1:4
fprintf('%c',check(i));
end
fprintf('\n\n');
end
end
fprintf('Bingo!! Congratualation\n');