🎨 Improve die faces, expose Value, make use of inheritance #86

Merged
alexis.drai merged 1 commits from improve-die-faces into main 3 years ago

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

@ -8,22 +8,34 @@ namespace Model.Dice.Faces
{ {
public class ColorDieFace : AbstractDieFace public class ColorDieFace : AbstractDieFace
{ {
private static readonly int MAX_HEX = 16777215;
/// <summary> /// <summary>
/// a decimal representation of the hex (...representation of the color) /// accepts hex strings like "ffbb84" and "#af567d" ([RRGGBB])
/// </summary>
protected override int Value { get; }
/// <summary>
/// accepts hex strings like "ffbb84" ([RRGGBB])
/// </summary> /// </summary>
/// <param name="hexValueString">hex string</param> /// <param name="hexValueString">hex string</param>
public ColorDieFace(string hexValueString) public ColorDieFace(string hexValueString)
{ {
// https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again // 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> /// <summary>
@ -31,13 +43,11 @@ namespace Model.Dice.Faces
/// </summary> /// </summary>
/// <param name="decimalValue"></param> /// <param name="decimalValue"></param>
public ColorDieFace(int decimalValue) public ColorDieFace(int decimalValue)
{ : this(decimalValue.ToString())
Value = decimalValue; { }
}
public override object GetPracticalValue() public override object GetPracticalValue()
{ {
// https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again // https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again
// maybe prepend it with a "#"...
return Value.ToString("X6").Insert(0, "#"); return Value.ToString("X6").Insert(0, "#");
} }
} }

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -8,18 +9,9 @@ namespace Model.Dice.Faces
{ {
public class ImageDieFace : AbstractDieFace 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) public ImageDieFace(string uri)
{ {
/*parse an int after the last occurrence of "/" ? */ Value = int.Parse(Path.GetFileNameWithoutExtension(uri));
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;
} }
public ImageDieFace(int code) public ImageDieFace(int code)
@ -29,7 +21,7 @@ namespace Model.Dice.Faces
public override object GetPracticalValue() 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 public class NumberDieFace : AbstractDieFace
{ {
protected override int Value { get; }
public NumberDieFace(int value) public NumberDieFace(int value)
{ {
Value = value; Value = value;

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

Loading…
Cancel
Save