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

Working proto

parent 2697a665
No related branches found
No related tags found
No related merge requests found
bin/ bin/
.vscode/ .vscode/
\ No newline at end of file doc/
from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr
from rich import print
import unicodedata
session = WolframLanguageSession()
frequencies = {}
def remove_accents(data: str):
return "".join(
(
c
for c in unicodedata.normalize(
"NFD", data
) # Normalization Form D (NFD) - Canonical Decomposition
if unicodedata.category(c) != "Mn" # Nonspacing mark (accents)
)
)
with open("liste_francais.txt", "r", encoding="utf-8") as f:
words = f.readlines()
words = [w.replace("\n", "") for w in words]
words = [w for w in words if len(w) == 5]
words = [remove_accents(w).lower() for w in words]
for i, w in enumerate(words):
frequencies[w] = session.evaluate(wl.WordFrequencyData(w))
print(f"{i +1}/{len(words)}", end="\r")
session.terminate()
print(frequencies)
This diff is collapsed.
This diff is collapsed.
...@@ -20,4 +20,17 @@ int main(void) { ...@@ -20,4 +20,17 @@ int main(void) {
cleanup_term(); cleanup_term();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
\ No newline at end of file
// #include "word-bank/bank.h"
// #include <ncurses.h>
// #include <stdio.h>
// #include <stdlib.h>
// int main(void) {
// char *w = get_random_word();
// printf("%s\n", w);
// return EXIT_SUCCESS;
// }
\ No newline at end of file
...@@ -117,6 +117,13 @@ void draw_title(int id) { ...@@ -117,6 +117,13 @@ void draw_title(int id) {
} }
} }
void show_answer(char *answer) { printw_center(answer, GAMEBOARD_Y_OFFSET + 10); }
void hide_answer() {
move(GAMEBOARD_Y_OFFSET + 10, 0);
clrtoeol();
}
bool init_term() { bool init_term() {
ui_window = initscr(); ui_window = initscr();
cbreak(); cbreak();
...@@ -227,7 +234,7 @@ void toggle_help() { ...@@ -227,7 +234,7 @@ void toggle_help() {
wrefresh(help); wrefresh(help);
} }
void show_gameboard(int tries_count, int word_length, char **tries, int **validations) { void show_gameboard(int tries_count, int word_length, char **tries, int **validations, int score) {
for (int i = 0; i < tries_count; i++) { for (int i = 0; i < tries_count; i++) {
// Draw current try letters // Draw current try letters
for (size_t j = 0; j < strlen(tries[i]); j++) { for (size_t j = 0; j < strlen(tries[i]); j++) {
...@@ -245,5 +252,9 @@ void show_gameboard(int tries_count, int word_length, char **tries, int **valida ...@@ -245,5 +252,9 @@ void show_gameboard(int tries_count, int word_length, char **tries, int **valida
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), " _");
} }
move(GAMEBOARD_Y_OFFSET - 2, TERM_MID_COLS - word_length - 3);
printw("Score: %d", score);
refresh(); refresh();
} }
\ No newline at end of file
...@@ -56,6 +56,19 @@ void printw_center(char *foo, int line); ...@@ -56,6 +56,19 @@ void printw_center(char *foo, int line);
*/ */
void draw_title(int id); void draw_title(int id);
/**
* @brief Show the answer
*
* @param answer
*/
void show_answer(char *answer);
/**
* @brief Hide the answer
*
*/
void hide_answer();
/** /**
* @brief Initialize the terminal * @brief Initialize the terminal
* *
...@@ -90,7 +103,8 @@ void toggle_help(); ...@@ -90,7 +103,8 @@ void toggle_help();
* @param word_length * @param word_length
* @param tries * @param tries
* @param validations * @param validations
* @param score
*/ */
void show_gameboard(int tries_count, int word_length, char **tries, int **validations); void show_gameboard(int tries_count, int word_length, char **tries, int **validations, int score);
#endif #endif
\ No newline at end of file
#include "bank.h"
//==========================
// PRIVATE
//==========================
FILE *bank_file;
char **bank = NULL;
int bank_count = 0;
/**
* @brief Initialize the bank file
*
*/
void init_bank() {
srand(clock());
bank_file = fopen(BANK_PATH, "r");
if (bank_file == NULL) {
perror("file");
exit(EXIT_FAILURE);
}
}
/**
* @brief Word picking condition
*
* @param word
* @return true
* @return false
*/
bool pick_condition(char *word) {
if (strlen(word) != WORD_LENGHT)
return false;
// Check for non-alphabet letters
for (size_t i = 0; i < strlen(word); i++) {
if (!((word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' && word[i] <= 'Z')))
return false;
}
return true;
}
/**
* @brief Cleanup the bank entries
*
* @param tmp_bank
* @param valid_word_count
*/
void cleanup_bank(char **tmp_bank, int valid_word_count) {
bank = malloc(valid_word_count * sizeof(char **));
int bank_idx = 0;
for (int i = 0; i < bank_count; i++) {
if (!pick_condition(tmp_bank[i]))
continue;
bank[bank_idx] = malloc((strlen(tmp_bank[i]) + 1) * sizeof(char));
strcpy(bank[bank_idx], tmp_bank[i]);
for (size_t j = 0; j < strlen(bank[bank_idx]); j++)
bank[bank_idx][j] = toupper(bank[bank_idx][j]);
bank_idx++;
}
for (int i = 0; i < bank_count; i++)
free(tmp_bank[i]);
free(tmp_bank);
bank_count = valid_word_count;
}
/**
* @brief Load the bank content
*
*/
void load_bank() {
char *lineBuf = NULL;
size_t n = 0;
size_t lineLength = 0;
int valid_word_count = 0;
char **tmp = malloc(count_words() * sizeof(char **));
while ((lineLength = getline(&lineBuf, &n, bank_file)) != (size_t)-1) {
lineBuf[strcspn(lineBuf, "\n")] = '\0'; // Set the last char of the string to EOL
tmp[bank_count] = malloc((lineLength + 1) * sizeof(char));
if (pick_condition(lineBuf))
valid_word_count++;
strcpy(tmp[bank_count], lineBuf);
bank_count++;
}
free(lineBuf);
fclose(bank_file);
cleanup_bank(tmp, valid_word_count);
}
//==========================
// PUBLIC
//==========================
int count_words() {
char ch;
int count = 0;
rewind(bank_file);
do {
ch = fgetc(bank_file);
if (ch == '\n')
count++;
} while (ch != EOF);
rewind(bank_file);
return count;
}
char *get_random_word() {
if (bank == NULL) {
init_bank();
load_bank();
}
return bank[rand() % bank_count];
}
char **get_bank() { return bank; }
int get_bank_size() { return bank_count; }
\ No newline at end of file
#ifndef BANK_H_
#define BANK_H_
#include "../wordle/wordle.h"
#include <ctype.h>
#include <ncurses.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#define BANK_PATH "./liste_francais.txt"
/**
* @brief Count the amount of word
*
* @return int
*/
int count_words();
/**
* @brief Get a random word from the list
*
* @return char*
*/
char *get_random_word();
/**
* @brief Get the word bank
*
* @return char**
*/
char **get_bank();
/**
* @brief Get the bank size
*
* @return int
*/
int get_bank_size();
#endif
\ No newline at end of file
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
//========================== //==========================
int gamemode = -1; int gamemode = -1;
bool _game_finished = false; bool _game_finished = false;
int score = 0;
char **tries; char **tries;
char *chosen_word; char *chosen_word;
int **validations; int **validations;
...@@ -21,7 +22,7 @@ void initialize_game() { ...@@ -21,7 +22,7 @@ void initialize_game() {
current_try_letter_id = 0; current_try_letter_id = 0;
// Tries and answer setup // Tries and answer setup
chosen_word = "SALUT"; // Must be randomly picked in a dictionary chosen_word = get_random_word();
tries = calloc(TRIES_COUNT, sizeof(char *)); tries = calloc(TRIES_COUNT, sizeof(char *));
validations = malloc(sizeof(int *) * TRIES_COUNT); validations = malloc(sizeof(int *) * TRIES_COUNT);
...@@ -56,6 +57,9 @@ void destroy_game() { ...@@ -56,6 +57,9 @@ void destroy_game() {
* *
*/ */
void restart_game() { void restart_game() {
score += (TRIES_COUNT - current_try_id) * 10;
chosen_word = get_random_word();
current_try_id = 0; current_try_id = 0;
current_try_letter_id = 0; current_try_letter_id = 0;
...@@ -108,7 +112,7 @@ void render_game() { ...@@ -108,7 +112,7 @@ void render_game() {
break; break;
} }
show_gameboard(TRIES_COUNT, WORD_LENGHT, tries, validations); show_gameboard(TRIES_COUNT, WORD_LENGHT, tries, validations, score);
} }
/** /**
...@@ -165,9 +169,26 @@ void handle_controls(int key) { ...@@ -165,9 +169,26 @@ void handle_controls(int key) {
break; break;
case KEYBIND_GUESS: case KEYBIND_GUESS:
// Length check
if (strlen(tries[current_try_id]) < WORD_LENGHT) if (strlen(tries[current_try_id]) < WORD_LENGHT)
break; 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 = 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];
...@@ -193,8 +214,10 @@ void launch_game() { ...@@ -193,8 +214,10 @@ void launch_game() {
// Guard clause // Guard clause
if (win_condition() || lose_condition()) { if (win_condition() || lose_condition()) {
show_answer(chosen_word);
restart_game(); restart_game();
handle_controls(getch()); handle_controls(getch());
hide_answer();
continue; continue;
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define WORDLE_H_ #define WORDLE_H_
#include "../ui/ui.h" #include "../ui/ui.h"
#include "../word-bank/bank.h"
#include <ctype.h> #include <ctype.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment