forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
2 Commits
cc74987a8b
...
702611409e
| Author | SHA1 | Date | |
|---|---|---|---|
| 702611409e | |||
|
|
32de0b90e7 |
80
getfuncs.c
80
getfuncs.c
@@ -1,80 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
/*
|
||||
récupère le nombre de lignes dans la banque de mots
|
||||
*/
|
||||
int getFileLength(){
|
||||
/*variables*/
|
||||
FILE* stream;
|
||||
int file_length = 0;
|
||||
char* line = NULL;
|
||||
size_t length = 0;
|
||||
ssize_t read;
|
||||
|
||||
/*prog*/
|
||||
stream = fopen("wordbank.txt", "r");
|
||||
if (stream == NULL){
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
while((read = getline(&line, &length, stream)) != -1){
|
||||
file_length++;
|
||||
}
|
||||
|
||||
fclose(stream);
|
||||
return file_length-1;
|
||||
}
|
||||
|
||||
/*
|
||||
récupère un mot aléatoire dans la banque de mots
|
||||
*/
|
||||
int fetchWord(char* fullword, int file_length){
|
||||
/*variables*/
|
||||
FILE* stream;
|
||||
int random = (rand() * time(NULL)) % (file_length);
|
||||
char read[8];
|
||||
unsigned int counter = 0;
|
||||
int char_size = (int) sizeof(char);
|
||||
int line=0;
|
||||
int offset=0;
|
||||
|
||||
/*prog*/
|
||||
printf("%d\n", random);
|
||||
stream = fopen("wordbank.txt", "r");
|
||||
if (stream == NULL){
|
||||
return -1;
|
||||
}
|
||||
|
||||
while((!feof(stream))&&(offset!=random)){
|
||||
fread(read, sizeof(char), 1, stream);
|
||||
if(*read=='\n'){
|
||||
offset++;
|
||||
}
|
||||
}
|
||||
|
||||
*read=' ';
|
||||
|
||||
while(*read!='\n'){
|
||||
fread(read, sizeof(char), 1, stream);
|
||||
fullword[counter] = *read;
|
||||
counter++;
|
||||
}
|
||||
|
||||
fclose(stream);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main(void){
|
||||
int flength;
|
||||
char fullWord[128];
|
||||
|
||||
flength = getFileLength();
|
||||
if(fetchWord(fullWord, flength)!=-1){
|
||||
printf("%s", fullWord);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LIFES 8 // Nombre de vies au debut du jeu
|
||||
|
||||
// Fonction pour lire une lettre proposee par le joueur
|
||||
char choose_letter() {
|
||||
char letter;
|
||||
printf("Entrez une lettre : ");
|
||||
scanf(" %c", &letter); // Le " " avant %c evite de lire un retour chariot
|
||||
printf("Vous avez choisi la lettre '%c'\n", letter);
|
||||
return letter;
|
||||
}
|
||||
|
||||
// Fonction pour calculer la taille d'une chaine (similaire a strlen)
|
||||
int word_size(const char *fullWord) {
|
||||
int size = 0;
|
||||
while (fullWord[size] != '\0') {
|
||||
size++;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
// Verifie combien de fois la lettre apparait dans fullWord
|
||||
int letter_occurrence(char letter, const char *fullWord) {
|
||||
int wordsize = wor_size(fullWord);
|
||||
int letterOccurrence = 0;
|
||||
|
||||
for (int i = 0; i < wordsize; i++) {
|
||||
if (fullWord[i] == letter) { // == pour comparer
|
||||
letterOccurrence++;
|
||||
}
|
||||
}
|
||||
|
||||
if (letterOccurrence == 0) {
|
||||
printf("La lettre '%c' n'apparait pas dans le mot.\n", letter);
|
||||
}
|
||||
|
||||
return letterOccurrence;
|
||||
}
|
||||
|
||||
// Remplace les '_' par la lettre trouvee dans partialWord
|
||||
void position_letter(char letter, const char *fullWord, char *partialWord) {
|
||||
int wordsize = word_size(fullWord);
|
||||
|
||||
for (int i = 0; i < wordsize; i++) {
|
||||
if (fullWord[i] == letter) {
|
||||
partialWord[i] = letter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Verifie si le joueur a gagne (si partialWord == fullWord)
|
||||
int has_won(const char *fullWord, const char *partialWord) {
|
||||
return strcmp(fullWord, partialWord) == 0; // 1 si egal, 0 sinon
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
char fullWord[] = "ordinateur"; // Le mot a deviner
|
||||
int size = word_size(fullWord);
|
||||
char partialWord[size + 1]; // +1 pour le '\0'
|
||||
|
||||
// Initialiser partialWord avec des '_'
|
||||
for (int i = 0; i < size; i++) {
|
||||
partialWord[i] = '_';
|
||||
}
|
||||
partialWord[size] = '\0';
|
||||
|
||||
int lifes = LIFES;
|
||||
int won = 0;
|
||||
|
||||
printf("Bienvenue au jeu du pendu !\n");
|
||||
printf("Le mot a deviner contient %d lettres.\n", size);
|
||||
|
||||
while (lifes > 0 && !won) {
|
||||
printf("\nMot actuel : %s\n", partialWord);
|
||||
printf("Vies restantes : %d\n", lifes);
|
||||
|
||||
char letter = choose_letter();
|
||||
|
||||
int occurrence = letter_occurrence(letter, fullWord);
|
||||
if (occurrence > 0) {
|
||||
position_letter(letter, fullWord, partialWord);
|
||||
} else {
|
||||
lifes--;
|
||||
}
|
||||
|
||||
if (has_won(fullWord, partialWord)) {
|
||||
won = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (won) {
|
||||
printf("\n Felicitations ! Vous avez trouve le mot : %s\n", fullWord);
|
||||
} else {
|
||||
printf("\n Vous avez perdu ! Le mot etait : %s\n", fullWord);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
anticonstitutionnellement
|
||||
éclésiastique
|
||||
abandonner
|
||||
seringue
|
||||
vocifération
|
||||
éponyme
|
||||
|
||||
Reference in New Issue
Block a user