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

Reformated game loop

parent c83dd25e
No related branches found
No related tags found
No related merge requests found
...@@ -186,7 +186,7 @@ void show_gameboard(int tries_count, int word_length, char **tries, int **valida ...@@ -186,7 +186,7 @@ void show_gameboard(int tries_count, int word_length, char **tries, int **valida
// Draw remaning try letters placeholder // Draw remaning try letters placeholder
for (size_t j = 0; j < word_length - strlen(tries[i]); j++) 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), "_ "); mvaddstr(GAMEBOARD_Y_OFFSET + i, TERM_MID_COLS + (word_length / 2) - (j * 2), " _");
} }
refresh(); refresh();
} }
\ No newline at end of file
...@@ -11,35 +11,78 @@ int **validations; ...@@ -11,35 +11,78 @@ int **validations;
int current_try_id = 0; int current_try_id = 0;
int current_try_letter_id = 0; int current_try_letter_id = 0;
/**
* @brief Initialize the game
*/
void initialize_game() {
chosen_word = "SALUT"; // Must be randomly picked in a dictionary
tries = calloc(TRIES_COUNT, sizeof(char *));
validations = malloc(sizeof(int *) * TRIES_COUNT);
for (int i = 0; i < TRIES_COUNT; i++) {
validations[i] = calloc(WORD_LENGHT + 1, sizeof(int));
tries[i] = calloc(WORD_LENGHT + 1, sizeof(char)); // +1 because of \0
}
}
/** /**
* @brief Terminate a game * @brief Terminate a game
*/ */
void terminate_game() { _game_finished = true; } void terminate_game() {
_game_finished = true;
for (int i = 0; i < TRIES_COUNT; i++) {
free(tries[i]);
free(validations[i]);
}
free(tries);
free(validations);
}
/** /**
* @brief Add a letter to the current try * @brief Condition(s) to lose a game
* *
* @param letter * @return true
* @return false
*/ */
void add_letter_to_try(char letter) { bool lose_condition() { return (current_try_id > TRIES_COUNT - 1); }
tries[current_try_id][current_try_letter_id] = letter;
current_try_letter_id += 1; /**
* @brief Render the game for the selected gamemode
*/
void render_game() {
switch (gamemode) {
case GAMEMODE_SOLO:
printw_center("Solo", 5);
break;
case GAMEMODE_1_V_1:
printw_center("Player 1", 5);
break;
case GAMEMODE_COMPUTER:
printw_center("Computer assisted", 5);
break;
}
show_gameboard(TRIES_COUNT, WORD_LENGHT, tries, validations);
} }
/** /**
* @brief Check if the given letter is a lowercase letter only * @brief Check if the given letter is a lowercase letter only (ASCII code)
* *
* @param key * @param key
* @return true * @return true
* @return false * @return false
*/ */
bool validate_letter(int key) { return (key >= 97 && key <= 122); } bool validate_letter(int key) { return (islower(key) || isupper(key)); }
/** /**
* @brief Validate the last guess * @brief Validate the last guess
*
*/ */
int *validate_last_guess(char answer[WORD_LENGHT], char try[WORD_LENGHT]) { int *validate_guess(char answer[WORD_LENGHT], char try[WORD_LENGHT]) {
int a[WORD_LENGHT] = {0, 2, 0, 1, 0}; int a[WORD_LENGHT] = {0, 2, 0, 1, 0};
int *validation = calloc(WORD_LENGHT, sizeof(int)); int *validation = calloc(WORD_LENGHT, sizeof(int));
...@@ -68,7 +111,7 @@ void handle_controls(int key) { ...@@ -68,7 +111,7 @@ void handle_controls(int key) {
if (strlen(tries[current_try_id]) < WORD_LENGHT) if (strlen(tries[current_try_id]) < WORD_LENGHT)
break; break;
int *tmp = validate_last_guess(chosen_word, tries[current_try_id]); int *tmp = validate_guess(chosen_word, tries[current_try_id]);
for (int i = 0; i < WORD_LENGHT; i++) for (int i = 0; i < WORD_LENGHT; i++)
validations[current_try_id][i] = tmp[i]; validations[current_try_id][i] = tmp[i];
free(tmp); free(tmp);
...@@ -85,58 +128,33 @@ void handle_controls(int key) { ...@@ -85,58 +128,33 @@ void handle_controls(int key) {
void set_gamemode(int menu_gamemode) { gamemode = menu_gamemode; } void set_gamemode(int menu_gamemode) { gamemode = menu_gamemode; }
void launch_game() { void launch_game() {
chosen_word = "SALUT"; initialize_game();
tries = calloc(TRIES_COUNT, sizeof(char *));
validations = malloc(sizeof(int *) * TRIES_COUNT);
for (int i = 0; i < TRIES_COUNT; i++) {
validations[i] = calloc(WORD_LENGHT + 1, sizeof(int));
tries[i] = calloc(WORD_LENGHT + 1, sizeof(char)); // +1 because of \0
}
while (!game_finished()) { while (!game_finished()) {
// Guard clause // Guard clause
if (current_try_id > TRIES_COUNT - 1) { if (lose_condition()) {
terminate_game(); terminate_game();
continue; continue;
} }
// Render // Render
switch (gamemode) { render_game();
case GAMEMODE_SOLO:
printw_center("Solo", 5);
break;
case GAMEMODE_1_V_1:
printw_center("Player 1", 5);
break;
case GAMEMODE_COMPUTER:
printw_center("Computer assisted", 5);
break;
}
show_gameboard(TRIES_COUNT, WORD_LENGHT, tries, validations);
// Next letter // Key handling
// || Need to check letter by letter every time to be able to continue using
// || shortcut keys like Ctrl+h during a guess.
int key = getch(); int key = getch();
if (validate_letter(key)) { if (validate_letter(key)) {
if (current_try_letter_id < WORD_LENGHT) if (current_try_letter_id < WORD_LENGHT) {
add_letter_to_try((char)key - 32); // Offset letter to only put upercase tries[current_try_id][current_try_letter_id] = toupper(key);
current_try_letter_id += 1;
}
} }
handle_controls(key); handle_controls(key);
refresh(); refresh();
} }
for (int i = 0; i < TRIES_COUNT; i++) {
free(tries[i]);
free(validations[i]);
}
free(tries);
free(validations);
} }
bool game_finished() { return _game_finished; } bool game_finished() { return _game_finished; }
\ No newline at end of file
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
#define WORDLE_H_ #define WORDLE_H_
#include "../ui/ui.h" #include "../ui/ui.h"
#include <ctype.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h>
// Gamemodes // Gamemodes
#define GAMEMODE_SOLO 0 #define GAMEMODE_SOLO 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment