add event (just problem with first notification)
continuous-integration/drone/push Build is passing Details

test_old_branch
Jérémy Mouyon 12 months ago
parent 64c950a5b4
commit 740598a508

@ -0,0 +1,12 @@
namespace QwirkleClassLibrary
{
public class AddPlayerNotifiedEventArgs : EventArgs
{
public int numberBack { get; private set; }
public AddPlayerNotifiedEventArgs(int numberBack)
{
this.numberBack = numberBack;
}
}
}

@ -21,6 +21,11 @@ namespace QwirkleClassLibrary
public ReadOnlyCollection<Score> ScoreList => scores.AsReadOnly(); public ReadOnlyCollection<Score> ScoreList => scores.AsReadOnly();
private readonly List<Score> scores = new(); private readonly List<Score> scores = new();
public event EventHandler<AddPlayerNotifiedEventArgs> PlayerAddNotified;
protected virtual void OnPlayerNotified(AddPlayerNotifiedEventArgs args)
=> PlayerAddNotified?.Invoke(this, args);
public Game() public Game()
{ {
bag = new TileBag(3); bag = new TileBag(3);
@ -31,11 +36,13 @@ namespace QwirkleClassLibrary
{ {
if (string.IsNullOrWhiteSpace(playerTag) == true || this.GameRunning == true) if (string.IsNullOrWhiteSpace(playerTag) == true || this.GameRunning == true)
{ {
OnPlayerNotified(new AddPlayerNotifiedEventArgs(1));
return false; return false;
} }
if (players.Count >= 4) if (players.Count >= 4)
{ {
OnPlayerNotified(new AddPlayerNotifiedEventArgs(2));
return false; return false;
} }
@ -43,19 +50,22 @@ namespace QwirkleClassLibrary
{ {
if (p.NameTag == playerTag) if (p.NameTag == playerTag)
{ {
OnPlayerNotified(new AddPlayerNotifiedEventArgs(3));
return false; return false;
} }
} }
players.Add(CreatePlayer(playerTag)); players.Add(CreatePlayer(playerTag));
scores.Add(new Score(players[players.Count-1])); scores.Add(new Score(players[players.Count-1]));
OnPlayerNotified(new AddPlayerNotifiedEventArgs(0));
return true; return true;
} }
public Player CreatePlayer(string playerTag) public Player CreatePlayer(string playerTag)
{ {
var player = new Player(playerTag); var player = new Player(playerTag);
return player; return player;
} }

@ -0,0 +1,33 @@
using QwirkleClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QwirkleConsoleApp
{
class NotificationClass
{
public void NotificationMessagePlayer(object? sender, AddPlayerNotifiedEventArgs args)
{
int t = args.numberBack;
switch (t)
{
case 0:
Console.WriteLine("Player as correctly add");
break;
case 1:
Console.WriteLine("ERROR : The name is null or white space");
break;
case 2:
Console.WriteLine("ERROR : The game is full");
break;
case 3:
Console.WriteLine("ERROR : Name exist");
break;
}
}
}
}

@ -1,24 +1,26 @@
using QwirkleClassLibrary; using QwirkleClassLibrary;
using QwirkleConsoleApp;
using System.Net.Quic; using System.Net.Quic;
using System.Text; using System.Text;
using System.Transactions; using System.Transactions;
using static System.Console; using static System.Console;
static void AddPlayers(Game game) static void AddPlayers(Game game)
{ {
NotificationClass nc = new NotificationClass();
while (game.PlayerList.Count < 4) while (game.PlayerList.Count < 4)
{ {
Write("Enter player tag : "); Write("Enter player tag : ");
string? playerTag = ReadLine(); string? playerTag = ReadLine();
if (game.AddPlayerInGame(playerTag) == false) game.AddPlayerInGame(playerTag);
{
WriteLine("ERROR : Player already exist or game is running !"); game.PlayerAddNotified -= nc.NotificationMessagePlayer;
}
else game.PlayerAddNotified += nc.NotificationMessagePlayer;
{
WriteLine("Player " + playerTag + " added !");
}
Write("Do you want to add another player ? (y/n) : "); Write("Do you want to add another player ? (y/n) : ");
string? answer = ReadLine(); string? answer = ReadLine();
@ -38,14 +40,14 @@ static void AddPlayers(Game game)
static void ShowTiles(Game game) static void ShowTiles(Game game)
{ {
WriteLine("\n --------------------- YOUR TILES ------------------------"); WriteLine("\n --------------------- YOUR TILES ------------------------");
var currentPlayer = game.GetPlayingPlayer(); var currentPlayer = game.GetPlayingPlayer();
var stringBuilder = new StringBuilder(); var stringBuilder = new StringBuilder();
for (int i = 0; i < currentPlayer.Tiles.Count(); i++) for (int i = 0; i < currentPlayer.Tiles.Count(); i++)
{ {
stringBuilder.Append("[" + (i+1) + "] "); stringBuilder.Append("[" + (i + 1) + "] ");
stringBuilder.AppendLine(currentPlayer.Tiles[i].ToString()); stringBuilder.AppendLine(currentPlayer.Tiles[i].ToString());
} }
@ -65,12 +67,12 @@ static void AddTile(Game game)
} }
tile = game.TileOfPlayerWithPos(no - 1); tile = game.TileOfPlayerWithPos(no - 1);
Write("Enter the x of the cell: "); Write("Enter the x of the cell: ");
int x = Convert.ToInt32(ReadLine()); int x = Convert.ToInt32(ReadLine());
Write("Enter the y of the cell : "); Write("Enter the y of the cell : ");
int y = Convert.ToInt32(ReadLine()); int y = Convert.ToInt32(ReadLine());
if (game.PlaceTile(game.GetPlayingPlayer(), tile, x, y) == true) if (game.PlaceTile(game.GetPlayingPlayer(), tile, x, y) == true)
{ {
WriteLine("ok ! your tile is placed"); WriteLine("ok ! your tile is placed");
@ -85,14 +87,14 @@ static void SwapTile(Game game)
{ {
var tilesToSwap = new List<Tile>(); var tilesToSwap = new List<Tile>();
bool continueSwap = true; bool continueSwap = true;
ShowTiles(game); ShowTiles(game);
while (continueSwap == true) while (continueSwap == true)
{ {
Write("Enter the number of the tile you want to swap : "); Write("Enter the number of the tile you want to swap : ");
int no = Convert.ToInt32(ReadLine()); int no = Convert.ToInt32(ReadLine());
if (no is < 1 or > 6) if (no is < 1 or > 6)
{ {
WriteLine("ERROR : Enter a number between 1 and 6 !"); WriteLine("ERROR : Enter a number between 1 and 6 !");
@ -101,23 +103,23 @@ static void SwapTile(Game game)
{ {
tilesToSwap.Add(game.TileOfPlayerWithPos(no - 1)); tilesToSwap.Add(game.TileOfPlayerWithPos(no - 1));
} }
Write("Do you want to swap another tile ? (y/n) : "); Write("Do you want to swap another tile ? (y/n) : ");
string? answer = ReadLine(); string? answer = ReadLine();
if (answer == "n") if (answer == "n")
{ {
continueSwap = false; continueSwap = false;
} }
} }
game.SwapTiles(game.GetPlayingPlayer(), tilesToSwap); game.SwapTiles(game.GetPlayingPlayer(), tilesToSwap);
} }
static void MenuSwitch(Game game) static void MenuSwitch(Game game)
{ {
int enter = 0; int enter = 0;
while (enter != 3) while (enter != 3)
{ {
ShowTiles(game); ShowTiles(game);
@ -152,11 +154,11 @@ static void MainMenu(Game game)
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
WriteLine("Game is starting !"); WriteLine("Game is starting !");
Console.ResetColor(); Console.ResetColor();
do do
{ {
string tagPlayerPlay = game.SetNextPlayer(); string tagPlayerPlay = game.SetNextPlayer();
WriteLine(" --------------------- GAME ! ------------------------"); WriteLine(" --------------------- GAME ! ------------------------");
WriteLine(tagPlayerPlay + "'s turn !"); WriteLine(tagPlayerPlay + "'s turn !");
@ -169,16 +171,16 @@ static void MainMenu(Game game)
static void MainGame() static void MainGame()
{ {
Game game = new Game(); Game game = new Game();
Console.ForegroundColor = ConsoleColor.DarkGreen; Console.ForegroundColor = ConsoleColor.DarkGreen;
WriteLine(" ===================== WELCOME TO QWIRKLE ! ====================="); WriteLine(" ===================== WELCOME TO QWIRKLE ! =====================");
WriteLine("Enter the players' nametags (2 to 4 players) : "); WriteLine("Enter the players' nametags (2 to 4 players) : ");
Console.ResetColor(); Console.ResetColor();
AddPlayers(game); AddPlayers(game);
game.StartGame(); game.StartGame();
MainMenu(game); MainMenu(game);
} }

Loading…
Cancel
Save