forked from JohnRosey/TP1-INF1008
/
Labyrinthe.cs
353 lines (299 loc) · 12.4 KB
/
Labyrinthe.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TP1_INF1008.Data;
using TP1_INF1008.Model;
using static TP1_INF1008.Model.Noeud;
namespace TP1_INF1008
{
public partial class Labyrinthe : Form
{
private Map map;
private static readonly string adresseLabyrinthe = "..\\..\\LabyrintheDessin.txt";
private static readonly string adresseCalcul = "..\\..\\LabyrintheCalcul.txt";
//private HashSet<Liaison> liaisonFinale = null;
private int largeur;
private int longueur;
private static readonly int MIN = 1;
private int max;
public static Labyrinthe UserInterface;
private static int nbOperationLabyrinthe;
private HashSet<Liaison> liaisonsFinales = null;
public Labyrinthe()
{
InitializeComponent();
UserInterface = this;
}
/* Methode qui permet d'aller chercher un Noeud sur la Map */
public Noeud GetNoeud(int posX, int posY)
{
map.isValideXY(posX, posY);
return new Noeud(this, posX, posY);
}
public Map GetMap()
{
return map;
}
/**
* Sauvegarde sous un format visuel le labyrunthe avec comme mur, les liaisons données.
*/
// TODO : Chercher une meilleure methode
public void saveToFile()
{
// Écrire dans le fichier "LabyrintheDessin.txt"
File.WriteAllText(adresseLabyrinthe,
$"\n\t\tLabyrinthe\n\t\t----------\n" +
$"\n{AffichageLabyrinthe()}");
// Écrire dans le fichier "LabyrintheCalcul.txt"
File.WriteAllText(adresseCalcul,
$"{lbl_infoDimension.Text}" +
$"\n{lbl_operation.Text}");
}
// Lorsqu'on clique sur le Bouton Générer
private void btn_generer_Click(object sender, EventArgs e)
{
// Binding Data with User Interface
longueur = Convert.ToInt32(txtBox_Longueur.Text.ToString());
largeur = Convert.ToInt32(txtBox_Largeur.Text.ToString());
max = Convert.ToInt32(txtBox_max.Text.ToString());
map = new Map(longueur, largeur);
map.PoidsAleatoires(MIN, max);
nbOperationLabyrinthe += map.GetNbreOperation();
lbl_operation.Text = $"Nombre d'opération : {nbOperationLabyrinthe}";
lbl_infoDimension.Text = $"information dimension : {map}";
// execution de l'algorithme de Prim
Prim();
// puis Affichage à la console
Console.WriteLine(AffichageLabyrinthe());
// Enregistrement du Labyrinthe produit au fichier .txt
saveToFile();
}
/**
* La Methode de Prim
*/
public HashSet<Liaison> Prim()
{
nbOperationLabyrinthe = 0;
HashSet<Liaison> liaisionDechues = new HashSet<Liaison>();
HashSet<Liaison> liaisonPossible = new HashSet<Liaison>();
liaisonsFinales = new HashSet<Liaison>();
HashSet<Noeud> noeudFinale = new HashSet<Noeud>();
Noeud noeudToAdd = GetNoeud(0, 0);
// Tant que le nombre cases possibles est inferieur à la surface du map
while (noeudFinale.Count < GetMap().GetLargeur * GetMap().GetLongueur)
{
noeudFinale.Add(noeudToAdd);
// on ajoute les voisins de la case ajouté dans les liaisons possible
var values = EnumDirection.GetValues<Direction>();
Liaison liaisonTempo;
foreach (Direction direction in values)
{
// try si le noeud n'a pas de voisin dans la direction donnée
try
{
liaisonTempo = noeudToAdd.getLiaison(direction);
// si la liaison est déjà présente dans les liaisons possibles
if (liaisonPossible.Contains(liaisonTempo))
{
// on supprime la liaison des liaisons possibles
// et on l'ajoute dans les liaisons rejété
// (car elle connecte deux Case déjà solution)
liaisonPossible.Remove(liaisonTempo);
liaisionDechues.Add(liaisonTempo);
// TODO : debug pour send Event
}
else if (!liaisionDechues.Contains(liaisonTempo) && !liaisonsFinales.Contains(liaisonTempo))
{
// on l'ajoute dans les liaisons possibles
liaisonPossible.Add(liaisonTempo);
// on notifie le UI nouvelle liaison possible
// TODO : debug pour send Event
}
}
catch (Exception)
{
// pass
}
nbOperationLabyrinthe += 1;
}
// S'il y a encore des liaisons possibles
if (liaisonPossible.Count != 0)
{
// Recherche la prochaine liaison potentielle
//liaisonTempo = liaisonPossible.OrderBy(x => this.CompareTo(x));
liaisonTempo = liaisonPossible.Min();
// on prend le noeud pas encore dans la solution
if (!noeudFinale.Contains(liaisonTempo.NodeDepart))
noeudToAdd = liaisonTempo.NodeDepart;
else
noeudToAdd = liaisonTempo.NodeArrive;
// on transfère la liaison de Possible à Solution Finale
liaisonPossible.Remove(liaisonTempo);
liaisonsFinales.Add(liaisonTempo);
// on notifie le UI nouvelle liaison solution
// TODO : sendEvent(liaisonTmp, 1);
}
}
return liaisonsFinales;
}
public string AffichageLabyrinthe()
{
// Vérifier si la methode de prim a déjà été executée au préalable
if (liaisonsFinales == null)
{
MessageBox.Show("Commencer par Lancer l'algorithme de PRIM !");
return null;
}
StringBuilder str = new StringBuilder();
Liaison[][] ecran = new Liaison[GetMap().GetLargeur * 2 - 1][];
for (int y = 0; y < (GetMap().GetLargeur * 2) - 1; y++)
{
ecran[y] = new Liaison[GetMap().GetLongueur * 2 - 1];
}
// parcourir les liaisons pour les ajouter dans le Tableau
Liaison tmp;
foreach (Liaison item in liaisonsFinales)
{
tmp = item;
int tmp_y = tmp.getCoordY();
int tmp_x = tmp.GetCoordX();
ecran[tmp_y][tmp_x] = tmp;
}
for (int y = 0; y < GetMap().GetLongueur * 2 - 1; y++)
{
for (int x = 0; x < GetMap().GetLargeur * 2 - 1; x++)
{
if (x % 2 == 0 && y % 2 == 0)
{
str.Append(ProduireDirectionLiaison(
ElementExiste(x, y - 1, ecran),
ElementExiste(x + 1, y, ecran),
ElementExiste(x, y + 1, ecran),
ElementExiste(x - 1, y, ecran)
));
}
else if(x % 2 == 1 && y % 2 == 0)
{
// liaison Horizontale
if (ElementExiste(x, y, ecran))
str.Append("═"); // Alt + 205
else
str.Append(" ");
}
else if (x % 2 == 0)
{
// Liaison verticale
if (ElementExiste(x, y, ecran))
str.Append("║"); // Alt + 186
else
str.Append(" ");
}
else
{
str.Append(" ");
}
str.Append(" ");
}
str.Append("\n");
}
return str.ToString();
}
/** Fonction assez Générique qui vérifie un élément au coordinnée x et y existe dans le tableau ou pas
* return bool
*/
private bool ElementExiste(int x, int y, Object[][] table)
{
try
{
return table[y][x] != null;
}
catch (Exception)
{
// on l'ignore
}
return false;
}
// Représentation des murs
private string ProduireDirectionLiaison(bool haut, bool droite, bool bas, bool gauche)
{
// 1___
if (haut)
{
return
droite ?
#region 11__
(bas ?
#region 111_
(gauche ?
"╬" : // 1111 (Alt + 206)
"╠" // 1110 (Alt + 204)
) :
#endregion
#region 110_
(gauche ?
"╩" : // 1101 (Alt + 202)
"╚" // 1100 (Alt + 200)
)
#endregion
) :
#endregion
#region 10__
(bas ?
#region 101_
(gauche ?
"╣" : // 1011(Alt + 185)
"║" // 1010(Alt + 186)
) :
#endregion
#region 100_
(gauche ?
"╝" : // 1001 (Alt + 188)
"║" // 1000 (Alt + 186)
)
#endregion
);
#endregion
}
else
{
return
droite ?
#region 01__
(bas ?
#region 011_
(gauche ?
"╦" : // 0111 (Alt + 203)
"╔" // 0110 (Alt + 201)
) :
#endregion
#region 010_
(gauche ?
"═" : // 0101 (Alt + 205)
"═" // 0100 (Alt + 205) //TODO : doute
)
#endregion
) :
#endregion
#region 00__
(bas ?
#region 001_
(gauche ?
"╗" : // 0011(Alt + 187)
"║" // 0010(Alt + 186)
) :
#endregion
#region 000_
(gauche ?
"═" : // 0001 (Alt + 205)
"?" // Impossible
)
#endregion
);
#endregion
}
}
}
}