From 7cec5696dce0a999055df56465ca48bbe042a4db Mon Sep 17 00:00:00 2001 From: Grizouille Date: Fri, 12 Dec 2025 09:10:08 +0100 Subject: [PATCH] Ajout d'une correction possible --- Correction/MiniJeuxFinal.sln | 53 +++++++++++ Correction/MiniJeuxFinal/Games/IAction.cs | 7 ++ Correction/MiniJeuxFinal/Games/IGame.cs | 17 ++++ Correction/MiniJeuxFinal/Games/IPlayer.cs | 17 ++++ .../PierrePapierCiseaux/Actions/IActionPpc.cs | 7 ++ .../Actions/PaperAction.cs | 12 +++ .../Actions/ScissorsAction.cs | 12 +++ .../Actions/StoneAction.cs | 12 +++ .../Factories/IActionPpcFactory.cs | 9 ++ .../Factories/InputActionFactory.cs | 33 +++++++ .../Factories/RandomActionFactory.cs | 17 ++++ .../PierrePapierCiseauxGame.cs | 58 +++++++++++++ .../PierrePapierCiseaux/Players/PlayerPpc.cs | 50 +++++++++++ Correction/MiniJeuxFinal/Games/Trash/Pendu.cs | 87 +++++++++++++++++++ Correction/MiniJeuxFinal/Games/Trash/Ppc.cs | 66 ++++++++++++++ Correction/MiniJeuxFinal/MiniJeuxFinal.csproj | 10 +++ Correction/MiniJeuxFinal/Program.cs | 34 ++++++++ .../MiniJeuxFinal/Wrappers/ConsoleService.cs | 11 +++ Correction/MiniJeuxFinal/Wrappers/IConsole.cs | 9 ++ Correction/ReadMe.md | 3 + .../Actions/PaperActionTest.cs | 33 +++++++ .../Actions/ScissorsActionTest.cs | 33 +++++++ .../Actions/StoneActionTest.cs | 33 +++++++ .../Factories/InputActionFactoryTest.cs | 58 +++++++++++++ .../Factories/RandomActionFactoryTest.cs | 29 +++++++ .../Players/PlayerPpcTest.cs | 73 ++++++++++++++++ .../MiniJeuxFinal.Test.csproj | 32 +++++++ .../Tests/MiniJeuxFinal.Test/PpcTest.cs | 13 +++ 28 files changed, 828 insertions(+) create mode 100644 Correction/MiniJeuxFinal.sln create mode 100644 Correction/MiniJeuxFinal/Games/IAction.cs create mode 100644 Correction/MiniJeuxFinal/Games/IGame.cs create mode 100644 Correction/MiniJeuxFinal/Games/IPlayer.cs create mode 100644 Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/IActionPpc.cs create mode 100644 Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/PaperAction.cs create mode 100644 Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/ScissorsAction.cs create mode 100644 Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/StoneAction.cs create mode 100644 Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/IActionPpcFactory.cs create mode 100644 Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/InputActionFactory.cs create mode 100644 Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/RandomActionFactory.cs create mode 100644 Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/PierrePapierCiseauxGame.cs create mode 100644 Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Players/PlayerPpc.cs create mode 100644 Correction/MiniJeuxFinal/Games/Trash/Pendu.cs create mode 100644 Correction/MiniJeuxFinal/Games/Trash/Ppc.cs create mode 100644 Correction/MiniJeuxFinal/MiniJeuxFinal.csproj create mode 100644 Correction/MiniJeuxFinal/Program.cs create mode 100644 Correction/MiniJeuxFinal/Wrappers/ConsoleService.cs create mode 100644 Correction/MiniJeuxFinal/Wrappers/IConsole.cs create mode 100644 Correction/ReadMe.md create mode 100644 Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/PaperActionTest.cs create mode 100644 Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/ScissorsActionTest.cs create mode 100644 Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/StoneActionTest.cs create mode 100644 Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Factories/InputActionFactoryTest.cs create mode 100644 Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Factories/RandomActionFactoryTest.cs create mode 100644 Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Players/PlayerPpcTest.cs create mode 100644 Correction/Tests/MiniJeuxFinal.Test/MiniJeuxFinal.Test.csproj create mode 100644 Correction/Tests/MiniJeuxFinal.Test/PpcTest.cs diff --git a/Correction/MiniJeuxFinal.sln b/Correction/MiniJeuxFinal.sln new file mode 100644 index 0000000..566338c --- /dev/null +++ b/Correction/MiniJeuxFinal.sln @@ -0,0 +1,53 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniJeux", "MiniJeuxFinal\MiniJeuxFinal.csproj", "{5BA674C8-192C-47C8-8F50-F9F17A762B2C}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniJeuxFinal.Test", "Tests\MiniJeuxFinal.Test\MiniJeuxFinal.Test.csproj", "{09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Debug|x64.ActiveCfg = Debug|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Debug|x64.Build.0 = Debug|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Debug|x86.ActiveCfg = Debug|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Debug|x86.Build.0 = Debug|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Release|Any CPU.Build.0 = Release|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Release|x64.ActiveCfg = Release|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Release|x64.Build.0 = Release|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Release|x86.ActiveCfg = Release|Any CPU + {5BA674C8-192C-47C8-8F50-F9F17A762B2C}.Release|x86.Build.0 = Release|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Debug|x64.ActiveCfg = Debug|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Debug|x64.Build.0 = Debug|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Debug|x86.ActiveCfg = Debug|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Debug|x86.Build.0 = Debug|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Release|Any CPU.Build.0 = Release|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Release|x64.ActiveCfg = Release|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Release|x64.Build.0 = Release|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Release|x86.ActiveCfg = Release|Any CPU + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {09AF8FA9-D62B-45DB-AC87-0BAE32811EAC} = {0AB3BF05-4346-4AA6-1389-037BE0695223} + EndGlobalSection +EndGlobal diff --git a/Correction/MiniJeuxFinal/Games/IAction.cs b/Correction/MiniJeuxFinal/Games/IAction.cs new file mode 100644 index 0000000..c515b2d --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/IAction.cs @@ -0,0 +1,7 @@ +namespace MiniJeuxFinal.Games +{ + public interface IAction + { + string Name { get; } + } +} diff --git a/Correction/MiniJeuxFinal/Games/IGame.cs b/Correction/MiniJeuxFinal/Games/IGame.cs new file mode 100644 index 0000000..a6b2a3b --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/IGame.cs @@ -0,0 +1,17 @@ +namespace MiniJeuxFinal.Games +{ + internal interface IGame + { + event EventHandler<(IPlayer winner, IPlayer[] players)>? GameEnded; + + event EventHandler<(string playerChoice, string computerChoice)>? TurnStarted; + + event EventHandler<(IPlayer? winner, IPlayer[] players)>? TurnEnded; + + + + string Name { get; } + + void Start(); + } +} diff --git a/Correction/MiniJeuxFinal/Games/IPlayer.cs b/Correction/MiniJeuxFinal/Games/IPlayer.cs new file mode 100644 index 0000000..782b62e --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/IPlayer.cs @@ -0,0 +1,17 @@ +namespace MiniJeuxFinal.Games +{ + internal interface IPlayer + { + string Name { get; } + + int Score { get; } + + + + void AddAction(params IAction[] action); + + IAction Play(); + + void IncrementScore(); + } +} diff --git a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/IActionPpc.cs b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/IActionPpc.cs new file mode 100644 index 0000000..524797e --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/IActionPpc.cs @@ -0,0 +1,7 @@ +namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Actions +{ + public interface IActionPpc : IAction + { + bool ToWin(IActionPpc other); + } +} diff --git a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/PaperAction.cs b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/PaperAction.cs new file mode 100644 index 0000000..fca409a --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/PaperAction.cs @@ -0,0 +1,12 @@ +namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Actions +{ + public class PaperAction : IActionPpc + { + public string Name => "Papier"; + + public bool ToWin(IActionPpc other) + { + return other is not ScissorsAction; + } + } +} diff --git a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/ScissorsAction.cs b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/ScissorsAction.cs new file mode 100644 index 0000000..f6a6e92 --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/ScissorsAction.cs @@ -0,0 +1,12 @@ +namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Actions +{ + public class ScissorsAction : IActionPpc + { + public string Name => "Ciseaux"; + + public bool ToWin(IActionPpc other) + { + return other is not StoneAction; + } + } +} diff --git a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/StoneAction.cs b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/StoneAction.cs new file mode 100644 index 0000000..e4a5627 --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Actions/StoneAction.cs @@ -0,0 +1,12 @@ +namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Actions +{ + public class StoneAction : IActionPpc + { + public string Name => "Pierre"; + + public bool ToWin(IActionPpc other) + { + return other is not PaperAction; + } + } +} diff --git a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/IActionPpcFactory.cs b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/IActionPpcFactory.cs new file mode 100644 index 0000000..9637c1b --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/IActionPpcFactory.cs @@ -0,0 +1,9 @@ +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; + +namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Factories +{ + public interface IActionPpcFactory + { + IActionPpc GetAction(IList action); + } +} diff --git a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/InputActionFactory.cs b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/InputActionFactory.cs new file mode 100644 index 0000000..2266563 --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/InputActionFactory.cs @@ -0,0 +1,33 @@ +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; +using MiniJeuxFinal.Wrappers; + +namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Factories +{ + public class InputActionFactory : IActionPpcFactory + { + private readonly IConsole _console; + + public InputActionFactory(IConsole console) + { + _console = console; + } + + + + public IActionPpc GetAction(IList action) + { + _console.WriteLine("Choisis (pierre, papier, ciseaux) : "); + for (int i = 0; i < action.Count; i++) + Console.WriteLine($"{i}: {action[i].Name}"); + + var result = _console.ReadLine(); + if (!int.TryParse(result, out var shootId) || shootId >= action.Count) + { + _console.WriteLine("❌ Choix invalide !"); + return GetAction(action); + } + + return action[shootId]; + } + } +} diff --git a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/RandomActionFactory.cs b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/RandomActionFactory.cs new file mode 100644 index 0000000..9f536c5 --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Factories/RandomActionFactory.cs @@ -0,0 +1,17 @@ +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; + +namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Factories +{ + public class RandomActionFactory : IActionPpcFactory + { + private static Random rnd = new Random(); + + + + public IActionPpc GetAction(IList action) + { + int n = rnd.Next(action.Count); + return action[n]; + } + } +} \ No newline at end of file diff --git a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/PierrePapierCiseauxGame.cs b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/PierrePapierCiseauxGame.cs new file mode 100644 index 0000000..3e8c6de --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/PierrePapierCiseauxGame.cs @@ -0,0 +1,58 @@ +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Factories; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Players; +using MiniJeuxFinal.Wrappers; + +namespace MiniJeuxFinal.Games.PierrePapierCiseaux +{ + internal class PierrePapierCiseauxGame : IGame + { + public string Name => "Jeu du Pierre, Papier, Ciseaux 🧱 📄 ✂️"; + + private readonly int _totalRounds; + private readonly string _playerName; + + public event EventHandler<(IPlayer winner, IPlayer[] players)>? GameEnded; + + public event EventHandler<(IPlayer? winner, IPlayer[] players)>? TurnEnded; + public event EventHandler<(string playerChoice, string computerChoice)>? TurnStarted; + + public PierrePapierCiseauxGame(int totalRound, string playerName) + { + _totalRounds = totalRound; + _playerName = playerName; + } + + + + public void Start() + { + IAction[] lstActions = [new StoneAction(), new PaperAction(), new ScissorsAction()]; + var player = new PlayerPpc(_playerName, new InputActionFactory(new ConsoleService())); + player.AddAction(lstActions); + var computer = new PlayerPpc("Ordi", new RandomActionFactory()); + computer.AddAction(lstActions); + + while (player.Score < _totalRounds && computer.Score < _totalRounds) + { + var actionPLayer = (IActionPpc)player.Play(); + var actionComputer = (IActionPpc)computer.Play(); + TurnStarted?.Invoke(this, (actionPLayer.Name, actionComputer.Name)); + + if (actionPLayer.ToWin(actionComputer)) + player.IncrementScore(); + else if (actionComputer.ToWin(actionPLayer)) + computer.IncrementScore(); + else + { + TurnEnded?.Invoke(this, (null, [player, computer])); + continue; + } + + TurnEnded?.Invoke(this, (actionPLayer.ToWin(actionComputer) ? player : computer, [player, computer])); + } + + GameEnded?.Invoke(this, (player.Score > computer.Score ? player : computer, [player, computer])); + } + } +} diff --git a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Players/PlayerPpc.cs b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Players/PlayerPpc.cs new file mode 100644 index 0000000..19279fd --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/Players/PlayerPpc.cs @@ -0,0 +1,50 @@ +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Factories; + +namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Players +{ + public class PlayerPpc : IPlayer + { + private readonly List _actions = []; + private readonly IActionPpcFactory _actionPpcFactory; + + + + public PlayerPpc(string name, IActionPpcFactory actionPpcFactory) + { + Name = name; + _actionPpcFactory = actionPpcFactory; + } + + + + public string Name { get; } + + public int Score { get; private set; } + + + public void AddAction(params IAction[] action) + { + _actions.AddRange(action.Cast()); + } + + public void IncrementScore() + { + Score++; + } + + public IAction Play() + { + if (!_actions.Any()) + throw new InvalidOperationException("You don't have a action"); + + return _actionPpcFactory.GetAction(_actions); + } + + + public override string ToString() + { + return $"{Name}: {Score}"; + } + } +} diff --git a/Correction/MiniJeuxFinal/Games/Trash/Pendu.cs b/Correction/MiniJeuxFinal/Games/Trash/Pendu.cs new file mode 100644 index 0000000..b6ebb7d --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/Trash/Pendu.cs @@ -0,0 +1,87 @@ +namespace MiniJeuxFinal.Games.Trash; + +public class Pendu +{ + public Pendu() + { + Start(); + } + + + private void Start() + { + string[] mots = { "ordinateur", "programmation", "developpement", "algorithmie", "variable" }; + Random random = new Random(); + string motSecret = mots[random.Next(0, mots.Length)]; + + char[] lettresTrouvees = new char[motSecret.Length]; + for (int i = 0; i < lettresTrouvees.Length; i++) + { + lettresTrouvees[i] = '_'; + } + + int vies = 7; + bool motComplet = false; + + Console.WriteLine("=== JEU DU PENDU ==="); + Console.WriteLine($"Le mot a {motSecret.Length} lettres"); + + while (vies > 0 && !motComplet) + { + // Afficher l'état actuel + Console.WriteLine($"\nVies restantes : {vies}"); + Console.WriteLine("Mot : " + string.Join(" ", lettresTrouvees)); + + // Demander une lettre + Console.Write("Propose une lettre : "); + var infoLettre = Console.ReadLine(); + + var lettre = infoLettre[0]; + + // Vérifier si la lettre est dans le mot + bool lettreTrouvee = false; + for (int i = 0; i < motSecret.Length; i++) + { + if (motSecret[i] == lettre) + { + lettresTrouvees[i] = lettre; + lettreTrouvee = true; + } + } + + if (!lettreTrouvee) + { + vies--; + Console.WriteLine($"❌ La lettre '{lettre}' n'est pas dans le mot."); + } + else + { + Console.WriteLine($"✅ Bonne lettre !"); + } + + // Vérifier si le mot est complet + motComplet = true; + foreach (char c in lettresTrouvees) + { + if (c == '_') + { + motComplet = false; + break; + } + } + } + + // Résultat final + if (motComplet) + { + Console.WriteLine($"\n🎉 FÉLICITATIONS ! Tu as trouvé le mot : {motSecret}"); + } + else + { + Console.WriteLine($"\n💀 PERDU ! Le mot était : {motSecret}"); + } + + Console.WriteLine("\nAppuyez sur une touche pour quitter..."); + Console.ReadLine(); + } +} \ No newline at end of file diff --git a/Correction/MiniJeuxFinal/Games/Trash/Ppc.cs b/Correction/MiniJeuxFinal/Games/Trash/Ppc.cs new file mode 100644 index 0000000..9f3ab91 --- /dev/null +++ b/Correction/MiniJeuxFinal/Games/Trash/Ppc.cs @@ -0,0 +1,66 @@ +namespace MiniJeuxFinal.Games.Trash; + +public class Ppc +{ + private static readonly Dictionary Scores = new Dictionary() + { + {"joueur", 0}, + {"ordi", 0} + }; + + + + public Ppc() + { + Console.WriteLine("Bienvenue dans "); + Console.WriteLine("✊✋✌️ Pierre-Papier-Ciseaux !"); + Console.WriteLine("Premier à 3 points gagne !"); + Start(); + // Résultat final + Console.WriteLine($"\n{(Scores["joueur"] == 3 ? "🎉 TU AS GAGNÉ !" : "😢 L\'ordinateur a gagné...")}"); + Console.WriteLine($"Score final : {Scores["joueur"]} - {Scores["ordi"]}"); + } + + + + private static void Start() + { + var random = new Random(); + var choix_possibles = new[] { "pierre", "papier", "ciseaux" }; + + while (Scores["joueur"] < 3 && Scores["ordi"] < 3) + { + Console.WriteLine($"\nScore : Toi {Scores["joueur"]} - {Scores["ordi"]} Ordinateur"); + Console.WriteLine("Choisis (pierre, papier, ciseaux) : "); + var joueur = Console.ReadLine(); + + if (!choix_possibles.Contains(joueur?.ToLower())) + { + Console.WriteLine("❌ Choix invalide !"); + continue; + } + + + var ordi = random.GetItems(choix_possibles, 1).FirstOrDefault(); + Console.WriteLine($"🤖 L'ordinateur a choisi : {ordi}"); + + // Déterminer le gagnant + if (joueur == ordi) + { + Console.WriteLine("✨ Égalité !"); + } + else if ((joueur == "pierre" && ordi == "ciseaux") || + (joueur == "papier" && ordi == "pierre") || + (joueur == "ciseaux" && ordi == "papier")) + { + Console.WriteLine("✅ Tu gagnes cette manche !"); + Scores["joueur"] += 1; + } + else + { + Console.WriteLine("❌ L'ordinateur gagne cette manche !"); + Scores["ordi"] += 1; + } + } + } +} \ No newline at end of file diff --git a/Correction/MiniJeuxFinal/MiniJeuxFinal.csproj b/Correction/MiniJeuxFinal/MiniJeuxFinal.csproj new file mode 100644 index 0000000..fd4bd08 --- /dev/null +++ b/Correction/MiniJeuxFinal/MiniJeuxFinal.csproj @@ -0,0 +1,10 @@ + + + + Exe + net9.0 + enable + enable + + + diff --git a/Correction/MiniJeuxFinal/Program.cs b/Correction/MiniJeuxFinal/Program.cs new file mode 100644 index 0000000..907ddae --- /dev/null +++ b/Correction/MiniJeuxFinal/Program.cs @@ -0,0 +1,34 @@ +using MiniJeuxFinal.Games.PierrePapierCiseaux; + +class Program() +{ + public static void Main() + { + var game = new PierrePapierCiseauxGame(3, "loic"); + game.TurnStarted += (sender, tuple) => + { + Console.WriteLine($"Tu joues : {tuple.playerChoice}"); + Console.WriteLine($"L’ordinateur joue : {tuple.computerChoice}"); + }; + game.TurnEnded += (sender, result) => + { + Thread.Sleep(2000); + Console.Clear(); + if (result.winner == null) + Console.WriteLine($"Egalitée !"); + else + Console.WriteLine($"{result.winner.Name} gagne la manche"); + Console.WriteLine(); + Console.WriteLine($"Score : {result.players[0]} - {result.players[1]}"); + Console.WriteLine(); + }; + game.GameEnded += (sender, result) => + { + Console.WriteLine($"{result.winner.Name} AS GAGNÉ ! 🎉🎉🎉"); + Console.WriteLine($"Score final : {result.winner.Score} - {result.players.FirstOrDefault(_ => _.Name != result.winner.Name)?.Score}"); + }; + Console.WriteLine(game.Name); + Console.WriteLine(); + game.Start(); + } +} diff --git a/Correction/MiniJeuxFinal/Wrappers/ConsoleService.cs b/Correction/MiniJeuxFinal/Wrappers/ConsoleService.cs new file mode 100644 index 0000000..599b64b --- /dev/null +++ b/Correction/MiniJeuxFinal/Wrappers/ConsoleService.cs @@ -0,0 +1,11 @@ +namespace MiniJeuxFinal.Wrappers +{ + internal class ConsoleService : IConsole + { + public string? ReadLine() => + Console.ReadLine(); + + public void WriteLine(string message) => + Console.WriteLine(message); + } +} diff --git a/Correction/MiniJeuxFinal/Wrappers/IConsole.cs b/Correction/MiniJeuxFinal/Wrappers/IConsole.cs new file mode 100644 index 0000000..c177d08 --- /dev/null +++ b/Correction/MiniJeuxFinal/Wrappers/IConsole.cs @@ -0,0 +1,9 @@ +namespace MiniJeuxFinal.Wrappers +{ + public interface IConsole + { + string? ReadLine(); + + void WriteLine(string message); + } +} diff --git a/Correction/ReadMe.md b/Correction/ReadMe.md new file mode 100644 index 0000000..80bff8f --- /dev/null +++ b/Correction/ReadMe.md @@ -0,0 +1,3 @@ +# Exemple final de se à quoi on pourrait aller + +Cette solution est un aperçu des bonne pratique mise en place. \ No newline at end of file diff --git a/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/PaperActionTest.cs b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/PaperActionTest.cs new file mode 100644 index 0000000..33ec8b7 --- /dev/null +++ b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/PaperActionTest.cs @@ -0,0 +1,33 @@ +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; +using NFluent; + +namespace MiniJeuxFinal.Test.Games.PierrePapierCiseaux.Actions +{ + public class PaperActionTest + { + private PaperAction CreateTarget() => new PaperAction(); + + + + [Fact] + public void Should_init() + { + var target = CreateTarget(); + Check.That(target.Name).IsEqualTo("Papier"); + } + + [Fact] + public void Should_win() + { + var target = CreateTarget(); + Check.That(target.ToWin(new StoneAction())).IsTrue(); + } + + [Fact] + public void Should_loose() + { + var target = CreateTarget(); + Check.That(target.ToWin(new ScissorsAction())).IsFalse(); + } + } +} diff --git a/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/ScissorsActionTest.cs b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/ScissorsActionTest.cs new file mode 100644 index 0000000..09fd2ee --- /dev/null +++ b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/ScissorsActionTest.cs @@ -0,0 +1,33 @@ +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; +using NFluent; + +namespace MiniJeuxFinal.Test.Games.PierrePapierCiseaux.Actions +{ + public class ScissorsActionTest + { + private ScissorsAction CreateTarget() => new ScissorsAction(); + + + + [Fact] + public void Should_init() + { + var target = CreateTarget(); + Check.That(target.Name).IsEqualTo("Ciseaux"); + } + + [Fact] + public void Should_win() + { + var target = CreateTarget(); + Check.That(target.ToWin(new PaperAction())).IsTrue(); + } + + [Fact] + public void Should_loose() + { + var target = CreateTarget(); + Check.That(target.ToWin(new StoneAction())).IsFalse(); + } + } +} diff --git a/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/StoneActionTest.cs b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/StoneActionTest.cs new file mode 100644 index 0000000..4afbca0 --- /dev/null +++ b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Actions/StoneActionTest.cs @@ -0,0 +1,33 @@ +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; +using NFluent; + +namespace MiniJeuxFinal.Test.Games.PierrePapierCiseaux.Actions +{ + public class StoneActionTest + { + private StoneAction CreateTarget() => new StoneAction(); + + + + [Fact] + public void Should_init() + { + var target = CreateTarget(); + Check.That(target.Name).IsEqualTo("Pierre"); + } + + [Fact] + public void Should_win() + { + var target = CreateTarget(); + Check.That(target.ToWin(new ScissorsAction())).IsTrue(); + } + + [Fact] + public void Should_loose() + { + var target = CreateTarget(); + Check.That(target.ToWin(new PaperAction())).IsFalse(); + } + } +} diff --git a/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Factories/InputActionFactoryTest.cs b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Factories/InputActionFactoryTest.cs new file mode 100644 index 0000000..fe294eb --- /dev/null +++ b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Factories/InputActionFactoryTest.cs @@ -0,0 +1,58 @@ +using FakeItEasy; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Factories; +using MiniJeuxFinal.Wrappers; +using NFluent; + +namespace MiniJeuxFinal.Test.Games.PierrePapierCiseaux.Factories +{ + public class InputActionFactoryTest + { + private readonly IConsole _console = A.Fake(); + + + + private InputActionFactory CreateTarget() => + new InputActionFactory(_console); + + + + [Theory] + [InlineData("0")] + [InlineData("1")] + [InlineData("2")] + public void Should_get_selected_action(string selected) + { + var target = CreateTarget(); + A.CallTo(() => _console.ReadLine()).Returns(selected); + var actions = new List + { + A.Fake(), + A.Fake(), + A.Fake() + }; + + var result = target.GetAction(actions); + Check.That(actions.Contains(result)).IsTrue(); + Check.That(result).IsEqualTo(actions[int.Parse(selected)]); + } + + [Fact] + public void Should_show_error_when_invalid_input() + { + var target = CreateTarget(); + A.CallTo(() => _console.ReadLine()).ReturnsNextFromSequence("3", "0"); + var actions = new List + { + A.Fake(), + A.Fake(), + A.Fake() + }; + + var result = target.GetAction(actions); + Check.That(actions.Contains(result)).IsTrue(); + Check.That(result).IsEqualTo(actions[int.Parse("0")]); + A.CallTo(() => _console.WriteLine("❌ Choix invalide !")).MustHaveHappenedOnceExactly(); + } + } +} diff --git a/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Factories/RandomActionFactoryTest.cs b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Factories/RandomActionFactoryTest.cs new file mode 100644 index 0000000..72d1da1 --- /dev/null +++ b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Factories/RandomActionFactoryTest.cs @@ -0,0 +1,29 @@ +using FakeItEasy; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Factories; +using NFluent; + +namespace MiniJeuxFinal.Test.Games.PierrePapierCiseaux.Factories +{ + public class RandomActionFactoryTest + { + private RandomActionFactory CreateTarget() => new RandomActionFactory(); + + + + [Theory] + [InlineData(2)] + [InlineData(5)] + [InlineData(3)] + [InlineData(10)] + public void Should_get_random_action(int actionNumber) + { + var target = CreateTarget(); + var actions = new List(); + for (int i = 0; i < actionNumber; i++) + actions.Add(A.Fake()); + + Check.That(actions.Contains(target.GetAction(actions))).IsTrue(); + } + } +} \ No newline at end of file diff --git a/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Players/PlayerPpcTest.cs b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Players/PlayerPpcTest.cs new file mode 100644 index 0000000..29fa0ea --- /dev/null +++ b/Correction/Tests/MiniJeuxFinal.Test/Games/PierrePapierCiseaux/Players/PlayerPpcTest.cs @@ -0,0 +1,73 @@ +using FakeItEasy; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Factories; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Players; +using NFluent; + +namespace MiniJeuxFinal.Test.Games.PierrePapierCiseaux.Players +{ + public class PlayerPpcTest + { + private readonly IActionPpcFactory _actionPpcFactory = A.Fake(); + + + + private PlayerPpc CreateTarget() => new PlayerPpc("Paul", _actionPpcFactory); + + + [Fact] + public void Should_initialize_target() + { + var player = CreateTarget(); + + Check.That(player.Name).IsEqualTo("Paul"); + Check.That(player.ToString()).IsEqualTo("Paul: 0"); + Check.That(player.Score).IsEqualTo(0); + } + + [Fact] + public void Should_increment_score() + { + var player = CreateTarget(); + Check.That(player.Score).IsEqualTo(0); + player.IncrementScore(); + Check.That(player.Score).IsEqualTo(1); + player.IncrementScore(); + player.IncrementScore(); + Check.That(player.Score).IsEqualTo(3); + } + + [Fact] + public void Should_add_a_single_action_and_get() + { + var player = CreateTarget(); + var action1 = A.Fake(); + A.CallTo(() => _actionPpcFactory.GetAction(A>._)).Returns(action1); + player.AddAction(action1); + var result = player.Play(); + + Check.That(result).IsEqualTo(action1); + } + + [Fact] + public void Should_add_a_multiple_actions_and_get() + { + var player = CreateTarget(); + var action1 = A.Fake(); + var action2 = A.Fake(); + var action3 = A.Fake(); + A.CallTo(() => _actionPpcFactory.GetAction(A>._)).Returns(action2); + player.AddAction(action1, action2, action3); + var result = player.Play(); + + Check.That(result).IsEqualTo(action2); + } + + [Fact] + public void Should_throw_when_has_no_action() + { + var player = CreateTarget(); + Check.ThatCode(() => player.Play()).Throws(); + } + } +} diff --git a/Correction/Tests/MiniJeuxFinal.Test/MiniJeuxFinal.Test.csproj b/Correction/Tests/MiniJeuxFinal.Test/MiniJeuxFinal.Test.csproj new file mode 100644 index 0000000..b336b6e --- /dev/null +++ b/Correction/Tests/MiniJeuxFinal.Test/MiniJeuxFinal.Test.csproj @@ -0,0 +1,32 @@ + + + + net9.0 + enable + enable + false + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + diff --git a/Correction/Tests/MiniJeuxFinal.Test/PpcTest.cs b/Correction/Tests/MiniJeuxFinal.Test/PpcTest.cs new file mode 100644 index 0000000..be21724 --- /dev/null +++ b/Correction/Tests/MiniJeuxFinal.Test/PpcTest.cs @@ -0,0 +1,13 @@ +using NFluent; + +namespace MiniJeuxFinal.Test; + +public class PpcTest +{ + [Fact] + public void Test1() + { + var i = 1; + Check.That(i).IsEqualTo(1); + } +}