-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_mastermind.c
159 lines (140 loc) · 3.69 KB
/
my_mastermind.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
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
152
153
154
155
156
157
158
159
#include "my_lib.h"
#define STDIN_FILE_DESCRIPTOR 0
#define EXPECTED_ANSWER_LENGTH 4
#define EXPECTED_INPUT_LENGTH (EXPECTED_ANSWER_LENGTH + 1)
// Function that compares secret code and answer and return number of well placed and missplaced
// Function expects inputs are valid
t_guess_result check_answer(char* secret, char* answer)
{
t_guess_result result;
result.missplaced_amount = 0;
result.well_placed_amount = 0;
int place;
int index = 0;
while (answer[index] != '\0')
{
place = find_index(secret, answer[index]);
if (place == -1)
{
index++;
continue;
}
else if (answer[index] == secret[index])
{
result.well_placed_amount += 1;
}
else if (answer[place] != secret[place])
{
result.missplaced_amount += 1;
}
index++;
}
return result;
}
char* read_answer()
{
char* input;
int result;
input = malloc(sizeof(char) * EXPECTED_INPUT_LENGTH);
write(0, "> ", 2);
result = read(STDIN_FILE_DESCRIPTOR, input, EXPECTED_INPUT_LENGTH);
// Check user entered at lest minimal amount and no error happened
if (result < EXPECTED_INPUT_LENGTH)
{
return NULL;
}
// Check user entered exact amount
if (input[EXPECTED_INPUT_LENGTH - 1] != '\n')
{
return NULL;
}
input[EXPECTED_INPUT_LENGTH - 1] = '\0';
return input;
}
bool is_code_valid(char *code) {
int index;
// checks length of code, must be 4 digits
if (strlen(code) != EXPECTED_ANSWER_LENGTH)
{
return false;
}
index = 0;
while(index < EXPECTED_ANSWER_LENGTH)
{
if(code[index] > '7' || code[index] < '0')
{
printf("Each digit must be between 0 and 7 included\n");
return false;
}
index++;
}
return true;
}
void get_random(t_opt* options)
{
int random;
options->c_val = malloc(sizeof(char) * EXPECTED_INPUT_LENGTH);
int index = 0;
srand(time(NULL));
while(index < EXPECTED_ANSWER_LENGTH)
{
random = rand() % 8;
options->c_val[index] = random + '0';
index++;
}
options->c_val[index] = '\0';
}
int main(int ac, char** av)
{
t_opt* options = get_opt(ac, av);
if(options->c == 1 && strlen(options->c_val) < 2)
{
printf("Wrong option value after -c\n");
return 1;
}
if(strlen(options->c_val) == 0)
{
get_random(options);
}
if (!is_code_valid(options->c_val) )
{
printf("Secret is invalid\n");
free(options);
return 1;
}
printf("====> Will you find the secret code?\n");
printf("---\n");
int attempt = 0;
char *answer;
t_guess_result guess;
while (attempt < options->t_val)
{
printf("Round %d\n", attempt);
answer = read_answer();
if (answer != NULL && is_code_valid(answer))
{
guess = check_answer(options->c_val, answer);
// WHEN WON
if (guess.well_placed_amount == EXPECTED_ANSWER_LENGTH)
{
printf("Congratz! You did it!\n");
free(answer);
free(options);
return 0;
}
printf("Well placed pieces: %d\n", guess.well_placed_amount);
printf("Misplaced pieces: %d\n", guess.missplaced_amount);
}
else
{
printf("Wrong input! Try again\n");
}
attempt++;
free(answer);
printf("---\n");
}
printf("Exeded amount of attempts\n");
printf("The right answer was %s\n", options->c_val);
free(options);
return 1;
}