add event check ;) (need trad)

test_old_branch
Jérémy Mouyon 11 months ago
parent b5914da032
commit 8e829039a7

@ -35,6 +35,11 @@ namespace QwirkleClassLibrary
protected virtual void OnNextPlayer(NextPlayerNotifiedEventArgs args)
=> NextPlayerNotified?.Invoke(this, args);
public event EventHandler<PlaceTileNotifiedEventArgs>? PlaceTileNotified;
protected virtual void OnPlaceTile(PlaceTileNotifiedEventArgs args)
=> PlaceTileNotified?.Invoke(this, args);
public Game()
{
bag = CreateTileBag(3);
@ -264,11 +269,13 @@ namespace QwirkleClassLibrary
if (extendedCell.GetTile.GetColor != tile.GetColor && extendedCell.GetTile.GetShape != tile.GetShape)
{
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Les couleurs/formes adjacentes ne correspondes pas"));
return false;
}
if (i == 6)
{
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "La ligne ou colonne fait deja 6 tuiles !"));
return false;
}
}
@ -304,8 +311,13 @@ namespace QwirkleClassLibrary
{
if (!b.HasOccupiedCase())
{
if (this.PlaceTile(this.GetPlayingPlayer(), t, x, y) == true)
{
this.AddCellUsed(this.GetBoard().GetCell(x, y));
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctement placé"));
return true;
}
}
var surroundingCells = new List<Cell?>
{
@ -324,6 +336,7 @@ namespace QwirkleClassLibrary
if (cell.GetTile.GetColor != t.GetColor && cell.GetTile.GetShape != t.GetShape)
{
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile ne correspond pas au bonne couleur des tiles a coté"));
return false;
}
@ -336,7 +349,18 @@ namespace QwirkleClassLibrary
}
}
return CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null);
if (CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null))
{
if(this.PlaceTile(this.GetPlayingPlayer(), t, x, y) == true)
{
this.AddCellUsed(this.GetBoard().GetCell(x, y));
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctement placé"));
return true;
}
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Cell Already use"));
}
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "La tuile n'a pu etre placé"));
return false;
}
public int GetPlayerScore(Player player, List<Cell> cellsPlayed, Board board)

@ -2,7 +2,7 @@
{
public class NextPlayerNotifiedEventArgs : EventArgs
{
public Player player { get; set; }
public Player player { get; private set; }
public NextPlayerNotifiedEventArgs(Player player)
{

@ -0,0 +1,15 @@
namespace QwirkleClassLibrary
{
public class PlaceTileNotifiedEventArgs : EventArgs
{
public Tile tile { get; private set; }
public string reason { get; private set; }
public PlaceTileNotifiedEventArgs(Tile tile, string reason)
{
this.tile = tile;
this.reason = reason;
}
}
}

@ -18,5 +18,10 @@ namespace QwirkleConsoleApp
{
Console.WriteLine(args.player.NameTag + "'s turn");
}
public void NotificationAddTile(object? sender, PlaceTileNotifiedEventArgs args)
{
Console.WriteLine("The tile [" + args.tile.ToString() + "] " + args.reason);
}
}
}

@ -53,6 +53,11 @@ static void ShowTiles(Game game)
static void AddTile(Game game)
{
NotificationClass nc = new NotificationClass();
game.PlaceTileNotified += nc.NotificationAddTile;
Tile? tile = null;
Write("Enter the number of the tile you want to place : ");
int no = Convert.ToInt32(ReadLine());
@ -70,22 +75,16 @@ static void AddTile(Game game)
Write("Enter the y of the cell : ");
int y = Convert.ToInt32(ReadLine());
if (game.IsMoveCorrect(tile, x, y, game.GetBoard()) == true)
/* if (game.IsMoveCorrect(tile, x, y, game.GetBoard()) == true)
{
if (game.PlaceTile(game.GetPlayingPlayer(), tile, x, y) == true)
{
WriteLine("ok ! your tile is placed");
game.AddCellUsed(game.GetBoard().GetCell(x, y));
}
else
{
WriteLine("ERROR : Cell already used");
}
}
else
{
WriteLine("ERROR : Move not correct");
}
}*/
game.IsMoveCorrect(tile, x, y, game.GetBoard());
game.PlaceTileNotified -= nc.NotificationAddTile;
}
static void SwapTile(Game game)

Loading…
Cancel
Save