153 lines
2.9 KiB
C
153 lines
2.9 KiB
C
|
# include <stdio.h>
|
||
|
# include <stdlib.h>
|
||
|
# include <graph.h>
|
||
|
|
||
|
typedef struct {
|
||
|
char valeurs[100];
|
||
|
int debut;
|
||
|
int fin;
|
||
|
size_t taille;
|
||
|
} file;
|
||
|
|
||
|
void initialise_file(file* f) {
|
||
|
f->debut = 0;
|
||
|
f->fin = 0;
|
||
|
f->taille = 0;
|
||
|
}
|
||
|
|
||
|
int empty(file* f) {
|
||
|
return (f->taille == 0);
|
||
|
}
|
||
|
|
||
|
void enqueue(file* f, char c) {
|
||
|
f->valeurs[f->fin] = c;
|
||
|
f->fin++;
|
||
|
f->taille++;
|
||
|
}
|
||
|
|
||
|
char dequeue(file* f) {
|
||
|
if (empty(f)) {
|
||
|
printf("Erreur : La file est vide.\n");
|
||
|
return;
|
||
|
}
|
||
|
f->debut++;
|
||
|
f->taille--;
|
||
|
return f->valeurs[f->debut-1];
|
||
|
}
|
||
|
|
||
|
|
||
|
void affiche_file(file* f) {
|
||
|
int i;
|
||
|
if (f->taille == 0) {
|
||
|
printf("File vide.\n");
|
||
|
return;
|
||
|
}
|
||
|
printf("<------------\n");
|
||
|
for (i = f->debut; i != f->fin; i++) {
|
||
|
if (i == 100) {
|
||
|
i = 0;
|
||
|
}
|
||
|
printf("%c ",f->valeurs[i]);
|
||
|
}
|
||
|
putchar('\n');
|
||
|
}
|
||
|
|
||
|
char first(file* f) {
|
||
|
return f->valeurs[f->debut];
|
||
|
}
|
||
|
|
||
|
void clear(file* f) {
|
||
|
while (!empty(f)) {
|
||
|
dequeue(f);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void affiche_couleurs(void) {
|
||
|
ChoisirCouleurDessin(CouleurParComposante(16,217,62)); /* Green */
|
||
|
RemplirRectangle(0,0,400,400);
|
||
|
ChoisirCouleurDessin(CouleurParComposante(245,14,14)); /* Red */
|
||
|
RemplirRectangle(400,0,400,400);
|
||
|
ChoisirCouleurDessin(CouleurParComposante(221,220,28)); /* Yellow */
|
||
|
RemplirRectangle(0,400,400,400);
|
||
|
ChoisirCouleurDessin(CouleurParComposante(28,95,221)); /* Blue */
|
||
|
RemplirRectangle(400,400,400,400);
|
||
|
}
|
||
|
|
||
|
void choisit_couleur_aleatoire(file* f) {
|
||
|
/* Met en valeur une couleur graphiquement et stocke la
|
||
|
couleur dans la file avec l'initiale de la couleur */
|
||
|
int x,y;
|
||
|
unsigned long ms = Microsecondes();
|
||
|
unsigned long cycle = 1000000L;
|
||
|
while (ms + cycle < Microsecondes())
|
||
|
srand(time(NULL));
|
||
|
x = rand() % 800;
|
||
|
y = rand() % 800;
|
||
|
if (x < 400 && y < 400) {
|
||
|
enqueue(f, 'G');
|
||
|
ChoisirCouleurDessin(CouleurParNom("green"));
|
||
|
RemplirRectangle(0,0,400,400);
|
||
|
}
|
||
|
else if (x > 400 && y < 400) {
|
||
|
enqueue(f, 'R');
|
||
|
ChoisirCouleurDessin(CouleurParNom("red"));
|
||
|
RemplirRectangle(400,0,400,400);
|
||
|
}
|
||
|
else if (x < 400 && y > 400) {
|
||
|
enqueue(f, 'Y');
|
||
|
ChoisirCouleurDessin(CouleurParNom("yellow"));
|
||
|
RemplirRectangle(0,400,400,400);
|
||
|
}
|
||
|
else if (x > 400 && y > 400) {
|
||
|
enqueue(f, 'B');
|
||
|
ChoisirCouleurDessin(CouleurParNom("blue"));
|
||
|
RemplirRectangle(400,400,400,400);
|
||
|
}
|
||
|
else {
|
||
|
printf("ERREUR");
|
||
|
}
|
||
|
ms = Microsecondes() + cycle;
|
||
|
while (Microsecondes() < ms) {
|
||
|
/* Attente 1sec */
|
||
|
}
|
||
|
affiche_couleurs();
|
||
|
}
|
||
|
|
||
|
char choix_utilisateur(void) {
|
||
|
/* TODO */
|
||
|
}
|
||
|
|
||
|
int main(void) {
|
||
|
int nb_tours = 1;
|
||
|
file f;
|
||
|
initialise_file(&f);
|
||
|
InitialiserGraphique();
|
||
|
CreerFenetre(200,100,800,800);
|
||
|
affiche_couleurs();
|
||
|
while (1) {
|
||
|
choisit_couleur_aleatoire(&f);
|
||
|
affiche_file(&f);
|
||
|
while (empty(f)) {
|
||
|
char couleur;
|
||
|
char choix;
|
||
|
couleur = dequeue(f);
|
||
|
choix = choix_utilisateur();
|
||
|
}
|
||
|
/* Boucle infinie */
|
||
|
}
|
||
|
/*affiche_file(f);
|
||
|
enqueue(f, 'A');
|
||
|
enqueue(f, 'B');
|
||
|
enqueue(f, 'C');
|
||
|
affiche_file(f);
|
||
|
dequeue(f);
|
||
|
affiche_file(f);
|
||
|
dequeue(f);
|
||
|
affiche_file(f);
|
||
|
enqueue(f, 'D');
|
||
|
affiche_file(f);
|
||
|
clear(f);
|
||
|
affiche_file(f);*/
|
||
|
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|