Solution possible au laboratoire : DnD
static void Main(string[] args)
{
Random rng = new Random();
int optionMenu = 0;
string menu = "DnD Lite\n 1. Afficher les stats des adversaires\n 2. Simuler le combat\n 3. Historique du dernier combat\n 4. Quitter";
string historique = "";
int[] statsGuerrier = new int[5];
int[] statsOrc = new int[5];
statsGuerrier[0] = 30;
statsGuerrier[1] = rng.Next(14, 19);
statsGuerrier[2] = 18;
statsGuerrier[3] = 8;
statsGuerrier[4] = 2 + calculerBonusDmg(statsGuerrier[1]);
statsOrc[0] = 30;
statsOrc[1] = rng.Next(10, 19);
statsOrc[2] = 14;
statsOrc[3] = 10;
statsOrc[4] = 2 + calculerBonusDmg(statsOrc[1]);
while (optionMenu != 4)
{
optionMenu = choisirOptionMenu(menu, 4);
Console.Clear();
if (optionMenu == 1)
{
afficherStats(statsGuerrier, "Guerrier");
afficherStats(statsOrc, "Orc");
}
else if (optionMenu == 2)
{
historique = simulerCombat(statsGuerrier, statsOrc);
}
else if (optionMenu == 3)
{
Console.WriteLine(historique);
}
if (optionMenu != 4)
{
Console.WriteLine("Appuyer sur une touche pour continuer");
Console.ReadKey();
}
}
}
static int calculerBonusDmg(int statAtt)
{
return (statAtt - 9)/2;
}
static int choisirOptionMenu(string menu, int nbOptions)
{
int choix = 0;
while (choix < 1 || choix > nbOptions)
{
Console.Clear();
Console.WriteLine(menu);
int.TryParse(Console.ReadLine(), out choix);
}
return choix;
}
static void afficherStats(int[] stats, string nom)
{
Console.WriteLine(nom);
Console.WriteLine("Hp : " + stats[0]);
Console.WriteLine("Att : " + stats[1]);
Console.WriteLine("Def : " + stats[2]);
}
static string simulerCombat(int[] statsJoueur, int[] statsEnnemi)
{
Random rng = new Random();
int initiativeJoueur = 0;
int initiativeEnnemi = 0;
int dommage = 0;
int round = 0;
string historique = "";
while (statsJoueur[0] > 0 && statsEnnemi[0] > 0)
{
round++;
historique += $"\n#############\nRound {round}\n";
initiativeJoueur = rng.Next(1, 21);
initiativeEnnemi = rng.Next(1, 21);
if (initiativeJoueur >= initiativeEnnemi)
{
historique += "Guerrier en premier \n\n";
dommage = attaquer(statsJoueur, statsEnnemi);
if (dommage > 0)
{
historique += "Le guerrier frappe pour " + dommage + " points de dommage \n";
statsEnnemi[0] -= dommage;
}
else
{
historique += "Le guerrier rate!\n";
}
dommage = attaquer(statsEnnemi, statsJoueur);
if (dommage > 0)
{
historique += "L'orc frappe pour " + dommage + " points de dommage \n";
statsJoueur[0] -= dommage;
}
else
{
historique += "L'orc rate!\n";
}
}
else
{
historique += "Orc en premier \n\n";
dommage = attaquer(statsEnnemi, statsJoueur);
if (dommage > 0)
{
historique += "L'orc frappe pour " + dommage + " points de dommage\n";
statsJoueur[0] -= dommage;
}
else
{
historique += "L'orc rate!\n";
}
dommage = attaquer(statsJoueur, statsEnnemi);
if (dommage > 0)
{
historique += "Le guerrier frappe pour " + dommage + " points de dommage \n";
statsEnnemi[0] -= dommage;
}
else
{
historique += "Le guerrier rate!\n";
}
}
historique += "Hp guerrier : " + statsJoueur[0] + " // Hp orc : " + statsEnnemi[0] + "\n";
}
Console.WriteLine("Hp guerrier : " + statsJoueur[0] + " // Hp orc : " + statsEnnemi[0]);
return historique;
}
static int attaquer(int[] statsJoueur, int[] statsEnnemi)
{
Random rng = new Random();
int d20 = 0;
int dommage = 0;
d20 = rng.Next(1, 21);
if (d20 > statsEnnemi[2])
{
dommage = rng.Next(1, statsJoueur[3] + 1) + statsJoueur[4];
}
return dommage;
}