Home > Software design >  WORDLE - Read .txt file but if a word repeats dont use it again
WORDLE - Read .txt file but if a word repeats dont use it again

Time:07-12

im doing a simple Wordle clone in C. I already finished it but im adding some little things that i think make it nicer and that let me practice more since im still new to programming.

Anyways, the problem is, i use a .txt file to get the "random" words to guess, and the program allows you to play up to 8consecutive games to add up all your points and get a higher highscore. Therefore, there's a SLIGHT chance you can get the same word twice(or more) in the same run. How can i make it so once a word is read in the .txt it gets crossed out and isnt take in consideration anymore.. i thought of deleting it from the .txt but there's two problems with that. First, i dont want it to be removed for ever and have to keep writing it again into the file, and secondly my teacher said its not needed to do something so complicated for a necessity this simple... so yeah

TL;DR: How can i read random words in a .txt file and cross out the ones used, WHITOUT DELETING THEM or altering the file so it keeps the same for next games?

Here's the function i use to get the random words, what should i add?

void randomPalabra(char palabra[]) {

int lineaActual = 0, lineaElegida = 0;
bool seguirLeyendo = true;

srand(time(NULL));
lineaElegida = rand() % 30   1;


FILE* archivoTxt;

archivoTxt = fopen("palabras.txt", "r");
if (archivoTxt == NULL) {

    printf("El archivo no existe.");
}
else {

    do {

        fgets(palabra, 6, archivoTxt);

        if (_stricmp(palabra, "\n") != 0) {

            lineaActual  ;
        }



    } while (lineaActual != lineaElegida); //Porque si hago esto funciona y se para pero si pongo un if(fgets..etc == NULL) no?? no son tecnicamente lo mismo escrito diferente?
    //por cierto, no entiendo porque de la nada fgets toma la string de caracteres y el \n como dos lineas diferentes, me vino de 10, pero antes no m funcionaba asi, y me tomaba siempre la palabra con el simbolo al final, por ejemplo "gatos\n". 

}

fclose(archivoTxt);

}

CodePudding user response:

Have a table in memory of the words used so far, since you specify a max of 8 its very simple.

this function returns true (1) if it remembered the word, false (0) if the word was already used

  int rememberWord(char* word) {
    static int used_word_count = 0;
    static char* used_words[8][6];

    for (int i = 0; i < used_word_count; i  ) {
        if (strcmp(word, used_words[i]) == 0)
            return 0; // already used
    }

    strcpy(used_words[used_word_count],word);
    used_word_count  ;
    return 1; // new word
}

hardcoded string length to be 6 (since wordle uses 5 character words)

so call this when you choose a word, if it returns zero it means you already used it

CodePudding user response:

What you can do is implement a Map Abstract Data Type (ADT) (Key, Value) with a hashing algorithm. When you randomly select a word from palabras.txt you submit the word to your hashing algorithm giving you a key, place the word alongside your key in the ADT. When it's time to check if another chosen word has already been used, you simply run it through the hashing algorithm and quickly see if the newly selected word has already been placed into your ADT.

Here are some general prototypes of a Map ADT:

  • put(key, value)
  • get(key)
  • contains_key(map, key)
  • contains_value(map, value)
  • remove(key)
  • size()
  • Related