2 * This file is licensed under the GPLv2 license
4 * Luiz Fernando N. Capitulino
5 * <lcapitulino@gmail.com>
15 #define ALPHABET_SIZE 26
18 const char *orig_word
;
24 char alphabet
[ALPHABET_SIZE
];
33 static int max_times
= 6;
35 static int player_game_init(struct player_game
*p
,
41 p
->len
= strlen(word
);
43 if ((int) p
->len
>= max_times
)
46 p
->word
= malloc(p
->len
);
49 memset(p
->word
, '-', p
->len
);
50 p
->word
[p
->len
] = '\0';
54 p
->hits
= p
->errors
= 0;
56 for (i
= 'a'; i
<= 'z'; i
++)
57 p
->alphabet
[i
- 'a'] = i
;
62 static void player_destroy(const struct player_game
*p
)
67 static void player_show_status(const struct player_game
*p
)
74 for (i
= 0; i
< (int) p
->len
; i
++)
75 printf("%c", p
->word
[i
]);
78 printf(" (%s)", p
->orig_word
);
82 printf("Letras disponiveis: ");
83 for (i
= 0; i
< ALPHABET_SIZE
; i
++)
84 printf("%c", p
->alphabet
[i
]);
87 printf("Falhas disponiveis: %d\n\n", max_times
- p
->errors
);
89 if (p
->errors
== max_times
- 1)
90 printf("Dica: %s\n\n", p
->tip
);
93 static int count_letter(const char *word
, char c
)
99 for (i
= 0; word
[i
] != '\0'; i
++)
106 static enum game_result
end_of_game(const struct player_game
*p
)
108 if (p
->hits
== (int) p
->len
)
111 if (p
->errors
== max_times
)
117 static void show_final_stats(const struct player_game
*p
,
118 enum game_result result
)
122 printf("Parabens! Acertou ");
125 printf("Azar! Perdeu ");
129 printf("usando %d letras (%d erros)\n", p
->hits
+ p
->errors
,p
->errors
);
131 printf("\nA palavra era: %s\n", p
->orig_word
);
135 static void player_turn(struct player_game
*p
)
140 printf("-> Digite uma letra e pressione enter: ");
145 if (c
< 'a' || c
> 'z') {
146 mprint("Apenas sao permitidas letras de a-z\n");
150 if (p
->alphabet
[c
- 'a'] == '-') {
151 mprint("Letra ja escolhida antes\n");
155 for (i
= 0; i
< p
->len
; i
++) {
156 if (p
->orig_word
[i
] == c
&& p
->word
[i
] == '-') {
163 if (count_letter(p
->orig_word
, c
) == count_letter(p
->word
, c
))
164 p
->alphabet
[c
- 'a'] = '-';
166 if (p
->word
[i
] != c
) {
172 void run_game(const char *word
, const char *tip
)
174 struct player_game player
;
176 player_game_init(&player
, word
, tip
);
179 enum game_result res
;
181 player_show_status(&player
);
182 player_turn(&player
);
183 res
= end_of_game(&player
);
185 player_show_status(&player
);
186 show_final_stats(&player
, res
);
191 player_destroy(&player
);