-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
67 lines (63 loc) · 1.98 KB
/
main.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
# include "my_mastermind.h"
/*
* Compares the player's guess against the secret code,
* while counting the number of well placed and misplaced
* pieces which is then given as feedback to the player.
* Returns True if the guess matches the secret code,
* or False otherwise.
*/
static bool is_correct_guess(char* guess, game_info game) {
unsigned int well_placed = 0;
unsigned int misplaced = 0;
for (int i = 0; guess[i]; i ++) {
if (guess[i] == game.code[i]) {
well_placed++;
game.pieces[guess[i] - '0'] -= 1;
}
}
for (int i = 0; guess[i]; i ++) {
if (guess[i] != game.code[i] && game.pieces[guess[i] - '0']) {
misplaced++;
game.pieces[guess[i] - '0'] -= 1;
}
}
if (well_placed == CODE_SIZE) {
printf("Congratz! You did it!\n");
return true;
}
else {
printf("Well placed pieces: %d\n", well_placed);
printf("Misplaced pieces: %d\n", misplaced);
return false;
}
}
int main(int ac, char** argv) {
char secret_code[] = "0000";
game_info game = {.pieces = {0,0,0,0,0,0,0,0,0}, .code = secret_code, .attempts = 0};
char guess[100];
unsigned int n = 0;
unsigned int round = 0;
bool is_next_round = true;
if (init_game(argv, ac, &game)) {
printf("Will you find the secret code?\n");
printf("Please enter a valid guess\n");
while(game.attempts) {
if (is_next_round) {
printf("---\n");
printf("Round %d\n", round++);
}
if (!(n = read_input(guess))) {
printf("exit\n");
break;
}
if (!is_valid_code(n, guess)) {
is_next_round = false;
printf("Wrong input!\n");
continue;
}
is_next_round = true;
game.attempts = is_correct_guess(guess, game) ? 0 : game.attempts - 1;
}
}
return 0;
}