-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecret.c
86 lines (62 loc) · 1.48 KB
/
secret.c
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
/**
* \file secret.c
* The super mind master's secret.
*/
#include <time.h>
#include <stdlib.h>
#include "secret.h"
/**
* Put a new random code in a pointed location.
* \param code a pre-allocated code location
* \return 0 in case of success
*/
int change_code(code_t *code) {
int i;
srand(time(NULL)); // should only be called once
for(i=0; i<CODE_LENGHT; i++) {
(*code)[i]=rand() % COLORS_NB;
}
}
/**
* Verify that all the keys in a code are defined.
* \return 1 is there a no UNDEFINED value in code, 0 otherwise
*/
int is_completed(code_t *code){
int i;
for(i=0; i<CODE_LENGHT; i++) {
if((*code)[i]==UNDEFINED) {
return 0;
}
}
return 1;
}
/**
* Compare a combinaison to the secret code.
*/
int *test(code_t *combinaison, code_t *secret){
int *test = malloc(sizeof(int)*2);
int i, j;
int counted[CODE_LENGHT];
test[0]=0;
test[1]=0;
for(i=0; i<CODE_LENGHT; i++) {
if( (*combinaison)[i] == (*secret)[i] ) {
test[0]++;
counted[i]=1;
} else {
counted[i]=0;
}
}
for(i=0; i<CODE_LENGHT; i++) {
if( (*combinaison)[i] != (*secret)[i] ) {
for(j=0; j<CODE_LENGHT; j++) {
if(!counted[j] && (*combinaison)[i] == (*secret)[j] ) {
test[1]++;
counted[j]=1;
break;
}
}
}
}
return test;
}