Merge pull request '🐛 Prevent Die ctor from making dice with no face' (#140) from fix-#127 into main
continuous-integration/drone/push Build is passing Details

Reviewed-on: #140
pull/182/head
Alexis Drai 2 years ago
commit 800c8dd8f8

@ -274,13 +274,17 @@ namespace App
Console.WriteLine("create a face with a number, or enter 'ok' if you're finished"); Console.WriteLine("create a face with a number, or enter 'ok' if you're finished");
menuChoiceNewFaces = Console.ReadLine(); menuChoiceNewFaces = Console.ReadLine();
PreventEmptyDieCreation(ref menuChoiceNewFaces, faces.Count);
if (!menuChoiceNewFaces.Equals("ok") && int.TryParse(menuChoiceNewFaces, out int num)) if (!menuChoiceNewFaces.Equals("ok") && int.TryParse(menuChoiceNewFaces, out int num))
{ {
faces.Add(new(num)); faces.Add(new(num));
} }
} }
die = new NumberDie(faces.ToArray()); NumberFace[] facesArr = faces.ToArray();
die = new NumberDie(facesArr[0], facesArr[1..]);
return die; return die;
} }
@ -294,10 +298,15 @@ namespace App
{ {
Console.WriteLine("create a face with an color name, or enter 'ok' if you're finished"); Console.WriteLine("create a face with an color name, or enter 'ok' if you're finished");
menuChoiceNewFaces = Console.ReadLine(); menuChoiceNewFaces = Console.ReadLine();
PreventEmptyDieCreation(ref menuChoiceNewFaces, faces.Count);
if (menuChoiceNewFaces != "ok") faces.Add(new(Color.FromName(menuChoiceNewFaces))); if (menuChoiceNewFaces != "ok") faces.Add(new(Color.FromName(menuChoiceNewFaces)));
} }
die = new ColorDie(faces.ToArray()); ColorFace[] facesArr = faces.ToArray();
die = new ColorDie(facesArr[0], facesArr[1..]);
return die; return die;
} }
@ -312,6 +321,8 @@ namespace App
Console.WriteLine("create a face with an image uri, or enter 'ok' if you're finished"); Console.WriteLine("create a face with an image uri, or enter 'ok' if you're finished");
menuChoiceNewFaces = Console.ReadLine(); menuChoiceNewFaces = Console.ReadLine();
PreventEmptyDieCreation(ref menuChoiceNewFaces, faces.Count);
if (menuChoiceNewFaces != "ok") if (menuChoiceNewFaces != "ok")
{ {
try try
@ -329,10 +340,23 @@ namespace App
} }
} }
} }
die = new ImageDie(faces.ToArray());
ImageFace[] facesArr = faces.ToArray();
die = new ImageDie(facesArr[0], facesArr[1..]);
return die; return die;
} }
private static void PreventEmptyDieCreation(ref string menuChoice, int count)
{
if (menuChoice.Equals("ok") && count == 0)
{
Console.WriteLine("create at least one valid face");
menuChoice = ""; // persiste en dehors du scope de cette fonction
}
}
private static IEnumerable<Die> PrepareDice(MasterOfCeremonies masterOfCeremonies) private static IEnumerable<Die> PrepareDice(MasterOfCeremonies masterOfCeremonies)
{ {
List<Die> result = new(); List<Die> result = new();

@ -27,8 +27,8 @@ namespace Data
NumberFace[] d6Faces = new NumberFace[] { new(1), new(2), new(3), new(4), new(5), new(6) }; NumberFace[] d6Faces = new NumberFace[] { new(1), new(2), new(3), new(4), new(5), new(6) };
monopolyDice.Add(new NumberDie(new NumberFace(1), new NumberFace(1), new NumberFace(1), new NumberFace(1))); monopolyDice.Add(new NumberDie(new NumberFace(1), new NumberFace(1), new NumberFace(1), new NumberFace(1)));
monopolyDice.Add(new NumberDie(d6Faces)); monopolyDice.Add(new NumberDie(d6Faces[0], d6Faces[1..]));
monopolyDice.Add(new NumberDie(d6Faces)); monopolyDice.Add(new NumberDie(d6Faces[0], d6Faces[1..]));
ColorFace[] colorFaces = new ColorFace[] ColorFace[] colorFaces = new ColorFace[]
{ {
@ -40,7 +40,7 @@ namespace Data
new(Color.FromName("white")) new(Color.FromName("white"))
}; };
monopolyDice.Add(new ColorDie(colorFaces)); monopolyDice.Add(new ColorDie(colorFaces[0], colorFaces[1..]));
string rootPath = "https://unsplash.com/photos/"; string rootPath = "https://unsplash.com/photos/";
@ -52,7 +52,7 @@ namespace Data
new(new Uri(rootPath + "A_Ncbi-RH6s")), new(new Uri(rootPath + "A_Ncbi-RH6s")),
}; };
monopolyDice.Add(new ImageDie(imageFaces)); monopolyDice.Add(new ImageDie(imageFaces[0], imageFaces[1..]));
NumberFace[] d20Faces = new NumberFace[] { NumberFace[] d20Faces = new NumberFace[] {
new(1), new(2), new(3), new(4), new(5), new(1), new(2), new(3), new(4), new(5),
@ -61,7 +61,7 @@ namespace Data
new(16), new(17), new(18), new(19), new(20) new(16), new(17), new(18), new(19), new(20)
}; };
dndDice.Add(new NumberDie(d20Faces)); dndDice.Add(new NumberDie(d20Faces[0], d20Faces[1..]));
gr.DiceGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(dndName, dndDice.AsEnumerable())); gr.DiceGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(dndName, dndDice.AsEnumerable()));
gr.DiceGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(monopolyName, monopolyDice.AsEnumerable())); gr.DiceGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(monopolyName, monopolyDice.AsEnumerable()));

@ -5,9 +5,9 @@ namespace Model.Dice
{ {
public class ColorDie : HomogeneousDie<Color> public class ColorDie : HomogeneousDie<Color>
{ {
public ColorDie(params ColorFace[] faces) : base(faces) public ColorDie(ColorFace first, params ColorFace[] faces)
: base(first, faces)
{ {
} }
} }
} }

@ -13,9 +13,9 @@ namespace Model.Dice
private readonly List<Face> faces = new(); private readonly List<Face> faces = new();
protected Die(params Face[] faces) protected Die(Face first, params Face[] faces)
{ {
this.faces.AddRange(faces); this.faces.AddRange(faces.Append(first));
} }
public virtual Face GetRandomFace() public virtual Face GetRandomFace()

@ -4,7 +4,8 @@ namespace Model.Dice
{ {
public abstract class HomogeneousDie<T> : Die public abstract class HomogeneousDie<T> : Die
{ {
protected HomogeneousDie(params Face<T>[] faces) : base(faces) protected HomogeneousDie(Face<T> first, params Face<T>[] faces)
: base(first, faces)
{ {
} }

@ -5,7 +5,8 @@ namespace Model.Dice
{ {
public class ImageDie : HomogeneousDie<Uri> public class ImageDie : HomogeneousDie<Uri>
{ {
public ImageDie(params ImageFace[] faces) : base(faces) public ImageDie(ImageFace first, params ImageFace[] faces)
: base(first, faces)
{ {
} }
} }

@ -4,7 +4,8 @@ namespace Model.Dice
{ {
public class NumberDie : HomogeneousDie<int> public class NumberDie : HomogeneousDie<int>
{ {
public NumberDie(params NumberFace[] faces) : base(faces) public NumberDie(NumberFace first, params NumberFace[] faces)
: base(first, faces)
{ {
} }
} }

Loading…
Cancel
Save