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.
113 lines
3.7 KiB
113 lines
3.7 KiB
using CoreLibrary.Core;
|
|
using CoreLibrary.Evenements;
|
|
|
|
namespace CoreLibrary.Joueurs
|
|
{
|
|
public class Robot : Joueur
|
|
{
|
|
private static int nbRobots;
|
|
|
|
private List<Code>? codesPossibles;
|
|
|
|
static Robot()
|
|
{
|
|
nbRobots = 0;
|
|
}
|
|
|
|
public Robot() :
|
|
base($"Naps {nbRobots + 1}")
|
|
{
|
|
++nbRobots;
|
|
}
|
|
|
|
public Robot(string nom) :
|
|
base(nom)
|
|
{
|
|
}
|
|
|
|
public override void QuandDemanderJoueurJouer(object? sender, PartieDemanderJoueurJouerEventArgs e)
|
|
{
|
|
if (e.Nom != Nom)
|
|
return;
|
|
|
|
if (codesPossibles == null)
|
|
codesPossibles = GenererTousCodesPossibles(e.Code.TailleMax);
|
|
|
|
SupprimerCodesImpossibles(codesPossibles, e.Plateau);
|
|
|
|
for(int i = 0; i < e.Code.TailleMax; ++i)
|
|
e.Code.AjouterJeton(codesPossibles.ElementAt(0).Jetons[i]);
|
|
}
|
|
|
|
private static List<Code> GenererTousCodesPossibles(int tailleCode)
|
|
{
|
|
Couleur[] couleurs = Enum.GetValues<Couleur>();
|
|
int nbLignes = (int)Math.Pow(couleurs.Length, tailleCode);
|
|
Jeton?[,] jetons = new Jeton?[nbLignes, tailleCode];
|
|
|
|
for(int indiceColonne = 0; indiceColonne < jetons.GetLength(1); ++indiceColonne)
|
|
{
|
|
int repetition = nbLignes / (int)Math.Pow(couleurs.Length, (indiceColonne + 1));
|
|
|
|
for (int indiceLigne = 0; indiceLigne < jetons.GetLength(0); ++indiceLigne)
|
|
{
|
|
jetons[indiceLigne, indiceColonne] = new Jeton(couleurs[(indiceLigne / repetition) % couleurs.Length]);
|
|
}
|
|
}
|
|
|
|
List<Code> codes = new List<Code>();
|
|
for (int i = 0; i < jetons.GetLength(0); ++i)
|
|
{
|
|
Code code = new Code(tailleCode);
|
|
for (int j = 0; j < jetons.GetLength(1); ++j)
|
|
{
|
|
code.AjouterJeton(jetons[i, j]!.Value);
|
|
}
|
|
codes!.Add(code);
|
|
}
|
|
|
|
return codes;
|
|
}
|
|
|
|
private static bool EstCodePossible(Plateau plateau, Code code)
|
|
{
|
|
for(int i = 0; i < plateau.Taille; ++i)
|
|
{
|
|
Code sonCode = new Code(code.TailleMax);
|
|
for(int j = 0; j < code.TailleMax; ++j)
|
|
{
|
|
sonCode.AjouterJeton(plateau.Grille.Item1.ElementAt(i).ElementAt(j));
|
|
}
|
|
|
|
IReadOnlyList<Indicateur> indicateurs = sonCode.Comparer(code);
|
|
|
|
if (
|
|
|
|
indicateurs.Count(indicateur => indicateur == Indicateur.BonnePlace) != plateau.Grille.Item2.ElementAt(i).Count(indicateur => indicateur == Indicateur.BonnePlace) ||
|
|
indicateurs.Count(indicateur => indicateur == Indicateur.BonneCouleur) != plateau.Grille.Item2.ElementAt(i).Count(indicateur => indicateur == Indicateur.BonneCouleur)
|
|
)
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private static void SupprimerCodesImpossibles(List<Code> codes, Plateau plateau)
|
|
{
|
|
if (codes == null)
|
|
return;
|
|
|
|
List<int> indicesASupprimer = new List<int>();
|
|
|
|
for(int i = codes.Count - 1; i >= 0; --i)
|
|
{
|
|
if(!EstCodePossible(plateau, codes.ElementAt(i)))
|
|
indicesASupprimer.Add(i);
|
|
}
|
|
|
|
foreach (int indice in indicesASupprimer)
|
|
codes.RemoveAt(indice);
|
|
}
|
|
}
|
|
}
|