diff --git a/Qwirkle/QwirkleClassLibrary/AddPlayerNotifiedEventArgs.cs b/Qwirkle/QwirkleClassLibrary/AddPlayerNotifiedEventArgs.cs new file mode 100644 index 0000000..1f6abd3 --- /dev/null +++ b/Qwirkle/QwirkleClassLibrary/AddPlayerNotifiedEventArgs.cs @@ -0,0 +1,12 @@ +namespace QwirkleClassLibrary +{ + public class AddPlayerNotifiedEventArgs : EventArgs + { + public string returnedNotified { get; private set; } + + public AddPlayerNotifiedEventArgs(string returnedNotified) + { + this.returnedNotified = returnedNotified; + } + } +} \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index 088cb03..e8a51bc 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -22,27 +22,43 @@ namespace QwirkleClassLibrary public ReadOnlyCollection PlayerList => players.AsReadOnly(); private readonly List players = new(); - public ReadOnlyCollection ScoreList => scores.AsReadOnly(); - private readonly List scores = new(); - public ReadOnlyCollection CellsUsed => cellUsed.AsReadOnly(); private readonly List cellUsed = new(); + public event EventHandler? PlayerAddNotified; + + protected virtual void OnPlayerNotified(AddPlayerNotifiedEventArgs args) + => PlayerAddNotified?.Invoke(this, args); + + public event EventHandler? NextPlayerNotified; + + protected virtual void OnNextPlayer(NextPlayerNotifiedEventArgs args) + => NextPlayerNotified?.Invoke(this, args); + public Game() { bag = CreateTileBag(3); board = CreateBoard(); + } public bool AddPlayerInGame(string? playerTag) { - if (string.IsNullOrWhiteSpace(playerTag) == true || this.GameRunning == true) + if (string.IsNullOrWhiteSpace(playerTag) == true) { + OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The name is null or white space.")); + return false; + } + + if(this.GameRunning == true) + { + OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is running.")); return false; } if (players.Count >= 4) { + OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is full.")); return false; } @@ -50,19 +66,22 @@ namespace QwirkleClassLibrary { if (p.NameTag == playerTag) { + OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : Name alreay taken")); return false; } + } players.Add(CreatePlayer(playerTag)); - scores.Add(new Score(players[players.Count - 1])); - + //scores.Add(new Score(players[players.Count-1])); + OnPlayerNotified(new AddPlayerNotifiedEventArgs("Player was correctly added")); return true; } public Player CreatePlayer(string playerTag) { var player = new Player(playerTag); + return player; } @@ -142,6 +161,7 @@ namespace QwirkleClassLibrary if (GameRunning == true) { players[0].IsPlaying = true; + //OnNextPlayer(new NextPlayerNotifiedEventArgs(players[0])); return players[0].NameTag; } else @@ -162,6 +182,7 @@ namespace QwirkleClassLibrary players[i].IsPlaying = false; players[(i + 1) % players.Count].IsPlaying = true; + OnNextPlayer(new NextPlayerNotifiedEventArgs(players[i])); return players[GetPlayingPlayerPosition()].NameTag; } diff --git a/Qwirkle/QwirkleClassLibrary/NextPlayerNotifiedEventArgs.cs b/Qwirkle/QwirkleClassLibrary/NextPlayerNotifiedEventArgs.cs new file mode 100644 index 0000000..6c0f6aa --- /dev/null +++ b/Qwirkle/QwirkleClassLibrary/NextPlayerNotifiedEventArgs.cs @@ -0,0 +1,12 @@ +namespace QwirkleClassLibrary +{ + public class NextPlayerNotifiedEventArgs : EventArgs + { + public Player player { get; set; } + + public NextPlayerNotifiedEventArgs(Player player) + { + this.player = player; + } + } +} \ No newline at end of file diff --git a/Qwirkle/QwirkleConsoleApp/NotificationClass.cs b/Qwirkle/QwirkleConsoleApp/NotificationClass.cs new file mode 100644 index 0000000..953e9d6 --- /dev/null +++ b/Qwirkle/QwirkleConsoleApp/NotificationClass.cs @@ -0,0 +1,22 @@ +using QwirkleClassLibrary; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace QwirkleConsoleApp +{ + class NotificationClass + { + public void NotificationPlayerAdd(object? sender, AddPlayerNotifiedEventArgs args) + { + Console.WriteLine(args.returnedNotified); + } + + public void NotificationNextPlayer(object? sender, NextPlayerNotifiedEventArgs args) + { + Console.WriteLine(args.player.NameTag + "'s turn"); + } + } +} diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index e76c236..6eb7a4f 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -1,24 +1,23 @@ using QwirkleClassLibrary; +using QwirkleConsoleApp; using System.Net.Quic; using System.Text; using System.Transactions; using static System.Console; + static void AddPlayers(Game game) { + NotificationClass nc = new NotificationClass(); + game.PlayerAddNotified += nc.NotificationPlayerAdd; + while (game.PlayerList.Count < 4) { Write("Enter player tag : "); string? playerTag = ReadLine(); - if (game.AddPlayerInGame(playerTag) == false) - { - WriteLine("ERROR : Player already exist or game is running !"); - } - else - { - WriteLine("Player " + playerTag + " added !"); - } + game.AddPlayerInGame(playerTag); + Write("Do you want to add another player ? (y/n) : "); string? answer = ReadLine(); @@ -38,14 +37,14 @@ static void AddPlayers(Game game) static void ShowTiles(Game game) { WriteLine("\n --------------------- YOUR TILES ------------------------"); - + var currentPlayer = game.GetPlayingPlayer(); var stringBuilder = new StringBuilder(); for (int i = 0; i < currentPlayer.Tiles.Count(); i++) { - stringBuilder.Append("[" + (i+1) + "] "); + stringBuilder.Append("[" + (i + 1) + "] "); stringBuilder.AppendLine(currentPlayer.Tiles[i].ToString()); } @@ -65,7 +64,7 @@ static void AddTile(Game game) } tile = game.TileOfPlayerWithPos(no - 1); - + Write("Enter the x of the cell: "); int x = Convert.ToInt32(ReadLine()); Write("Enter the y of the cell : "); @@ -93,14 +92,14 @@ static void SwapTile(Game game) { var tilesToSwap = new List(); bool continueSwap = true; - + ShowTiles(game); while (continueSwap == true) { Write("Enter the number of the tile you want to swap : "); int no = Convert.ToInt32(ReadLine()); - + if (no is < 1 or > 6) { WriteLine("ERROR : Enter a number between 1 and 6 !"); @@ -109,23 +108,23 @@ static void SwapTile(Game game) { tilesToSwap.Add(game.TileOfPlayerWithPos(no - 1)); } - + Write("Do you want to swap another tile ? (y/n) : "); string? answer = ReadLine(); - + if (answer == "n") { continueSwap = false; } } - + game.SwapTiles(game.GetPlayingPlayer(), tilesToSwap); } static void MenuSwitch(Game game) { int enter = 0; - + while (enter != 3) { ShowBoard(game); @@ -138,7 +137,14 @@ static void MenuSwitch(Game game) WriteLine("[2] Swap your tiles"); WriteLine("[3] Skip your turn"); - enter = Convert.ToInt32(ReadLine()); + try + { + enter = Convert.ToInt32(ReadLine()); + } + catch { + WriteLine("ERROR : You must type (1 / 2 / 3). Please retry : "); + } + switch (enter) { @@ -187,12 +193,14 @@ static void MainMenu(Game game) WriteLine("Game is starting !"); Console.ResetColor(); + NotificationClass nc = new NotificationClass(); + game.NextPlayerNotified += nc.NotificationNextPlayer; + do { - string tagPlayerPlay = game.SetNextPlayer(); + game.SetNextPlayer(); WriteLine(" --------------------- GAME ! ------------------------"); - WriteLine(tagPlayerPlay + "'s turn !"); game.DrawTiles(game.GetPlayingPlayer()); MenuSwitch(game); @@ -208,11 +216,11 @@ static void MainGame() WriteLine(" ===================== WELCOME TO QWIRKLE ! ====================="); WriteLine("Enter the players' nametags (2 to 4 players) : "); Console.ResetColor(); - + AddPlayers(game); game.StartGame(); - + MainMenu(game); }