forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
12 Commits
3e4bcf3b52
...
vaisse
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32de0b90e7 | ||
|
|
87487bde84 | ||
| c650075e86 | |||
| 728b639857 | |||
| 6ea2416f9c | |||
| da870af20f | |||
| dbcbaa124e | |||
|
|
12d9bffc99 | ||
| 919959dc39 | |||
| 804709b1e0 | |||
| 8830171c87 | |||
| d460f09e6f |
80
getfuncs.c
80
getfuncs.c
@@ -1,80 +0,0 @@
|
|||||||
#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;
|
|
||||||
}*/
|
|
||||||
100
src/facile.txt
Normal file
100
src/facile.txt
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
ARBRE
|
||||||
|
LIVRE
|
||||||
|
POMME
|
||||||
|
CHAIR
|
||||||
|
TABLE
|
||||||
|
CHIEN
|
||||||
|
CHAT
|
||||||
|
VACHE
|
||||||
|
CHEVAL
|
||||||
|
PORTE
|
||||||
|
MUR
|
||||||
|
TOIT
|
||||||
|
LUNE
|
||||||
|
SOLEIL
|
||||||
|
MARE
|
||||||
|
NEIGE
|
||||||
|
GLACE
|
||||||
|
FLEUR
|
||||||
|
ROSE
|
||||||
|
BLEUET
|
||||||
|
ORANGE
|
||||||
|
BANANE
|
||||||
|
TOMATE
|
||||||
|
CITRON
|
||||||
|
PAPAYE
|
||||||
|
RAISIN
|
||||||
|
MANGUE
|
||||||
|
CERISE
|
||||||
|
OLIVE
|
||||||
|
PIMENT
|
||||||
|
SUCRE
|
||||||
|
SEL
|
||||||
|
POIVRE
|
||||||
|
MOUTON
|
||||||
|
COQ
|
||||||
|
POULE
|
||||||
|
LAPIN
|
||||||
|
AIGLE
|
||||||
|
TIGRE
|
||||||
|
LION
|
||||||
|
ZEBRE
|
||||||
|
SINGE
|
||||||
|
PANDA
|
||||||
|
KOALA
|
||||||
|
RENARD
|
||||||
|
OURS
|
||||||
|
CRABE
|
||||||
|
POISSON
|
||||||
|
REKIN
|
||||||
|
DAUPHIN
|
||||||
|
BALEINE
|
||||||
|
PLUIE
|
||||||
|
VENT
|
||||||
|
FEU
|
||||||
|
TERRE
|
||||||
|
NUAGE
|
||||||
|
ORAGE
|
||||||
|
PENTE
|
||||||
|
MER
|
||||||
|
RIVAGE
|
||||||
|
RIVIERE
|
||||||
|
ETANG
|
||||||
|
PLAINE
|
||||||
|
COLLINE
|
||||||
|
VALLEE
|
||||||
|
ROCHER
|
||||||
|
SABLE
|
||||||
|
PRAIRIE
|
||||||
|
BOIS
|
||||||
|
FORET
|
||||||
|
GIVRE
|
||||||
|
CAVERNE
|
||||||
|
CERCLE
|
||||||
|
ANGLE
|
||||||
|
CARRE
|
||||||
|
CUBE
|
||||||
|
SPHERE
|
||||||
|
POINT
|
||||||
|
TRAIT
|
||||||
|
CHEMIN
|
||||||
|
ROUTE
|
||||||
|
RUE
|
||||||
|
VILLE
|
||||||
|
VILLAGE
|
||||||
|
PAYS
|
||||||
|
MONDE
|
||||||
|
ETOILE
|
||||||
|
GALAXIE
|
||||||
|
COSMOS
|
||||||
|
TEMPLE
|
||||||
|
CHAPELLE
|
||||||
|
JARDIN
|
||||||
|
FLECHE
|
||||||
|
ECUREUIL
|
||||||
|
COUSIN
|
||||||
|
AMIS
|
||||||
|
ENNEMI
|
||||||
|
VILAIN
|
||||||
|
COQUIN
|
||||||
|
VOYOU
|
||||||
49
src/functions.c
Normal file
49
src/functions.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include "functions.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
char choose_letter() {
|
||||||
|
char letter;
|
||||||
|
printf("Entrez une lettre : ");
|
||||||
|
scanf(" %c", &letter);
|
||||||
|
printf("Vous avez choisi la lettre '%c'\n", letter);
|
||||||
|
return letter;
|
||||||
|
}
|
||||||
|
|
||||||
|
int word_size(const char *fullWord) {
|
||||||
|
int size = 0;
|
||||||
|
while (fullWord[size] != '\0') {
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
int letter_occurrence(char letter, const char *fullWord) {
|
||||||
|
int wordsize = word_size(fullWord);
|
||||||
|
int letterOccurrence = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < wordsize; i++) {
|
||||||
|
if (fullWord[i] == letter) {
|
||||||
|
letterOccurrence++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (letterOccurrence == 0) {
|
||||||
|
printf("La lettre '%c' n'apparait pas dans le mot.\n", letter);
|
||||||
|
}
|
||||||
|
|
||||||
|
return letterOccurrence;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int has_won(const char *fullWord, const char *partialWord) {
|
||||||
|
return strcmp(fullWord, partialWord) == 0;
|
||||||
|
}
|
||||||
12
src/functions.h
Normal file
12
src/functions.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef FUNCTIONS_H
|
||||||
|
#define FUNCTIONS_H
|
||||||
|
|
||||||
|
#define LIFES 8
|
||||||
|
|
||||||
|
char choose_letter();
|
||||||
|
int word_size(const char *fullWord);
|
||||||
|
int letter_occurrence(char letter, const char *fullWord);
|
||||||
|
void position_letter(char letter, const char *fullWord, char *partialWord);
|
||||||
|
int has_won(const char *fullWord, const char *partialWord);
|
||||||
|
|
||||||
|
#endif
|
||||||
44
src/getfuncs.c
Normal file
44
src/getfuncs.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include "getfuncs.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int getFileLength(const char *filename) {
|
||||||
|
FILE* stream = fopen(filename, "r");
|
||||||
|
if (!stream) return -1;
|
||||||
|
|
||||||
|
int file_length = 0;
|
||||||
|
char buffer[128];
|
||||||
|
while (fgets(buffer, sizeof(buffer), stream)) {
|
||||||
|
file_length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(stream);
|
||||||
|
return file_length;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fetchWord(const char *filename, char *fullWord, int file_length) {
|
||||||
|
if (file_length <= 0) return -1;
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
int random_line = rand() % file_length;
|
||||||
|
|
||||||
|
FILE* stream = fopen(filename, "r");
|
||||||
|
if (!stream) return -1;
|
||||||
|
|
||||||
|
int current = 0;
|
||||||
|
while (fgets(fullWord, 128, stream)) {
|
||||||
|
if (current == random_line) {
|
||||||
|
// Retirer le '\n' si présent
|
||||||
|
char *newline = strchr(fullWord, '\n');
|
||||||
|
if (newline) *newline = '\0';
|
||||||
|
fclose(stream);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
current++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(stream);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
7
src/getfuncs.h
Normal file
7
src/getfuncs.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef GETFUNCS_H
|
||||||
|
#define GETFUNCS_H
|
||||||
|
|
||||||
|
int getFileLength(const char *filename);
|
||||||
|
int fetchWord(const char *filename, char *fullWord, int file_length);
|
||||||
|
|
||||||
|
#endif
|
||||||
80
src/interface.c
Normal file
80
src/interface.c
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <graph.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define WINDOW_HEIGTH 500
|
||||||
|
#define WINDOW_WIDTH 500
|
||||||
|
|
||||||
|
/*
|
||||||
|
Contient l'ensemble des informations essentiels d'un bouton
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int length;
|
||||||
|
int heigth;
|
||||||
|
char* text;
|
||||||
|
unsigned short int r;
|
||||||
|
unsigned short int g;
|
||||||
|
unsigned short int b;
|
||||||
|
} Button;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Ouvre la fenêtre. Permet au jeu de démarer.
|
||||||
|
*/
|
||||||
|
void STARTGAMe(){
|
||||||
|
InitialiserGraphique();
|
||||||
|
CreerFenetre(10, 10, WINDOW_HEIGTH, WINDOW_WIDTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Vérifie si le curseur est placée dans un bouton. Renvoi 0 si oui. Renvoi -1 sinon.
|
||||||
|
*/
|
||||||
|
int inButton(Button* b){
|
||||||
|
int minimum_x = b->x;
|
||||||
|
int minimum_y = b->y;
|
||||||
|
int maximum_x = (b->x) + (b->length);
|
||||||
|
int maximum_y = (b->y) + (b->heigth);
|
||||||
|
if((_X>=minimum_x)&&(_X<=maximum_x)&&(_Y>=minimum_y)&&(_Y<=maximum_y)){
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Dessine un bouton sur l'écran
|
||||||
|
*/
|
||||||
|
void drawButton(Button* b){
|
||||||
|
ChoisirCouleurDessin(CouleurParComposante(b->r,b->g,b->b));
|
||||||
|
RemplirRectangle(b->x, b->y, b->length, b->heigth);
|
||||||
|
ChoisirCouleurDessin(CouleurParNom("white"));
|
||||||
|
EcrireTexte((b->x)+20, (b->y)+20, b->text, (b->heigth)/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Affiche le menu
|
||||||
|
*/
|
||||||
|
void setMenu(){
|
||||||
|
/*éléments*/
|
||||||
|
Button easy = {WINDOW_WIDTH*(1-0.75), WINDOW_HEIGTH*(1-0.75), WINDOW_WIDTH/8, WINDOW_HEIGTH/10, "facile", 148, 225, 224};
|
||||||
|
Button medium = {WINDOW_WIDTH*(1-0.50), WINDOW_HEIGTH*(1-0.75), WINDOW_WIDTH/8, WINDOW_HEIGTH/10, "moyen", 148, 225, 224};
|
||||||
|
Button hard = {WINDOW_WIDTH*(1-0.25), WINDOW_HEIGTH*(1-0.75), WINDOW_WIDTH/8, WINDOW_HEIGTH/10, "difficile", 148, 225, 224};
|
||||||
|
|
||||||
|
/*dessin*/
|
||||||
|
ChoisirCouleurDessin(CouleurParNom("black"));
|
||||||
|
EcrireTexte(WINDOW_WIDTH/2, WINDOW_HEIGTH/2, "PENDU", WINDOW_WIDTH/10);
|
||||||
|
drawButton(&easy);
|
||||||
|
drawButton(&medium);
|
||||||
|
drawButton(&hard);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
STARTGAMe();
|
||||||
|
setMenu();
|
||||||
|
Touche();
|
||||||
|
FermerGraphique();
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
61
src/main.c
Normal file
61
src/main.c
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "functions.h"
|
||||||
|
#include "getfuncs.h"
|
||||||
|
|
||||||
|
void play_game(const char *filename) {
|
||||||
|
char fullWord[128];
|
||||||
|
int flength = getFileLength(filename);
|
||||||
|
if (fetchWord(filename, fullWord, flength) != 0) {
|
||||||
|
printf("Erreur de lecture du fichier.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int size = word_size(fullWord);
|
||||||
|
char partialWord[size + 1];
|
||||||
|
for (int i = 0; i < size; i++) partialWord[i] = '_';
|
||||||
|
partialWord[size] = '\0';
|
||||||
|
|
||||||
|
int lifes = LIFES;
|
||||||
|
int won = 0;
|
||||||
|
|
||||||
|
printf("Le mot a deviner contient %d lettres.\n", size);
|
||||||
|
|
||||||
|
while (lifes > 0 && !won) {
|
||||||
|
printf("\nMot actuel : %s\nVies restantes : %d\n", partialWord, 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("\nFelicitations ! Vous avez trouve le mot : %s\n", fullWord);
|
||||||
|
else printf("\nVous avez perdu ! Le mot etait : %s\n", fullWord);
|
||||||
|
}
|
||||||
|
|
||||||
|
void play_hard_mode() {
|
||||||
|
printf("Mode difficile : vous devez deviner deux mots !\n");
|
||||||
|
play_game("facile.txt");
|
||||||
|
play_game("moyen.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int choice;
|
||||||
|
printf("Choisissez le mode :\n1 - Facile\n2 - Moyen\n3 - Difficile\n> ");
|
||||||
|
scanf("%d", &choice);
|
||||||
|
|
||||||
|
switch(choice) {
|
||||||
|
case 1: play_game("facile.txt"); break;
|
||||||
|
case 2: play_game("moyen.txt"); break;
|
||||||
|
case 3: play_hard_mode(); break;
|
||||||
|
default: printf("Mode invalide.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
100
src/moyen.txt
Normal file
100
src/moyen.txt
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
ORDINATEUR
|
||||||
|
PROGRAMME
|
||||||
|
TELEPHONE
|
||||||
|
CARNETAGE
|
||||||
|
UNIVERSITE
|
||||||
|
HABITATION
|
||||||
|
BIBLIOTHEQUE
|
||||||
|
MICROPHONE
|
||||||
|
NAVIGATEUR
|
||||||
|
CONDUCTEUR
|
||||||
|
VOYAGEURS
|
||||||
|
CONSTRUCTEUR
|
||||||
|
CHOCOLATERIE
|
||||||
|
APPARTEMENT
|
||||||
|
RESTAURANT
|
||||||
|
IMPRIMERIE
|
||||||
|
MOUSTACHES
|
||||||
|
CAMPAGNARD
|
||||||
|
INVENTAIRE
|
||||||
|
EXPLORATEUR
|
||||||
|
COMPAGNON
|
||||||
|
MATHEMATICS
|
||||||
|
ASTRONAUTE
|
||||||
|
ARCHITECTE
|
||||||
|
MECANICIEN
|
||||||
|
CHIRURGIEN
|
||||||
|
ELEPHANTES
|
||||||
|
HOSPITALES
|
||||||
|
MONTAGNES
|
||||||
|
DESERTIQUE
|
||||||
|
HUMANISME
|
||||||
|
TROUBADOUR
|
||||||
|
VOYAGEUSES
|
||||||
|
CROCODILES
|
||||||
|
DINOSAURES
|
||||||
|
HERISSONNE
|
||||||
|
CULTIVATEUR
|
||||||
|
PLANTATION
|
||||||
|
CROISSANCE
|
||||||
|
DECORATION
|
||||||
|
TRADITION
|
||||||
|
ORGANISATEUR
|
||||||
|
DEVELOPPEUR
|
||||||
|
INFORMATIQUE
|
||||||
|
AUTOROUTES
|
||||||
|
CARTOGRAPHIE
|
||||||
|
GEOGRAPHIE
|
||||||
|
HISTORIQUE
|
||||||
|
LUMINOSITE
|
||||||
|
TRANSPORTEUR
|
||||||
|
NAVIGATIONS
|
||||||
|
IMAGINATION
|
||||||
|
CITADINITE
|
||||||
|
POPULATION
|
||||||
|
MARCHANDISE
|
||||||
|
ECOLOGISTE
|
||||||
|
BOTANISTES
|
||||||
|
ZOOLOGIQUE
|
||||||
|
HARMONIEUX
|
||||||
|
MEDITATION
|
||||||
|
RESISTANCE
|
||||||
|
INSPIRATION
|
||||||
|
INNOVATION
|
||||||
|
CONDITION
|
||||||
|
SUPERVISEUR
|
||||||
|
SPECTATEURS
|
||||||
|
ILLUMINATION
|
||||||
|
PLATEFORME
|
||||||
|
PERSPECTIVE
|
||||||
|
CALCULATEUR
|
||||||
|
OBSERVATEUR
|
||||||
|
ALGORITHME
|
||||||
|
EDUCATEURS
|
||||||
|
SOUVERAINE
|
||||||
|
PARLEMENTS
|
||||||
|
MINISTREES
|
||||||
|
ACROBATIES
|
||||||
|
MUSICIENNE
|
||||||
|
DIRECTION
|
||||||
|
HORIZONTAL
|
||||||
|
VERTICALES
|
||||||
|
REVOLUTION
|
||||||
|
SCIENTIFIQUE
|
||||||
|
EXPERIENCE
|
||||||
|
CONNAISSANCE
|
||||||
|
LABORATOIRE
|
||||||
|
ELECTRICIEN
|
||||||
|
MAGNIFIQUE
|
||||||
|
COLLECTEURS
|
||||||
|
DETERMINANT
|
||||||
|
FASCINANTS
|
||||||
|
HABITUATION
|
||||||
|
DISPERSION
|
||||||
|
FRATERNITE
|
||||||
|
INTEGRITES
|
||||||
|
RESPONSABLE
|
||||||
|
DEPLACEMENT
|
||||||
|
CONCUBINE
|
||||||
|
EPHEMERE
|
||||||
|
SALOPARD
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
anticonstitutionnellement
|
|
||||||
éclésiastique
|
|
||||||
abandonner
|
|
||||||
seringue
|
|
||||||
vocifération
|
|
||||||
éponyme
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user