-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_functions.c
276 lines (244 loc) · 6.3 KB
/
my_functions.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "my_headers.h"
// checks for -c flag, returns secret code if it's given
char *check_c_opt(int argc, char *argv[], char* master_code){
int i = 1;
while(i < argc){
if(my_strcmp(argv[i], "-c")){
i++;
if(my_strlen(argv[i]) != 4){
printf(COLOR_RED "Wrong input! The code must be 4 digits!" COLOR_RESET "\n");
break;
}
else{
if(code_validator(argv[i])){
my_strcpy(master_code, argv[i]);
}
}
}
i++;
}
return master_code;
}
//checks for -t flag, returns number of attempts
int check_t_opt(int argc, char *argv[]){
int attempts = 10;
int i = 1;
while(i < argc){
if(my_strcmp(argv[i], "-t")){
i++;
if((atoi(argv[i])) > 10 || (atoi(argv[i])) < 1){
printf(COLOR_RED "The number of attempts was set to 10!" COLOR_RESET "\n");
break;
}
else{
attempts = atoi(argv[i]);
break;
}
}
i++;
}
return attempts;
}
//reads from standard input, returns user's guess str
char *my_guess_reader(char *guess, char *my_buf){
int i = 0;
while (1){
read(0, my_buf, 1);
if(*my_buf == '\n'){
break;
}
guess[i] = *my_buf;
i++;
}
guess[i] = '\0';
return guess;
}
//compares secret code to user's guess
bool my_guess_check(char *master_code, char *guess){
int well_placed = 0;
int misplaced_pieces = 0;
for(int i=0; i<4; i++){
if(master_code[i]==guess[i]){
well_placed++;
}
else{
for(int j=0; j<4; j++){
if(master_code[i]==guess[j]){
misplaced_pieces++;
}
}
}
}
if(well_placed == 4){
printf(COLOR_YELLOW "WOWZA! You guessed it!" COLOR_RESET "\n");
return true;
}
else{
printf("Well placed pieces: %d\n", well_placed);
printf("Misplaced pieces: %d\n", misplaced_pieces);
return false;
}
}
//compares if two strings are of an equal value
bool my_strcmp(char* param_1, char* param_2){
int i = 0;
while(param_1[i] != '\0' || param_2[i] != '\0'){
if(param_1[i]>param_2[i]){
return false;
}
if(param_1[i]<param_2[i]){
return false;
}
if(param_1[i]==param_2[i]){
i++;
}
}
return true;
}
//calculates the length of a string
int my_strlen(char* param_1){
int length = 0;
int i = 0;
while (param_1[i] != '\0'){
length++;
i++;
}
return length;
}
//copies the string pointed to by param_2 to the buffer pointed by param_1
char* my_strcpy(char* param_1, char* param_2){
int length = my_strlen(param_1);
int length_2str = my_strlen(param_2);
int k = 0;
while (k < length_2str) {
param_1[length] = param_2[k];
k++;
length++;
}
param_1[length] = '\0';
return param_1;
}
//searches for the first occurrence of the character c in the string. Returns true if present
bool my_strchr(char* param_1, char param_2){
int i = 0;
while (param_1[i] != '\0'){
if(param_2 == param_1[i]){
return true;
}
i++;
}
return false;
}
//checks if the char occurs twice in array
bool my_duplchr(char* param_1, char param_2){
int count = 0;
int i = 0;
while (param_1[i] != '\0'){
if(param_2 == param_1[i]){
count++;
}
i++;
}
if(count>1){
return true;
}
return false;
}
//checks if int is not present in the array
bool my_strint(int* param_1, int param_2){
int i = 0;
while(i<4){
if(param_2 == param_1[i]){
return false;
}
i++;
}
return true;
}
//generates an array of random unique numbers in the range from 0 to 7
int *my_secretcode(int* secret_code){
srand(time(0));
int i = 0;
int number;
while(i < 4){
number = rand() % 8;
if(i==0){
secret_code[i] = number;
i++;
}
else if(my_strint(secret_code, number)){
secret_code[i] = number;
i++;
}
}
return secret_code;
}
//converts int data type to string data type in the range from 0 to 7, default length 4
char* my_itoa(char* master_code, int *secret_code){
int i=0;
while(i<4){
char number;
if(secret_code[i]==0){
number = 48;
}
else if(secret_code[i]==1){
number = 49;
}
else if(secret_code[i]==2){
number = 50;
}
else if(secret_code[i]==3){
number = 51;
}
else if(secret_code[i]==4){
number = 52;
}
else if(secret_code[i]==5){
number = 53;
}
else if(secret_code[i]==6){
number = 54;
}
else if(secret_code[i]==7){
number = 55;
}
master_code[i] = number;
i++;
}
master_code[i] = '\0';
return master_code;
}
//checks if a secret code is of a valid length
bool valid_length (char *master_code){
if(my_strlen(master_code) != 4){
printf(COLOR_RED "Wrong input! The code must be 4 digits!" COLOR_RESET "\n");
return false;
}
return true;
}
//checks if a string is a valid input: made of valid digits and doesn't contain duplicates
bool code_validator(char *master_code){
int i = 0;
while(master_code[i] != '\0'){
if(master_code[i] < 48 || master_code[i] > 55){
printf(COLOR_RED "Wrong input! Only digits from 0 to 7 are allowed!" COLOR_RESET "\n");
return false;
}
else if(my_duplchr(master_code, master_code[i])){
printf(COLOR_RED "Wrong input! No duplicates allowed!" COLOR_RESET "\n");
return false;
}
i++;
}
return true;
}
//returns a pointer to a string that has been emptied
char *my_emptystr(char* param){
int i =0;
int length = my_strlen(param);
while(i < length){
param[i] = '\0';
i++;
}
return param;
}