Skip to content
Snippets Groups Projects
Commit 738d08fb authored by tanguy.cavagna's avatar tanguy.cavagna :desktop:
Browse files

Better control logic and added correct letters in next try by default

parent 6e1be2f9
No related branches found
No related tags found
No related merge requests found
......@@ -237,7 +237,14 @@ void toggle_help() {
void show_gameboard(int tries_count, int word_length, char **tries, int **validations, int score) {
for (int i = 0; i < tries_count; i++) {
// Draw current try letters
for (size_t j = 0; j < strlen(tries[i]); j++) {
for (int j = 0; j < word_length; j++) {
// Placeholder
if (tries[i][j] == '\0') {
mvaddch(GAMEBOARD_Y_OFFSET + i, TERM_MID_COLS - word_length + (j * 2), '_');
continue;
}
// Letters
if (validations[i][j] == 1)
attron(COLOR_PAIR(ORANGE_PAIR));
if (validations[i][j] == 2)
......@@ -247,10 +254,6 @@ void show_gameboard(int tries_count, int word_length, char **tries, int **valida
attroff(COLOR_PAIR(GREEN_PAIR));
attroff(COLOR_PAIR(ORANGE_PAIR));
}
// Draw remaning try letters placeholder
for (size_t j = 0; j < word_length - strlen(tries[i]); j++)
mvaddstr(GAMEBOARD_Y_OFFSET + i, TERM_MID_COLS + (word_length / 2) - (j * 2), " _");
}
move(GAMEBOARD_Y_OFFSET - 2, TERM_MID_COLS - word_length - 3);
......
......@@ -20,6 +20,7 @@ void initialize_game() {
_game_finished = false;
current_try_id = 0;
current_try_letter_id = 0;
score = 0;
// Tries and answer setup
chosen_word = get_random_word();
......@@ -125,9 +126,9 @@ void render_game() {
bool validate_letter(int key) { return (islower(key) || isupper(key)); }
/**
* @brief Validate the last guess
* @brief Control the placement of each letter
*/
int *validate_guess(char answer[WORD_LENGHT], char try[WORD_LENGHT]) {
int *letter_placement_control(char answer[WORD_LENGHT], char try[WORD_LENGHT]) {
int *validation = calloc(WORD_LENGHT, sizeof(int));
char cpy[WORD_LENGHT + 1];
strcpy(cpy, answer);
......@@ -153,6 +154,36 @@ int *validate_guess(char answer[WORD_LENGHT], char try[WORD_LENGHT]) {
return validation;
}
/**
* @brief Is a try valid
*
* @param try
* @return true
* @return false
*/
bool validate_guess(char try[WORD_LENGHT]) {
// Length check
if (strlen(try) < WORD_LENGHT)
return false;
// Presence check
char **bank = get_bank();
bool present = false;
for (int i = 0; i < get_bank_size(); i++) {
if (!strcmp(bank[i], try))
present = true;
}
if (!present) {
current_try_letter_id = 0;
for (int i = 0; i < WORD_LENGHT; i++)
tries[current_try_id][i] = '\0';
return false;
}
return true;
}
/**
* @brief Handle controls
*
......@@ -169,33 +200,25 @@ void handle_controls(int key) {
break;
case KEYBIND_GUESS:
// Length check
if (strlen(tries[current_try_id]) < WORD_LENGHT)
if (!validate_guess(tries[current_try_id]))
break;
// Presence check
char **bank = get_bank();
bool present = false;
for (int i = 0; i < get_bank_size(); i++) {
if (!strcmp(bank[i], tries[current_try_id]))
present = true;
}
if (!present) {
current_try_letter_id = 0;
for (int i = 0; i < WORD_LENGHT; i++)
tries[current_try_id][i] = '\0';
break;
}
// Validations
int *tmp = validate_guess(chosen_word, tries[current_try_id]);
int *tmp = letter_placement_control(chosen_word, tries[current_try_id]);
for (int i = 0; i < WORD_LENGHT; i++)
validations[current_try_id][i] = tmp[i];
free(tmp);
current_try_id += 1;
current_try_letter_id = 0;
// Place correct letters from previous in the current one
if (current_try_id < TRIES_COUNT) {
for (int i = 0; i < WORD_LENGHT; i++) {
if (validations[current_try_id - 1][i] == LETTER_PLACED)
tries[current_try_id][i] = tries[current_try_id - 1][i];
}
}
break;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment