Merge pull request '🎨 Improve die faces, expose Value, make use of inheritance' (#86) from improve-die-faces into main
continuous-integration/drone/push Build is passing Details

Reviewed-on: #86
pull/88/head
Alexis Drai 3 years ago
commit 2556b46192

@ -14,7 +14,7 @@ namespace Model.Dice.Faces
/// <br/>
/// USE GetPracticalValue for a Value specific to face type
/// </summary>
protected abstract int Value { get; }
public int Value { get; protected set; }
public abstract object GetPracticalValue();

@ -8,22 +8,34 @@ namespace Model.Dice.Faces
{
public class ColorDieFace : AbstractDieFace
{
private static readonly int MAX_HEX = 16777215;
/// <summary>
/// a decimal representation of the hex (...representation of the color)
/// </summary>
protected override int Value { get; }
/// <summary>
/// accepts hex strings like "ffbb84" ([RRGGBB])
/// accepts hex strings like "ffbb84" and "#af567d" ([RRGGBB])
/// </summary>
/// <param name="hexValueString">hex string</param>
public ColorDieFace(string hexValueString)
{
// https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again
// we remove any initial '#' before parsing
if (hexValueString.StartsWith('#'))
{
hexValueString = hexValueString[1..];
}
// if style is ("f0b"), this constructor can develop it to "ff00bb" before doing the job
// if style is "f0b", this constructor can develop it to "ff00bb" before doing the job
if (hexValueString.Length == 3)
{
foreach (char ch in hexValueString)
{
// replace one instance of the char by two instances of it
hexValueString = hexValueString.Replace(new string(ch, 1), new string(ch, 2));
}
}
int result = int.Parse(hexValueString, System.Globalization.NumberStyles.HexNumber);
Value = int.Parse(hexValueString, System.Globalization.NumberStyles.HexNumber);
if (result < 0) Value = 0;
else if (result > MAX_HEX) Value = MAX_HEX;
else Value = result;
}
/// <summary>
@ -31,13 +43,11 @@ namespace Model.Dice.Faces
/// </summary>
/// <param name="decimalValue"></param>
public ColorDieFace(int decimalValue)
{
Value = decimalValue;
}
: this(decimalValue.ToString())
{ }
public override object GetPracticalValue()
{
// https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again
// maybe prepend it with a "#"...
return Value.ToString("X6").Insert(0, "#");
}
}

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -8,18 +9,9 @@ namespace Model.Dice.Faces
{
public class ImageDieFace : AbstractDieFace
{
/// <summary>
/// an image URL code, to find the image URL (to find the image)
/// </summary>
protected override int Value { get; }
public ImageDieFace(string uri)
{
/*parse an int after the last occurrence of "/" ? */
string resultString = uri[(uri.LastIndexOf('/') + 1)..];
/* !! here we should make sure to remove any ".jpg" etc, if there was one in the uri*/
int result = int.Parse(resultString);
Value = result;
Value = int.Parse(Path.GetFileNameWithoutExtension(uri));
}
public ImageDieFace(int code)
@ -29,7 +21,7 @@ namespace Model.Dice.Faces
public override object GetPracticalValue()
{
return string.Format("Assets/images/{0}", Value);
return string.Format($"Assets/images/{Value}.png");
}
}
}

@ -8,7 +8,6 @@ namespace Model.Dice.Faces
{
public class NumberDieFace : AbstractDieFace
{
protected override int Value { get; }
public NumberDieFace(int value)
{
Value = value;

@ -383,8 +383,8 @@ namespace Tests.Model_UTs
"\nPlayers: Alice Bob" +
"\nNext: Alice" +
"\nLog:" +
"\n\t" + date + " " + time + " -- Alice rolled: 4 Assets/images/40 #A00FA0" +
"\n\t" + date + " " + time + " -- Bob rolled: 3 Assets/images/20 #A00BB8" +
"\n\t" + date + " " + time + " -- Alice rolled: 4 Assets/images/40.png #A00FA0" +
"\n\t" + date + " " + time + " -- Bob rolled: 3 Assets/images/20.png #A00BB8" +
"\n";
string actual = game.ToString();

Loading…
Cancel
Save