My Project
Chargement...
Recherche...
Aucune correspondance
hanoi.c
Aller à la documentation de ce fichier.
1/*
2 * Tours de Hanoi animees et interactives pour ManuX/Arcade (ANSI 80x25).
3 * Base : version adaptee pour ManuX par l'utilisateur.
4 * Concu avec l'aide de ChatGPT (OpenAI), 2026.
5 * SPDX-License-Identifier: 0BSD
6 */
7#include <manux/config.h>
8#include <manux/types.h>
9#include <stdio.h>
10#include <unistd.h>
11
12#define MAX_DISQUES 10
13#define NB_LIGNES 25
14#define NB_COLONNES 80
15#define Y_HAUT 3
16#define ESPACEMENT 20
17#define COLONNE_MESSAGE 19
18
19static int N = 4;
20#define Y_BASE (N + 6)
21#define LIGNE_MESSAGE (Y_BASE + 3)
22
23static int tours[3][MAX_DISQUES];
24static int hauteurs[3];
25static int abandon;
26static int modeDemo;
27
28void position(int ligne, int colonne)
29{
30 printf("\033[%d;%dH", ligne, colonne);
31}
32
33void rafraichir(void)
34{
35#ifdef __linux__
36 /* Conserve le choix de la version ManuX : pas de fflush. */
37 fflush(stdout);
38#endif
39}
40
42{
44}
45
46void effacer_ecran(void)
47{
48 for (int y = 1; y <= NB_LIGNES; y++) {
49 position(y, 1);
50 for (int x = 1; x < NB_COLONNES; x++)
51 printf(" ");
52 }
53 rafraichir();
54}
55
56int colonne_tour(int tour)
57{
58 return 12 + tour * ESPACEMENT;
59}
60
61void disque(int taille, int x, int y, int effacer)
62{
63 position(y, x - taille);
64 for (int i = -taille; i <= taille; i++) {
65 if (!effacer) {
66 printf("=");
67 } else {
68 int axe = 0;
69 if (y >= Y_HAUT && y < Y_BASE)
70 for (int t = 0; t < 3; t++)
71 if (x + i == colonne_tour(t))
72 axe = 1;
73 printf("%c", axe ? '|' : ' ');
74 }
75 }
76 rafraichir();
77}
78
80{
82 for (int i = 1; i < NB_COLONNES; i++)
83 printf(" ");
84}
85
86void message(int taille, int depart, int arrivee)
87{
90 printf("Le disque %d va de la tour %d a la tour %d",
91 taille, depart + 1, arrivee + 1);
92 rafraichir();
93}
94
95static void hanoiInformation(const char *texte)
96{
99 printf("%s", texte);
100 rafraichir();
101}
102
103void decor(void)
104{
106 for (int t = 0; t < 3; t++) {
107 int x = colonne_tour(t);
108 for (int y = Y_HAUT; y < Y_BASE; y++) {
109 position(y, x);
110 printf("|");
111 }
112 position(Y_BASE, x - N);
113 for (int i = 0; i < 2 * N + 1; i++)
114 printf("-");
115 }
116 for (int i = 0; i < N; i++)
117 disque(tours[0][i], colonne_tour(0), Y_BASE - 1 - i, 0);
118 rafraichir();
119}
120
121/* Un caractere disponible, ou 0 si aucun. */
122static int hanoiLireTouche(char *touche)
123{
124 return lire(0, touche, 1) == 1;
125}
126
127/* Dans la demo, q interrompt meme un mouvement en cours. */
128static void hanoiVerifierAbandon(void)
129{
130 char touche;
131 if (modeDemo && hanoiLireTouche(&touche) && touche == 'q')
132 abandon = 1;
133}
134
135/* Attend une touche sans faire d'hypothese sur le buffering clavier. */
136static char hanoiAttendreTouche(void)
137{
138 char touche;
139 while (!hanoiLireTouche(&touche))
141 return touche;
142}
143
144/* Retourne 0 si le mouvement est abandonne en cours d'animation. */
145int deplacer(int depart, int arrivee)
146{
147 int taille = tours[depart][--hauteurs[depart]];
148 int x = colonne_tour(depart);
149 int cible = colonne_tour(arrivee);
150 int y = Y_BASE - 1 - hauteurs[depart];
151 int destination = Y_BASE - 1 - hauteurs[arrivee];
152
153 message(taille, depart, arrivee);
154
155 while (y > 2) {
156 disque(taille, x, y, 1);
157 y--;
158 disque(taille, x, y, 0);
160 hanoiVerifierAbandon();
161 if (abandon) return 0;
162 }
163 while (x != cible) {
164 disque(taille, x, y, 1);
165 x += (x < cible) ? 1 : -1;
166 disque(taille, x, y, 0);
168 hanoiVerifierAbandon();
169 if (abandon) return 0;
170 }
171 while (y < destination) {
172 disque(taille, x, y, 1);
173 y++;
174 disque(taille, x, y, 0);
176 hanoiVerifierAbandon();
177 if (abandon) return 0;
178 }
179 tours[arrivee][hauteurs[arrivee]++] = taille;
180 return 1;
181}
182
183void hanoi(int n, int depart, int arrivee, int auxiliaire)
184{
185 if (n == 0 || abandon)
186 return;
187 hanoi(n - 1, depart, auxiliaire, arrivee);
188 if (abandon) return;
189 hanoiVerifierAbandon();
190 if (abandon || !deplacer(depart, arrivee)) return;
191 hanoi(n - 1, auxiliaire, arrivee, depart);
192}
193
194static void hanoiInitialiser(void)
195{
196 for (int t = 0; t < 3; t++) {
197 hauteurs[t] = 0;
198 for (int i = 0; i < MAX_DISQUES; i++)
199 tours[t][i] = 0;
200 }
201 hauteurs[0] = N;
202 for (int i = 0; i < N; i++)
203 tours[0][i] = N - i;
204 abandon = 0;
205}
206
207/* Choix des options : une seule touche suffit, pas besoin d'Entree. */
208static void hanoiConfigurer(void)
209{
210 char touche;
212 position(7, 24);
213 printf("=== ManuX / Arcade : Hanoi ===");
214 position(10, 18);
215 printf("Nombre de disques : 3 a 9, ou 0 pour 10");
216 do {
217 touche = hanoiAttendreTouche();
218 } while (!((touche >= '3' && touche <= '9') || touche == '0'));
219 N = touche == '0' ? 10 : touche - '0';
220
221 position(12, 18);
222 printf("Mode : d = demonstration, j = jeu");
223 do {
224 touche = hanoiAttendreTouche();
225 } while (touche != 'd' && touche != 'j');
226 modeDemo = touche == 'd';
227}
228
229static int hanoiMouvementValide(int depart, int arrivee)
230{
231 if (depart == arrivee || hauteurs[depart] == 0)
232 return 0;
233 if (hauteurs[arrivee] == 0)
234 return 1;
235 return tours[depart][hauteurs[depart] - 1] <
236 tours[arrivee][hauteurs[arrivee] - 1];
237}
238
239static void hanoiJouer(void)
240{
241 int depart = -1;
242 char touche;
243 position(LIGNE_MESSAGE + 2, 12);
244 printf("Tours : 1, 2, 3 | q : quitter");
245 hanoiInformation("Choisir la tour de depart (1-3)");
246
247 while (!abandon && hauteurs[2] != N) {
248 touche = hanoiAttendreTouche();
249 if (touche == 'q') {
250 abandon = 1;
251 break;
252 }
253 if (touche < '1' || touche > '3')
254 continue;
255 if (depart < 0) {
256 depart = touche - '1';
257 if (hauteurs[depart] == 0) {
258 hanoiInformation("Cette tour est vide !");
259 depart = -1;
260 } else {
261 hanoiInformation("Choisir la tour d'arrivee (1-3)");
262 }
263 } else {
264 int arrivee = touche - '1';
265 if (!hanoiMouvementValide(depart, arrivee)) {
266 hanoiInformation("Deplacement interdit ! Recommencer.");
267 } else {
268 deplacer(depart, arrivee);
269 }
270 depart = -1;
271 if (hauteurs[2] != N)
272 hanoiInformation("Choisir la tour de depart (1-3)");
273 }
274 }
275}
276
278{
279 hanoiConfigurer();
280 hanoiInitialiser();
281 decor();
282 if (modeDemo) {
283 position(LIGNE_MESSAGE + 2, 12);
284 printf("Demonstration : q pour quitter");
285 hanoi(N, 0, 2, 1);
286 } else {
287 hanoiJouer();
288 }
289
292 if (abandon)
293 printf("Partie interrompue !");
294 else if (modeDemo)
295 printf("Resolution terminee !");
296 else
297 printf("Bravo, partie gagnee !");
299 rafraichir();
300 return 0;
301}
void decor(void)
Definition hanoi.c:103
int resoudreHanoi(void)
Definition hanoi.c:277
#define MAX_DISQUES
Definition hanoi.c:12
int deplacer(int depart, int arrivee)
Definition hanoi.c:145
#define COLONNE_MESSAGE
Definition hanoi.c:17
void rafraichir(void)
Definition hanoi.c:33
void effacer_message(void)
Definition hanoi.c:79
void message(int taille, int depart, int arrivee)
Definition hanoi.c:86
#define LIGNE_MESSAGE
Definition hanoi.c:21
void position(int ligne, int colonne)
Definition hanoi.c:28
#define Y_HAUT
Definition hanoi.c:15
#define Y_BASE
Definition hanoi.c:20
#define ESPACEMENT
Definition hanoi.c:16
void hanoi(int n, int depart, int arrivee, int auxiliaire)
Definition hanoi.c:183
void pause_animation(void)
Definition hanoi.c:41
void disque(int taille, int x, int y, int effacer)
Definition hanoi.c:61
int colonne_tour(int tour)
Definition hanoi.c:56
#define NB_COLONNES
Definition hanoi.c:14
void effacer_ecran(void)
Definition hanoi.c:46
#define NB_LIGNES
#define stdout
Definition stdio.h:19
int lire(int fd, void *buffer, int nb)
void printf(char *format,...)
Un premier printf.
Definition stdio.c:142
: 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