-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_mastermind_add.c
148 lines (125 loc) · 3.12 KB
/
my_mastermind_add.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "my_mastermind.h"
#define MAX_INPUT_SIZE 128
static int compare_codes(char digit, char *input, int index);
char *create_secret()
{
// set up return str that contains the code
char* secretPtr = (char*)malloc(5 * sizeof(char));
secretPtr[4] = '\0';
// seed the random nbr generator for rand()
srand(time(0));
// fill the code with nbrs from 0 to 7, convert the int to char while doing it
for (int i = 0; i < 4; i++)
{
int randNumber = (rand() % 8);
secretPtr[i] = (randNumber + '0');
}
return secretPtr;
}
char *read_input()
{
char *input = NULL;
char buff[MAX_INPUT_SIZE];
my_memset(buff, '\0', MAX_INPUT_SIZE - 1);
read(0, buff, 5);
buff[4] = '\0';
// make 'quitting' possible
if (my_strcmp(buff, "quit") == 0)
{
input = my_strdup(buff);
return input;
}
for (int i = 0; i < 4; i++)
{
if ((buff[i] < '0' || buff[i] > '7'))
{
printf("Wrong input!\n");
return NULL;
}
}
input = my_strdup(buff);
return input;
}
int analyze_input(char *input, char *code)
{
int well = 0;
int miss = 0;
// check for well-placed pieces
for (int i = 0; i < my_strlen(input); i++)
{
if (input[i] == code[i])
well++;
}
// check for misplaced pieces
for (int i = 0; i < my_strlen(code); i++)
{
// increase nbr of missed pieces if match between the two codes is found
if (compare_codes(code[i], input, i))
miss++;
}
// display game info
if (well < 4)
{
printf("Well placed pieces: %d\n", well);
printf("Misplaced pieces: %d\n", miss);
}
return well;
}
static int compare_codes(char digit, char *input, int index)
{
// compare current digit of secret code to all of input
for (int i = 0; i < my_strlen(input); i++)
{
if (digit == input[i] && index != i) // if match is not in the same place, it's a 'missed' piece
return 1;
}
return 0;
}
int my_strlen(char *string)
{
int counter = 0;
while (string[counter] != '\0')
counter++;
return counter;
}
char *my_strdup(char* str)
{
char* dupStr;
int len = my_strlen(str) + 1;
dupStr = (char*)malloc(sizeof(char) * len);
if (dupStr == NULL)
{
printf("malloc of size %d failed!\n", len);
return NULL;
}
for (int i = 0; i < len; i++)
dupStr[i] = str[i];
dupStr[len-1] = '\0';
return dupStr;
}
int my_strcmp(char *s1, char *s2)
{
while (*s1)
{
// if characters differ, or end of the second string is reached
if (*s1 != *s2)
break;
// move to the next pair of characters
s1++;
s2++;
// return the ASCII difference after converting `char*` to `unsigned char*`
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
void my_memset(char *str, char c, int n)
{
int i = 0;
for (; i < n; i++)
str[i] = c;
str[i] = '\0';
}