forked from menault/TD3_DEV51_Qualite_Algo
Compare commits
3 Commits
728b639857
...
vaisse
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32de0b90e7 | ||
|
|
87487bde84 | ||
| c650075e86 |
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -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