last modif of branch
continuous-integration/drone/push Build is passing Details

test_old_branch
Jérémy Mouyon 11 months ago
parent 740598a508
commit bdcc6cb138

@ -2,11 +2,11 @@
{ {
public class AddPlayerNotifiedEventArgs : EventArgs public class AddPlayerNotifiedEventArgs : EventArgs
{ {
public int numberBack { get; private set; } public string returnedNotified { get; private set; }
public AddPlayerNotifiedEventArgs(int numberBack) public AddPlayerNotifiedEventArgs(string returnedNotified)
{ {
this.numberBack = numberBack; this.returnedNotified = returnedNotified;
} }
} }
} }

@ -11,7 +11,7 @@ namespace QwirkleClassLibrary
{ {
public class Game : IPlayer, IRules public class Game : IPlayer, IRules
{ {
private readonly TileBag bag; private TileBag bag;
public bool GameRunning { get; private set; } public bool GameRunning { get; private set; }
private Board board; private Board board;
@ -21,28 +21,40 @@ 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; public event EventHandler<AddPlayerNotifiedEventArgs>? PlayerAddNotified;
protected virtual void OnPlayerNotified(AddPlayerNotifiedEventArgs args) protected virtual void OnPlayerNotified(AddPlayerNotifiedEventArgs args)
=> PlayerAddNotified?.Invoke(this, args); => PlayerAddNotified?.Invoke(this, args);
public event EventHandler<NextPlayerNotifiedEventArgs>? NextPlayerNotified;
protected virtual void OnNextPlayer(NextPlayerNotifiedEventArgs args)
=> NextPlayerNotified?.Invoke(this, args);
public Game() public Game()
{ {
bag = new TileBag(3); bag = new TileBag(3);
board = CreateBoard(); board = CreateBoard();
} }
public bool AddPlayerInGame(string? playerTag) 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(1)); OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is running."));
return false; return false;
} }
if (players.Count >= 4) if (players.Count >= 4)
{ {
OnPlayerNotified(new AddPlayerNotifiedEventArgs(2)); OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is full."));
return false; return false;
} }
@ -50,7 +62,7 @@ namespace QwirkleClassLibrary
{ {
if (p.NameTag == playerTag) if (p.NameTag == playerTag)
{ {
OnPlayerNotified(new AddPlayerNotifiedEventArgs(3)); OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : Name alreay taken"));
return false; return false;
} }
@ -58,7 +70,7 @@ namespace QwirkleClassLibrary
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)); OnPlayerNotified(new AddPlayerNotifiedEventArgs("Player was correctly added"));
return true; return true;
} }
@ -117,7 +129,7 @@ namespace QwirkleClassLibrary
{ {
for (int j = 0; j < 6; j++) for (int j = 0; j < 6; j++)
{ {
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count + 1); int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count);
p.AddTileToPlayer(bag.TilesBag[val]); p.AddTileToPlayer(bag.TilesBag[val]);
bag.RemoveTileInBag(bag.TilesBag[val]); bag.RemoveTileInBag(bag.TilesBag[val]);
@ -130,6 +142,7 @@ namespace QwirkleClassLibrary
if (GameRunning == true) if (GameRunning == true)
{ {
players[0].IsPlaying = true; players[0].IsPlaying = true;
//OnNextPlayer(new NextPlayerNotifiedEventArgs(players[0]));
return players[0].NameTag; return players[0].NameTag;
} }
else else
@ -150,6 +163,7 @@ namespace QwirkleClassLibrary
players[i].IsPlaying = false; players[i].IsPlaying = false;
players[(i + 1) % players.Count].IsPlaying = true; players[(i + 1) % players.Count].IsPlaying = true;
OnNextPlayer(new NextPlayerNotifiedEventArgs(players[i]));
return players[GetPlayingPlayerPosition()].NameTag; return players[GetPlayingPlayerPosition()].NameTag;
} }

@ -0,0 +1,12 @@
namespace QwirkleClassLibrary
{
public class NextPlayerNotifiedEventArgs : EventArgs
{
public Player player { get; set; }
public NextPlayerNotifiedEventArgs(Player player)
{
this.player = player;
}
}
}

@ -9,25 +9,14 @@ namespace QwirkleConsoleApp
{ {
class NotificationClass class NotificationClass
{ {
public void NotificationMessagePlayer(object? sender, AddPlayerNotifiedEventArgs args) public void NotificationPlayerAdd(object? sender, AddPlayerNotifiedEventArgs args)
{ {
int t = args.numberBack; Console.WriteLine(args.returnedNotified);
}
switch (t) public void NotificationNextPlayer(object? sender, NextPlayerNotifiedEventArgs args)
{ {
case 0: Console.WriteLine(args.player.NameTag + "'s turn");
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;
}
} }
} }
} }

@ -9,6 +9,7 @@ using static System.Console;
static void AddPlayers(Game game) static void AddPlayers(Game game)
{ {
NotificationClass nc = new NotificationClass(); NotificationClass nc = new NotificationClass();
game.PlayerAddNotified += nc.NotificationPlayerAdd;
while (game.PlayerList.Count < 4) while (game.PlayerList.Count < 4)
{ {
@ -17,10 +18,6 @@ static void AddPlayers(Game game)
game.AddPlayerInGame(playerTag); game.AddPlayerInGame(playerTag);
game.PlayerAddNotified -= nc.NotificationMessagePlayer;
game.PlayerAddNotified += nc.NotificationMessagePlayer;
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();
@ -130,7 +127,14 @@ static void MenuSwitch(Game game)
WriteLine("[2] Swap your tiles"); WriteLine("[2] Swap your tiles");
WriteLine("[3] Skip your turn"); 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) switch (enter)
{ {
@ -155,12 +159,14 @@ static void MainMenu(Game game)
WriteLine("Game is starting !"); WriteLine("Game is starting !");
Console.ResetColor(); Console.ResetColor();
NotificationClass nc = new NotificationClass();
game.NextPlayerNotified += nc.NotificationNextPlayer;
do do
{ {
string tagPlayerPlay = game.SetNextPlayer(); game.SetNextPlayer();
WriteLine(" --------------------- GAME ! ------------------------"); WriteLine(" --------------------- GAME ! ------------------------");
WriteLine(tagPlayerPlay + "'s turn !");
game.DrawTiles(game.GetPlayingPlayer()); game.DrawTiles(game.GetPlayingPlayer());
MenuSwitch(game); MenuSwitch(game);

Loading…
Cancel
Save