LCD numbers kata (C#)
using System.Collections.Generic; | |
using System.Linq; | |
namespace ViveCodigo.Katas.LCD | |
{ | |
/// <summary> | |
/// Basado en la idea original de @rodrigo_salado | |
/// </summary> | |
public class LCD | |
{ | |
/// <summary> | |
/// Diccionario que define los diferentes símbolos para pintar los números. | |
/// </summary> | |
private static readonly Dictionary<int, string> Leds = | |
new Dictionary<int, string> | |
{ | |
{0, " "}, | |
{1, " _ "}, | |
{2, "| |"}, | |
{3, "|_|"}, | |
{5, " _|"}, | |
{6, "|_ "}, | |
{7, " |"}, | |
{8, "| "}, | |
}; | |
/// <summary> | |
/// Diccionario que define los diferentes dígitos disponibles. | |
/// </summary> | |
private static readonly Dictionary<char, LCD> Digitos = | |
new Dictionary<char, LCD> | |
{ | |
{'0', new LCD(1, 2, 3)}, | |
{'1', new LCD(0, 7, 7)}, | |
{'2', new LCD(1, 5, 6)}, | |
{'3', new LCD(1, 5, 5)}, | |
{'4', new LCD(0, 3, 7)}, | |
{'5', new LCD(1, 6, 5)}, | |
{'6', new LCD(1, 6, 3)}, | |
{'7', new LCD(1, 7, 7)}, | |
{'8', new LCD(1, 3, 3)}, | |
{'9', new LCD(1, 3, 5)} | |
}; | |
/// <summary> | |
/// Arreglo que contiene la representación de cadena del dígito. | |
/// </summary> | |
private readonly string[] filas; | |
private LCD(int top, int mid, int bot) : | |
this(new[] {Leds[top], Leds[mid], Leds[bot]}) {} | |
private LCD(string[] filas) | |
{ | |
this.filas = filas; | |
} | |
private LCD() | |
{ | |
filas = new[] { "", "", "" }; | |
} | |
public override string ToString() | |
{ | |
return string.Join("\n", filas); | |
} | |
public static LCD operator +(LCD a, LCD b) | |
{ | |
return new LCD(a.filas.Select((t, i) => t + b.filas[i]).ToArray()); | |
} | |
public static string ToLCD(int numero) | |
{ | |
var result = new LCD(); | |
foreach (var digito in numero.ToString()) | |
{ | |
result += Digitos[digito]; | |
} | |
return result.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment