You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mastermind/Sources/CoreLibrary/Core/Code.cs

89 lines
2.4 KiB

using CoreLibrary.Exceptions;
using System.Runtime.Serialization;
namespace CoreLibrary.Core
{
[DataContract]
public class Code
{
[DataMember]
private readonly List<Jeton> jetons = new List<Jeton>();
public IReadOnlyList<Jeton> Jetons => jetons;
public int Taille => jetons.Count;
[DataMember]
public int TailleMax { get; private init; }
public bool Complet => Taille == TailleMax;
public bool Vide => Taille == 0;
public Code(int taille)
{
if (taille < 0)
throw new TailleCodeException(taille);
TailleMax = taille;
}
public void AjouterJeton(Jeton jeton)
{
if (Complet)
throw new CodeCompletException();
jetons.Add(jeton);
}
public Jeton RecupererJeton(int indice)
{
if (indice < 0 || indice >= Taille)
throw new IndiceCodeException(indice, Taille - 1);
return jetons.ElementAt(indice);
}
public void SupprimerDernierJeton()
{
if (Vide)
throw new CodeVideException();
jetons.RemoveAt(Taille - 1);
}
public IReadOnlyList<Indicateur> Comparer(Code code)
{
// Je suis le bon code
List<Indicateur> indicateurs = new List<Indicateur>();
if (!code.Complet)
throw new CodeIncompletException();
if (code.TailleMax != TailleMax)
throw new CodeInvalideException();
List<Jeton> mesJetons = Jetons.ToList();
List<Jeton> sesJetons = code.Jetons.ToList();
for (int i = 0; i < mesJetons.Count; ++i)
{
if (mesJetons[i] == sesJetons[i])
{
mesJetons.RemoveAt(i);
sesJetons.RemoveAt(i);
indicateurs.Add(Indicateur.BonnePlace);
}
}
for (int i = 0; i < sesJetons.Count; ++i)
{
if (mesJetons.Remove(sesJetons[i]))
{
sesJetons.RemoveAt(i);
indicateurs.Add(Indicateur.BonneCouleur);
}
}
return indicateurs;
}
}
}