Compare commits
8 Commits
master
...
919959dc39
| Author | SHA1 | Date | |
|---|---|---|---|
| 919959dc39 | |||
| 3e4bcf3b52 | |||
|
|
ff97710546 | ||
| 804709b1e0 | |||
|
|
0908750892 | ||
|
|
0a7067acc3 | ||
| 8830171c87 | |||
| d460f09e6f |
80
getfuncs.c
Normal file
80
getfuncs.c
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
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;
|
||||
}*/
|
||||
104
src_karamosr/karamosr.c
Normal file
104
src_karamosr/karamosr.c
Normal file
@@ -0,0 +1,104 @@
|
||||
#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 = wordSize(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 = wordSize(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 = wordSize(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 = chooseLetter();
|
||||
|
||||
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;
|
||||
}
|
||||
*/
|
||||
7
wordbank.txt
Normal file
7
wordbank.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
anticonstitutionnellement
|
||||
éclésiastique
|
||||
abandonner
|
||||
seringue
|
||||
vocifération
|
||||
éponyme
|
||||
|
||||
Reference in New Issue
Block a user