using MayoAbeille; using GénérateurId; ConfigInfo? cfg = Config.LireConfig("../../../config.json"); Abeille.SYMBOLE = cfg.Abeille.Symbole; Abeille.COULEUR = cfg.Abeille.Couleur; Alvéole.SYMBOLE = cfg.Alvéole.Symbole; Alvéole.COULEUR = cfg.Alvéole.Couleur; Miel.SYMBOLE = cfg.Miel.Symbole; Miel.COULEUR = cfg.Miel.Couleur; Vide.SYMBOLE = cfg.Vide.Symbole; Vide.COULEUR = cfg.Vide.Couleur; Surface surface = new(cfg.Surface.Hauteur, cfg.Surface.Largeur); try { surface = Populer( Décorer(surface, GénérerDécorations( surface, cfg.Surface.Décorations ) ), cfg.Surface.Abeilles ); Apiculteur bill = new("Bill", surface); } catch(DécorationsIncompatiblesException) { Environment.Exit(-1); // insérez du code si vous le jugez pertinent } catch(PopulationTropNombreuseException) { Environment.Exit(-2); // insérez du code si vous le jugez pertinent } catch (SurfacePleineException) { Environment.Exit(-3); // insérez du code si vous le jugez pertinent } catch (PositionConflictuelleException) { Environment.Exit(-4); // insérez du code si vous le jugez pertinent } Teindre(); while (!(surface.Compter(Miel.SYMBOLE) == 0)) { Projeter(surface); foreach(Abeille ab in RegistreAbeilles.Instance.Population) ab.Agir(surface); Décrire(RegistreAbeilles.Instance.Population, new(0, surface.Hauteur)); } Projeter(surface); Décrire(RegistreAbeilles.Instance.Population, new(0, surface.Hauteur)); /////// static void Décrire(List abeilles, Point2D pos) { Console.SetCursorPosition(pos.X, pos.Y); foreach(Abeille ab in abeilles) { Console.WriteLine($"{ab.Nom} à la position {ab.Pos}"); } foreach (string ev in RegistreÉvénements.Instance.ObtenirÉvénements(5)) Console.WriteLine(ev); } /// éventuellement, tenir compte de la position de la Surface static void Projeter(Surface surf) { for(int ligne = 0; ligne != surf.Hauteur; ++ligne) { for(int col = 0; col != surf.Largeur; ++col) { Point2D pt = new(col, ligne); Peintre.Peindre(pt, surf[pt]); } Console.WriteLine(); } } static List GénérerPositions(int hau, int lar) { List pos = new(); for (int i = 0; i != hau; ++i) for (int j = 0; j != lar; ++j) pos.Add(new(j, i)); return pos; } static Point2D Piger(List pts, Random dé) { if (pts.Count == 0) throw new SurfacePleineException(); int n = dé.Next(pts.Count); (pts[n], pts[pts.Count - 1]) = (pts[pts.Count - 1], pts[n]); Point2D pos = pts[pts.Count - 1]; pts.RemoveAt(pts.Count - 1); return pos; } static char ChoisirDécoration(Random dé) => dé.Next(0, 100) switch { < 15 => Miel.SYMBOLE, // [0..15) < 50 => Alvéole.SYMBOLE, // [15..50) _ => '.' // vide // [50..100) }; static void Teindre() { Peintre.Associer(Miel.SYMBOLE, Miel.COULEUR); Peintre.Associer(Alvéole.SYMBOLE, Alvéole.COULEUR); Peintre.Associer(Vide.SYMBOLE, Vide.COULEUR); Peintre.Associer(Abeille.SYMBOLE, Abeille.COULEUR); } static List GénérerDécorations(Surface surf, int nbDécos) { Random dé = new(); List pos = GénérerPositions(surf.Hauteur, surf.Largeur); List décos = new(); for (int i = 0; i != nbDécos; ++i) décos.Add(new(Piger(pos, dé), ChoisirDécoration(dé))); return décos; } static List Répertorier(Surface surf, char sym) { List pos = new(); for (int ligne = 0; ligne != surf.Hauteur; ++ligne) for (int col = 0; col != surf.Largeur; ++col) { Point2D pt = new(col, ligne); if (surf[pt].Symbole == sym) pos.Add(pt); } return pos; } static Surface Décorer(Surface surface, List décorations) { if (surface.Aire < décorations.Count) throw new SurfacePleineException(); foreach (Décoration déco in décorations) if (surface[déco.Pos].EstOccupée) throw new PositionConflictuelleException(déco.Pos); else surface[déco.Pos] = new(déco.Symbole); return surface; } static Surface Populer(Surface surface, int nbAbeilles) { Random dé = new(); IGénérateurId gen = new FabriqueGénérateurs().Créer(TypeGénérateur.Partagé, "AB"); List options = Répertorier(surface, Vide.SYMBOLE); if (options.Count < nbAbeilles) throw new SurfacePleineException(); for(int i = 0; i != nbAbeilles; ++i) { int ndx = dé.Next(options.Count); Abeille mayo = new(gen.Prendre(), options[ndx]); RegistreAbeilles.Instance.Ajouter(mayo); options.RemoveAt(ndx); surface[mayo.Pos].Symbole = Abeille.SYMBOLE; } return surface; } class DécorationsIncompatiblesException : Exception; class PopulationTropNombreuseException : Exception; class SurfacePleineException : Exception; class PositionConflictuelleException : Exception { Point2D Pos { get; init; } public PositionConflictuelleException(Point2D pos) { Pos = pos; } public override string Message => $"Position conflictuelle : {Pos}"; }