7#include <manux/config.h>
8#include <manux/types.h>
17#define COLONNE_MESSAGE 19
21#define LIGNE_MESSAGE (Y_BASE + 3)
24static int hauteurs[3];
30 printf(
"\033[%d;%dH", ligne, colonne);
61void disque(
int taille,
int x,
int y,
int effacer)
64 for (
int i = -taille; i <= taille; i++) {
70 for (
int t = 0; t < 3; t++)
73 printf(
"%c", axe ?
'|' :
' ');
86void message(
int taille,
int depart,
int arrivee)
90 printf(
"Le disque %d va de la tour %d a la tour %d",
91 taille, depart + 1, arrivee + 1);
95static void hanoiInformation(
const char *texte)
106 for (
int t = 0; t < 3; t++) {
113 for (
int i = 0; i < 2 * N + 1; i++)
116 for (
int i = 0; i < N; i++)
122static int hanoiLireTouche(
char *touche)
124 return lire(0, touche, 1) == 1;
128static void hanoiVerifierAbandon(
void)
131 if (modeDemo && hanoiLireTouche(&touche) && touche ==
'q')
136static char hanoiAttendreTouche(
void)
139 while (!hanoiLireTouche(&touche))
147 int taille = tours[depart][--hauteurs[depart]];
150 int y =
Y_BASE - 1 - hauteurs[depart];
151 int destination =
Y_BASE - 1 - hauteurs[arrivee];
153 message(taille, depart, arrivee);
160 hanoiVerifierAbandon();
161 if (abandon)
return 0;
165 x += (x < cible) ? 1 : -1;
168 hanoiVerifierAbandon();
169 if (abandon)
return 0;
171 while (y < destination) {
176 hanoiVerifierAbandon();
177 if (abandon)
return 0;
179 tours[arrivee][hauteurs[arrivee]++] = taille;
183void hanoi(
int n,
int depart,
int arrivee,
int auxiliaire)
185 if (n == 0 || abandon)
187 hanoi(n - 1, depart, auxiliaire, arrivee);
189 hanoiVerifierAbandon();
190 if (abandon || !
deplacer(depart, arrivee))
return;
191 hanoi(n - 1, auxiliaire, arrivee, depart);
194static void hanoiInitialiser(
void)
196 for (
int t = 0; t < 3; t++) {
202 for (
int i = 0; i < N; i++)
208static void hanoiConfigurer(
void)
213 printf(
"=== ManuX / Arcade : Hanoi ===");
215 printf(
"Nombre de disques : 3 a 9, ou 0 pour 10");
217 touche = hanoiAttendreTouche();
218 }
while (!((touche >=
'3' && touche <=
'9') || touche ==
'0'));
219 N = touche ==
'0' ? 10 : touche -
'0';
222 printf(
"Mode : d = demonstration, j = jeu");
224 touche = hanoiAttendreTouche();
225 }
while (touche !=
'd' && touche !=
'j');
226 modeDemo = touche ==
'd';
229static int hanoiMouvementValide(
int depart,
int arrivee)
231 if (depart == arrivee || hauteurs[depart] == 0)
233 if (hauteurs[arrivee] == 0)
235 return tours[depart][hauteurs[depart] - 1] <
236 tours[arrivee][hauteurs[arrivee] - 1];
239static void hanoiJouer(
void)
244 printf(
"Tours : 1, 2, 3 | q : quitter");
245 hanoiInformation(
"Choisir la tour de depart (1-3)");
247 while (!abandon && hauteurs[2] != N) {
248 touche = hanoiAttendreTouche();
253 if (touche <
'1' || touche >
'3')
256 depart = touche -
'1';
257 if (hauteurs[depart] == 0) {
258 hanoiInformation(
"Cette tour est vide !");
261 hanoiInformation(
"Choisir la tour d'arrivee (1-3)");
264 int arrivee = touche -
'1';
265 if (!hanoiMouvementValide(depart, arrivee)) {
266 hanoiInformation(
"Deplacement interdit ! Recommencer.");
271 if (hauteurs[2] != N)
272 hanoiInformation(
"Choisir la tour de depart (1-3)");
284 printf(
"Demonstration : q pour quitter");
293 printf(
"Partie interrompue !");
295 printf(
"Resolution terminee !");
297 printf(
"Bravo, partie gagnee !");
int deplacer(int depart, int arrivee)
void effacer_message(void)
void message(int taille, int depart, int arrivee)
void position(int ligne, int colonne)
void hanoi(int n, int depart, int arrivee, int auxiliaire)
void pause_animation(void)
void disque(int taille, int x, int y, int effacer)
int colonne_tour(int tour)
int lire(int fd, void *buffer, int nb)
void printf(char *format,...)
Un premier printf.
: Définition des fonctions de base du mode utilisateur de ManuX.
int attendreMillisecondes(uint32_t nMs)
un appel système sans intérêt si ce n'est de faire des tests/demo