-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_mastermind.c
145 lines (118 loc) · 2.8 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int my_strchr(char* str, char c) {
for (int i = 0; str[i]; i++) {
if (str[i] == c) {
return 1;
}
}
return 0;
}
char* my_rand(char* secret_code) {
int i = 0;
srand(time(0));
char valid_numbers;
while (i < 4) {
valid_numbers = (rand() % 10) + '0';
if (my_strchr(secret_code, valid_numbers) == 0) {
secret_code[i++] = valid_numbers;
}
}
return secret_code;
}
int my_atoi(char* str) {
int i = 0;
int num = 0;
int flag = 0;
if (str[i] == '-') {
flag = 1;
i = 1;
}
while (str[i] >= '0' && str[i] <= '9') {
num *= 10;
num = num + (str[i++] - '0');
}
if (flag == 1) {
num *= -1;
}
return num;
}
int my_strlen(char* str) {
int i = 0;
while (str[i]) {
i++;
}
return i;
}
int my_strcmp(char* p1, char* p2) {
int len1 = my_strlen(p1);
int len2 = my_strlen(p2);
if (len1 != len2) {
return len1 - len2;
}
for (int i = 0; i < len1; i++) {
if (p1[i] != p2[i]) {
return p1[i] - p2[i];
}
}
return 0;
}
char* my_strcpy(char* c1, char* c2) {
int i = 0;
while (c2[i]) {
c1[i] = c2[i];
i++;
}
return c1;
}
char* my_scanf() {
char c;
int i = 0;
char* str = (char*)calloc(6, sizeof(char)); // Free this memory later
printf(">");
while (read(0, &c, 1)) {
if (c == '\n' || i == 5) {
return str;
}
str[i] = c;
i++;
}
return str;
}
void my_gameprocess(int attempts, char* secret_code) {
int i = 0;
char* entercode;
printf("Will you find the secret code?\nPlease enter a valid guess\n");
while (i < attempts) {
printf("Round %d\n", i);
if (my_strcmp(entercode = my_scanf(), "stop") == 0) {
free(entercode);
return;
}
printf("%s\n", entercode);
free(entercode);
i++;
}
}
void main_work(int argc, char** argv) {
char* secret_code = (char*)calloc(5, sizeof(char));
int attempts = 10;
for (int i = 1; i < argc; i++) {
if ((my_strcmp(argv[i], "-c")) == 0) {
my_strcpy(secret_code, argv[i + 1]);
} else if ((my_strcmp(argv[i], "-t")) == 0) {
attempts = my_atoi(argv[i + 1]);
}
}
if (!secret_code[0]) {
my_rand(secret_code);
}
printf("%d\n%s\n", attempts, secret_code);
my_gameprocess(attempts, secret_code);
free(secret_code);
}
int main(int argc, char** argv) {
main_work(argc, argv);
return 0;
}