From b2a987a6633b7482afa69750e0d0daca36fab372 Mon Sep 17 00:00:00 2001 From: "jeremy.mouyon" Date: Thu, 16 May 2024 20:10:20 +0200 Subject: [PATCH] ok, add event end of game, add main menu for create game.. WAIT REMY SCOREEEEE PLZZZZZZE --- .../EndOfGameNotifiedEventArgs.cs | 12 ++ Qwirkle/QwirkleClassLibrary/Game.cs | 20 ++- Qwirkle/QwirkleClassLibrary/IRules.cs | 2 +- Qwirkle/QwirkleClassLibrary/TileBag.cs | 6 + .../QwirkleConsoleApp/NotificationClass.cs | 5 + Qwirkle/QwirkleConsoleApp/Program.cs | 121 +++++++++++++++--- 6 files changed, 141 insertions(+), 25 deletions(-) create mode 100644 Qwirkle/QwirkleClassLibrary/EndOfGameNotifiedEventArgs.cs diff --git a/Qwirkle/QwirkleClassLibrary/EndOfGameNotifiedEventArgs.cs b/Qwirkle/QwirkleClassLibrary/EndOfGameNotifiedEventArgs.cs new file mode 100644 index 0000000..992135f --- /dev/null +++ b/Qwirkle/QwirkleClassLibrary/EndOfGameNotifiedEventArgs.cs @@ -0,0 +1,12 @@ +namespace QwirkleClassLibrary +{ + public class EndOfGameNotifiedEventArgs + { + public Player player { get; private set; } + + public EndOfGameNotifiedEventArgs(Player player) + { + this.player = player; + } + } +} \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index be115ad..c3aea8c 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using System.Xml.Linq; using System.Security.Cryptography; using System.Collections; +using System.Collections.Immutable; namespace QwirkleClassLibrary { @@ -41,6 +42,11 @@ namespace QwirkleClassLibrary protected virtual void OnPlaceTile(PlaceTileNotifiedEventArgs args) => PlaceTileNotified?.Invoke(this, args); + public event EventHandler? EndOfGameNotified; + + protected virtual void OnEndOfGame(EndOfGameNotifiedEventArgs args) + => EndOfGameNotified?.Invoke(this, args); + public Game() { bag = CreateTileBag(3); @@ -78,7 +84,10 @@ namespace QwirkleClassLibrary } - players.Add(CreatePlayer(playerTag)); + Player pl = CreatePlayer(playerTag); + + players.Add(pl); + scoreBoard.Add(pl, 0); OnPlayerNotified(new AddPlayerNotifiedEventArgs("Player was correctly added")); return true; } @@ -151,7 +160,7 @@ namespace QwirkleClassLibrary { foreach (var p in players) { - for (int j = 0; j < 6; j++) + for (int j = 0; j < 6; j++) // ici pour test { int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count); @@ -218,6 +227,7 @@ namespace QwirkleClassLibrary { if (bag.TilesBag.Count == 0) { + IsGameOver(player); return false; } @@ -444,8 +454,12 @@ namespace QwirkleClassLibrary return score; } - public bool IsGameOver() + + public bool IsGameOver(Player player) { + OnEndOfGame(new EndOfGameNotifiedEventArgs(player)); + GameRunning = false; + //ScoreBoard.ToImmutableSortedDictionary(); return true; } } diff --git a/Qwirkle/QwirkleClassLibrary/IRules.cs b/Qwirkle/QwirkleClassLibrary/IRules.cs index 5041b74..652c023 100644 --- a/Qwirkle/QwirkleClassLibrary/IRules.cs +++ b/Qwirkle/QwirkleClassLibrary/IRules.cs @@ -18,6 +18,6 @@ namespace QwirkleClassLibrary bool CheckTilesInLine(List cells, Board b, int x, int y); - bool IsGameOver(); + bool IsGameOver(Player player); } } diff --git a/Qwirkle/QwirkleClassLibrary/TileBag.cs b/Qwirkle/QwirkleClassLibrary/TileBag.cs index 01ed06d..230a958 100644 --- a/Qwirkle/QwirkleClassLibrary/TileBag.cs +++ b/Qwirkle/QwirkleClassLibrary/TileBag.cs @@ -30,6 +30,12 @@ namespace QwirkleClassLibrary } } } + + + /* Tile t = new Tile(Shape.Club, Color.Red); + Tile t2 = new Tile(Shape.Shuriken, Color.Red); + tiles.Add(t); + tiles.Add(t2);*/ TilesBag = tiles.AsReadOnly(); } diff --git a/Qwirkle/QwirkleConsoleApp/NotificationClass.cs b/Qwirkle/QwirkleConsoleApp/NotificationClass.cs index 138479d..890eb6f 100644 --- a/Qwirkle/QwirkleConsoleApp/NotificationClass.cs +++ b/Qwirkle/QwirkleConsoleApp/NotificationClass.cs @@ -23,5 +23,10 @@ namespace QwirkleConsoleApp { Console.WriteLine("The tile [" + args.tile.ToString() + "] " + args.reason); } + + public void NotificationEndOfGame(object? sender, EndOfGameNotifiedEventArgs args) + { + Console.WriteLine("This end of game ! The last player is " + args.player.NameTag + " !"); + } } } diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index b33282a..b1fa4cf 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -1,5 +1,8 @@ using QwirkleClassLibrary; using QwirkleConsoleApp; +using System.Collections.Immutable; +using System.Collections.ObjectModel; +using System.Diagnostics.Metrics; using System.Net.Quic; using System.Text; using System.Transactions; @@ -60,21 +63,46 @@ static void AddTile(Game game) Tile? tile = null; Write("Enter the number of the tile you want to place : "); - int no = Convert.ToInt32(ReadLine()); - while (no is < 1 or > 6) + int no = -1; + int x = -1; + int y = -1; + + try { - Write("ERROR : Enter a number between 1 and 6 ! : "); - no = Convert.ToInt32(ReadLine()); + while (no is < 1 or > 6) + { + Write("ERROR : Enter a number between 1 and 6 ! : "); + no = Convert.ToInt32(ReadLine()); + } } + catch + { + WriteLine("ERROR : You must type. Please retry : "); + } + tile = game.TileOfPlayerWithPos(no - 1); Write("Enter the x of the cell: "); - int x = Convert.ToInt32(ReadLine()); - Write("Enter the y of the cell : "); - int y = Convert.ToInt32(ReadLine()); - + try + { + x = Convert.ToInt32(ReadLine()); + } + catch + { + WriteLine("ERROR : You must type. Please retry : "); + } + Write("Enter the y of the cell: "); + try + { + y = Convert.ToInt32(ReadLine()); + } + catch + { + WriteLine("ERROR : You must type. Please retry : "); + } + game.PlaceTile(game.GetPlayingPlayer(), tile, x, y); game.PlaceTileNotified -= nc.NotificationAddTile; } @@ -133,7 +161,8 @@ static void MenuSwitch(Game game) { enter = Convert.ToInt32(ReadLine()); } - catch { + catch + { WriteLine("ERROR : You must type (1 / 2 / 3). Please retry : "); } @@ -150,6 +179,7 @@ static void MenuSwitch(Game game) case 3: WriteLine("Your score on this turn : " + game.GetPlayerScore(game.GetPlayingPlayer(), game.CellsUsed, game.GetBoard())); game.EmptyCellUsed(); + game.DrawTiles(game.GetPlayingPlayer()); return; } } @@ -159,11 +189,11 @@ static void ShowBoard(Game game) { Board board = game.GetBoard(); - for(int i = 0; i pair in g.ScoreBoard) + { + i++; + WriteLine("[" + i + "] " + pair.Key.NameTag + " with " + pair.Value.ToString() + " points."); + } + + +} + static void MainMenu(Game game) { game.GiveTilesToPlayers(); @@ -192,33 +234,70 @@ static void MainMenu(Game game) { NotificationClass nc = new NotificationClass(); game.NextPlayerNotified += nc.NotificationNextPlayer; + game.EndOfGameNotified += nc.NotificationEndOfGame; game.SetNextPlayer(); WriteLine(" --------------------- GAME ! ------------------------"); - game.DrawTiles(game.GetPlayingPlayer()); MenuSwitch(game); game.NextPlayerNotified -= nc.NotificationNextPlayer; - } while (game.IsGameOver()); + } while (game.GameRunning); + + WriteLine(" --------------------- END OF GAME ! ------------------------"); + WriteLine(" --------------------- THE BOARD : --------------------------"); + ShowBoard(game); + WriteLine(" --------------------- THE SCORE BOARD : ---------------------"); + ShowScoreBoard(game); } static void MainGame() { - Game game = new Game(); - + Console.ForegroundColor = ConsoleColor.DarkGray; WriteLine(" ===================== WELCOME TO QWIRKLE ! ====================="); - WriteLine("Enter the players' nametags (2 to 4 players) : "); Console.ResetColor(); - AddPlayers(game); + int enter = 0; + + while (enter != 3) + { + + WriteLine("[1] Create game"); + WriteLine("[2] Show leaderboard"); + WriteLine("[3] Exit"); + Write("Enter your choice : "); + + try + { + enter = Convert.ToInt32(ReadLine()); + } + catch + { + WriteLine("ERROR : You must type (1 / 2 / 3). Please retry : "); + } - game.StartGame(); - MainMenu(game); + switch (enter) + { + case 1: + WriteLine("Enter minimun 2 player and max 4 player !"); + Game game = new Game(); + AddPlayers(game); + game.StartGame(); + MainMenu(game); + break; + case 2: + //ShowLeaderboard(); + break; + case 3: + return; + } + + } } -MainGame(); \ No newline at end of file +MainGame(); +