C# / .NET : Jeu de pierre, feuille, ciseaux (jankenpon) - CodeS SourceS

PHOTO EMBED

Thu May 05 2022 17:20:43 GMT+0000 (Coordinated Universal Time)

Saved by @hiso #basic

// Crée avec SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/Default.aspx)
// Mail du programmeur: yanngeffrotin@gmail.com
// [je l'utilise donc je le soutiens avec www.paypal.fr]

/* Algorithme du Jaken (version épurée)

Algo Jaken

Constantes

Variables     
scor, scor2, choix : entiers
texte, choix2 : caractères

DEBUT

Afficher "Jeu de Jaken"
Afficher "Licence publique générale GNU"   
      
scor <- 0
scor2 <- 0
choix <- 0
choix2 <- "pierre"
         
Répéter
    Afficher " "
    Répéter
        Afficher "pierre, papier ou ciseau?"
        Saisir texte
    Jusqu'à texte="pierre" ou texte="papier" ou texte="ciseau"
   
    si texte = "pierre" alors
        choix <- 0
    finsi
    si texte = "papier" alors
        choix <- 1
    finsi
    si texte = "ciseau" alors
        choix <- 2
    finsi

    Afficher " "
    Afficher "joueur1: ", texte       
            
    nb <- aléatoire(2)
            
    si nb = 0 alors
        choix2 <- "pierre"
    finsi
    si nb = 1 alors
        choix2 <- "papier"
    finsi    
    si nb = 2 alors
        choix2 <- "ciseau"
    finsi   
   
    Afficher "joueur2: ", choix2
    Afficher " "
    
    si choix=0 et nb=0 alors
        Afficher "Partie Nulle."
    sinon
        si choix=0 et nb=1 alors
            Afficher "Le papier recouvre la pierre."
            scor2 <- scor2 + 1
        sinon
            si choix=0 et nb=2 alors
                Afficher "La pierre casse les ciseaux."
                scor <- scor + 1
            finsi
        finsi
    finsi
   
    si choix=1 et nb=0 alors
        Afficher "Le papier enveloppe la pierre."
        scor <- scor + 1
    sinon
        si choix=1 et nb=1 alors
            Afficher "Partie Nulle."
        sinon
            si choix=1 et nb=2 alors
                Afficher "Le ciseau coupe le papier."
                scor2 <- scor2 + 1
            finsi
        finsi
    finsi
   
    si choix=2 et nb=0 alors
        Afficher "La pierre casse le ciseau."
        scor2 <- scor2 + 1
    sinon
        si choix=2 et nb=1 alors
            Afficher "Le ciseau coupe le papier."
            scor <- scor + 1   
        sinon
            si choix=2 et nb=2 alors
                Afficher "Partie Nulle."
            finsi
        finsi
    finsi

    Afficher " "
    Afficher "Les scores sont :"
    Afficher "Joueur1 : ", scor
    Afficher "Joueur2 : ", scor2
    Afficher " "
    
    Répéter
        Afficher "Encore une partie?(O/N)"
        Saisir texte
        texte <- agrandir(texte)
    Jusqu'à texte="N" ou texte="O"
               
jusqu'à texte="N"

FIN


/


using System;

namespace Jaken
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			Console.WriteLine(" "); //Annonce du début
			Console.WriteLine("Jeu de Jaken"); // rock scissor paper
			Console.WriteLine("Licence publique générale GNU"); // http://www.gnu.org/licenses/gpl.html

			//Constantes et Variables //variable locale non assignée
			
			string texte1; 
			string texte2="rien"; 
			
			bool exit = false;
			
			int choix1 = 0; // choix du joueur 1 ( 0, 1, 2 )
			int choix2 = 0; // choix du joueur 2 ( 0, 1, 2 )
			
			
			int nbtour = 1; // Le premier tour commence
			int Maxtab = 531; // Taille maximal du tableau
			
			int[] Tchoix = new int[Maxtab];
			int[] Tetat = new int[Maxtab]; //perdu(0), nul(1), gagne(2)
			

			//DEBUT
			do
			{
				int scor1 = 0; // Le score du joueur 1
				int scor2 = 0; // Le score du joueur 2
				Console.WriteLine(" ");
				Console.WriteLine("----------");
				Console.WriteLine("(F) - Mode facile");
				Console.WriteLine("(M) - Mode moyen");
				Console.WriteLine("(D) - Mode difficile");
				Console.WriteLine("(S) - Sortir"); 
				Console.WriteLine("----------");
				texte1 = Console.ReadLine();
				texte1 = texte1.ToUpper();
				Console.WriteLine("----------");
				Console.WriteLine(" ");
				switch (texte1)
				{
					case "S": // Sortie
						break;
                    
					case "F": // Humain contre Machine Random
						exit = false;
						nbtour=1;
						do //Début de la boucle
						{	Console.WriteLine(" ");
							Console.WriteLine("--- mode facile, tour n°" + nbtour + " ---");
							choix_humain(ref texte2, ref exit);
							if (texte1 != "quitter")
							{	convert_to_nb(texte2, ref choix1);
								choix_random(ref choix2);
								convert_to_string(choix2);
								gagnants_et_résultats(choix1, choix2, ref scor1, ref scor2, ref Tetat, nbtour);
								nbtour = nbtour + 1;
							}
						} while (exit != true); //Fin de la boucle
						break;

					case "M": // Humain contre Méthode Minasi
						exit = false;
						nbtour=1;
						do //Début de la boucle
						{	Console.WriteLine(" ");
							Console.WriteLine("--- mode moyen, tour n°" + nbtour + " ----");
							choix_humain(ref texte2, ref exit);
							if (texte2 != "quitter")
							{	convert_to_nb(texte2, ref choix1);
								algorithme_de_Minasi(ref choix2, nbtour, choix1, Tchoix);
								gagnants_et_résultats(choix1, choix2, ref scor1, ref scor2, ref Tetat, nbtour);
								nbtour = nbtour + 1;
							}
						} while (exit != true); //Fin de la boucle
						break;

					case "D": // Humain contre Algo de Shannon
						int[] Tstat = new int[29]; //27 + 1 vide + 1 nul
						exit = false;
						nbtour=1;
						do //Début de la boucle
						{	Console.WriteLine(" ");
							Console.WriteLine("--- mode difficile, tour n°" + nbtour + " ---");
							choix_humain(ref texte2, ref exit);
							if (texte2 != "quitter")
							{	convert_to_nb(texte2, ref choix1);
								algorithme_de_Shannon(ref choix2, nbtour, choix1, Tstat, Tchoix, Tetat);
								
								gagnants_et_résultats(choix1, choix2, ref scor1, ref scor2, ref Tetat, nbtour);
								nbtour = nbtour + 1;
							}
						} while (exit != true); //Fin de la boucle
						break;
				}
			} while (texte1 != "S"); //texte1 != "0" && texte1 != "à"
			//FIN
		}

		static void choix_humain(ref string texte2, ref bool exit)
		{	Console.WriteLine(" ");
			do
			{
				Console.WriteLine("pierre, papier, ciseau ou quitter?");
				texte2 = Console.ReadLine();
				texte2 = texte2.ToLower();
			} while (texte2 != "pierre" && texte2 != "papier" && texte2 != "ciseau" && texte2 != "quitter");
			
			if (texte2 == "quitter" )
			{ exit = true; }
		}
		
		static void convert_to_nb(string texte, ref int choix1)
		{	if (texte == "pierre" )
			{ choix1 = 0; }

			if (texte == "papier")
			{ choix1 = 1; }

			if (texte == "ciseau")
			{ choix1 = 2; }

			Console.WriteLine(" ");
			Console.WriteLine("joueur1: " + texte);
		}

		static void choix_random(ref int choix2)
		{	Random r = new Random(); //Aléatoire : jeu de hasard pur
			choix2 = r.Next(2);
		}
		
		static void convert_to_string(int choix2)
		{	string texte2 = "pierre"; // use of unassigned local variable
			if (choix2 == 0)
			{ texte2 = "pierre"; }
			if (choix2 == 1)
			{ texte2 = "papier"; }
			if (choix2 == 2)
			{ texte2 = "ciseau"; }
			Console.WriteLine("joueur2: " + texte2);
		}
			
			
		static void algorithme_de_Minasi(ref int choix2, int nbtour, int choix1, int[] Tchoix)
		{   //Algorithme de Minasi : Stocke les coups joués dans un tableau, recherche le meilleur coups

			int cpt;
			int Maxproba = 0;
			int[] Tstat = new int[3];
			Tstat[0] = Tstat[1] = Tstat[2] = 0;
			int nboccurence = 0;

			Tchoix[nbtour] = choix1;

			if (nbtour > 2)
			{
				for (cpt = 1; cpt <= nbtour - 1; cpt++)
				{
					if (Tchoix[cpt] == Tchoix[nbtour - 1])
					{
						nboccurence = nboccurence + 1;

						if (Tchoix[cpt + 1] == 0) // pierre
						{	Tstat[0] = Tstat[0] + 1;
						}

						if (Tchoix[cpt + 1] == 1) //papier
						{	Tstat[1] = Tstat[1] + 1;
						}

						if (Tchoix[cpt + 1] == 2) //ciseau
						{	Tstat[2] = Tstat[2] + 1;
						}
					}
				}
			}
			else
			{	choix_random(ref choix2);
			}

			if (Tstat[0] > Tstat[1]) // Recherche du maximum
			{
				if (Tstat[0] > Tstat[2])
				{ Maxproba = 0; }
			}
			else
			{
				if (Tstat[1] > Tstat[2])
				{ Maxproba = 1; }
				else
				{ Maxproba = 2; }
			}
			
			Maxproba = Maxproba + 1; // choix Minasi = choix adversaire + 1
			if (nbtour > 2)
			{choix2 = Maxproba ;}
			
			// Choix du joueur 2
			if (choix2 == 3)
			{choix2 = 0;} // Le 3 redevient 0
			
			//Affichage du choix du joueur 2
			convert_to_string(choix2);
		}

		static void algorithme_de_Shannon(ref int choix2, int nbtour, int choix, int[] Tstat, int[] Tchoix, int[] Tetat)
		{   //Algorithme de Shannon : Affecte dans un arbre des probabilités, l'apparition des coups et l'état des parties, séléction du plus probable
			
			int i = 0;
			int Maxstat = 0;
			int Maxstatcontenu = 0;
			int j, k, l ;
			Tchoix[nbtour] = choix;
             
			if (nbtour > 2)
			{   // 3*3*3=27 possibilités par tour
				for (j=0;j<=2;j++)
				{	
					for (k=0;k<=2;k++)
					{
						for (l=0;l<=2;l++)
						{	
							i = i + 1 ;
							if (Tetat[nbtour - 2] == j && Tchoix[nbtour - 1] == k && Tetat[nbtour - 1] == l)
							{	
								Tstat[i] = Tstat[i] + 1;
							}
							Console.WriteLine("Stat n°"+i+" : "+ Tstat[i]+ " (tour "+(nbtour-2)+"="+j+", choix tour-1="+k+", etat précédent="+l+")"); //affiche l'arbre
						}
					}
				}

				for (i = 1; i <= 27; i++) // Recherche du maximum
				{	//Console.WriteLine("Maxstat["+ (i-1) +"] : " + Tstat[i]);
					//Console.WriteLine("Maxstatcontenu : " + Maxstatcontenu);
					if (Tstat[i] > Maxstatcontenu)
					{	Maxstat = i;
						Maxstatcontenu = Tstat[i];
						
					}
				}
				
				// choix Shannon = choix adversaire le plus probable + 1
				if (Maxstat == 1 || Maxstat == 2 || Maxstat == 3 || Maxstat == 10 || Maxstat == 11 || Maxstat == 12 || Maxstat == 19 || Maxstat == 20 || Maxstat == 21)
				{choix2 = 1;}
				if (Maxstat == 4 || Maxstat == 5 || Maxstat == 6 || Maxstat == 13 || Maxstat == 14 || Maxstat == 15 || Maxstat == 22 || Maxstat == 23 || Maxstat == 24)
				{choix2 = 2;}
				if (Maxstat == 7 || Maxstat == 8 || Maxstat == 9 || Maxstat == 16 || Maxstat == 17 || Maxstat == 18 || Maxstat == 25 || Maxstat == 26 || Maxstat == 27)
				{choix2 = 0;}
				
				convert_to_string(choix2);
			}
			else
			{
				choix_random(ref choix2);
				convert_to_string(choix2);
			}
		}

		static void gagnants_et_résultats(int choix1, int choix2, ref int scor1, ref int scor2, ref int[] Tetat, int nbtour)
		{   // Détermination des gagnants et affichage des résultats
            
			Console.WriteLine(" ");
			if (choix1 == 0 && choix2 == 0)
			{
				Console.WriteLine("Partie Nulle.");
				Tetat[nbtour] = 1; //"nul"
			}
			else
			{
				if (choix1 == 0 && choix2 == 1)
				{
					Console.WriteLine("Le papier recouvre la pierre.");
					scor2 = scor2 + 1;
					Tetat[nbtour] = 0; //"perdu"
				}
				else
				{
					if (choix1 == 0 && choix2 == 2)
					{
						Console.WriteLine("La pierre casse les ciseaux.");
						scor1 = scor1 + 1;
						Tetat[nbtour] = 2; //"gagne"
					}
				}
			}
			if (choix1 == 1 && choix2 == 0)
			{
				Console.WriteLine("Le papier enveloppe la pierre.");
				scor1 = scor1 + 1;
				Tetat[nbtour] = 2;
			}
			else
			{
				if (choix1 == 1 && choix2 == 1)
				{
					Console.WriteLine("Partie Nulle.");
					Tetat[nbtour] = 1;
				}
				else
				{
					if (choix1 == 1 && choix2 == 2)
					{
						Console.WriteLine("Le ciseau coupe le papier.");
						scor2 = scor2 + 1;
						Tetat[nbtour] = 0;
					}
				}
			}

			if (choix1 == 2 && choix2 == 0)
			{
				Console.WriteLine("La pierre casse le ciseau.");
				scor2 = scor2 + 1;
				Tetat[nbtour] = 0;
			}
			else
			{
				if (choix1 == 2 && choix2 == 1)
				{
					Console.WriteLine("Le ciseau coupe le papier.");
					scor1 = scor1 + 1;
					Tetat[nbtour] = 2;
				}
				else
				{
					if (choix1 == 2 && choix2 == 2)
					{
						Console.WriteLine("Partie Nulle.");
						Tetat[nbtour] = 1;
					}
				}
			}
			//Affichage des résultats
			Console.WriteLine(" ");
			Console.WriteLine("Les scores sont :");
			Console.WriteLine("Joueur1 : " + scor1);
			Console.WriteLine("Joueur2 : " + scor2);
		}
	}
}
content_copyCOPY

https://codes-sources.commentcamarche.net/source/34692-jeu-de-pierre-feuille-ciseaux-jankenpon