|
|
|
@ -16,7 +16,7 @@ namespace QwirkleClassLibrary
|
|
|
|
|
{
|
|
|
|
|
public ReadOnlyDictionary<Player, int> ScoreBoard => scoreBoard.AsReadOnly();
|
|
|
|
|
private readonly Dictionary<Player, int> scoreBoard = new();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private TileBag bag;
|
|
|
|
|
public bool GameRunning { get; private set; }
|
|
|
|
|
private Board board;
|
|
|
|
@ -62,7 +62,7 @@ namespace QwirkleClassLibrary
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(this.GameRunning)
|
|
|
|
|
if (this.GameRunning)
|
|
|
|
|
{
|
|
|
|
|
OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is running."));
|
|
|
|
|
return false;
|
|
|
|
@ -106,7 +106,7 @@ namespace QwirkleClassLibrary
|
|
|
|
|
board = new Board(8, 8);
|
|
|
|
|
return board;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TileBag CreateTileBag(int nbSet)
|
|
|
|
|
{
|
|
|
|
|
bag = new TileBag(nbSet);
|
|
|
|
@ -119,20 +119,20 @@ namespace QwirkleClassLibrary
|
|
|
|
|
this.GameRunning = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void AddCellUsed(Cell? c)
|
|
|
|
|
{
|
|
|
|
|
if (c != null) cellUsed.Add(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void EmptyCellUsed()
|
|
|
|
|
{
|
|
|
|
|
cellUsed.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Player GetPlayingPlayer()
|
|
|
|
|
{
|
|
|
|
|
if(GetPlayingPlayerPosition() == -1)
|
|
|
|
|
if (GetPlayingPlayerPosition() == -1)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("No player play.");
|
|
|
|
|
}
|
|
|
|
@ -160,7 +160,7 @@ namespace QwirkleClassLibrary
|
|
|
|
|
{
|
|
|
|
|
foreach (var p in players)
|
|
|
|
|
{
|
|
|
|
|
for (int j = 0; j < 6; j++)
|
|
|
|
|
for (int j = 0; j < 1; j++) // 6
|
|
|
|
|
{
|
|
|
|
|
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count);
|
|
|
|
|
|
|
|
|
@ -214,8 +214,8 @@ namespace QwirkleClassLibrary
|
|
|
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Cell already used"));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Allows a player to draw tiles from the bag as soon as he has less than 6 tiles
|
|
|
|
|
/// </summary>
|
|
|
|
@ -223,20 +223,19 @@ namespace QwirkleClassLibrary
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool DrawTiles(Player player)
|
|
|
|
|
{
|
|
|
|
|
while(player.Tiles.Count < 6)
|
|
|
|
|
while (player.Tiles.Count < 6)
|
|
|
|
|
{
|
|
|
|
|
if (bag.TilesBag.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
IsGameOver(player);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count + 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
player.AddTileToPlayer(bag.TilesBag[val]);
|
|
|
|
|
bag.RemoveTileInBag(bag.TilesBag[val]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -284,8 +283,8 @@ namespace QwirkleClassLibrary
|
|
|
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Color / Shape does not match with the surrounding tiles !"));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(extendedCell.GetTile.GetColor == tile.GetColor && extendedCell.GetTile.GetShape == tile.GetShape)
|
|
|
|
|
|
|
|
|
|
if (extendedCell.GetTile.GetColor == tile.GetColor && extendedCell.GetTile.GetShape == tile.GetShape)
|
|
|
|
|
{
|
|
|
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Tile already placed on the same line / column !"));
|
|
|
|
|
return false;
|
|
|
|
@ -303,15 +302,15 @@ namespace QwirkleClassLibrary
|
|
|
|
|
|
|
|
|
|
public bool CheckTilesInLine(List<Cell> cells, Board b, int x, int y)
|
|
|
|
|
{
|
|
|
|
|
if(cells.Count == 0)
|
|
|
|
|
if (cells.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var x1 = cells[0].GetX;
|
|
|
|
|
var y1 = cells[0].GetY;
|
|
|
|
|
|
|
|
|
|
if(cells.Count < 2 && (x1 == x || y1 == y))
|
|
|
|
|
|
|
|
|
|
if (cells.Count < 2 && (x1 == x || y1 == y))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
@ -320,7 +319,7 @@ namespace QwirkleClassLibrary
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var x2 = cells[1].GetX;
|
|
|
|
|
var y2 = cells[1].GetY;
|
|
|
|
|
|
|
|
|
@ -332,7 +331,7 @@ namespace QwirkleClassLibrary
|
|
|
|
|
{
|
|
|
|
|
return y == y1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -385,7 +384,7 @@ namespace QwirkleClassLibrary
|
|
|
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "isn't on the same line as the ones previously placed !"));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return surroundingCells.Any(cell => cell?.GetTile != null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -398,8 +397,8 @@ namespace QwirkleClassLibrary
|
|
|
|
|
score += CalculateAdjacentScore(cell, b, cellsPlayed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!scoreBoard.TryAdd(player, score))
|
|
|
|
|
|
|
|
|
|
if (!scoreBoard.TryAdd(player, score))
|
|
|
|
|
{
|
|
|
|
|
scoreBoard[player] += score;
|
|
|
|
|
}
|
|
|
|
@ -454,12 +453,63 @@ namespace QwirkleClassLibrary
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<int> CheckTilesBag()
|
|
|
|
|
{
|
|
|
|
|
List<int> PlayerTilesBagPos = new List<int>();
|
|
|
|
|
|
|
|
|
|
if (bag.TilesBag.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < players.Count(); i++)
|
|
|
|
|
{
|
|
|
|
|
if (players[i].Tiles.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
PlayerTilesBagPos.Add(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PlayerTilesBagPos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CheckBoardTile(List<int> PlayerTilesBagPos)
|
|
|
|
|
{
|
|
|
|
|
for(int i=0; i<PlayerTilesBagPos.Count(); i++)
|
|
|
|
|
{
|
|
|
|
|
for(int j=0; j < this.players[PlayerTilesBagPos[i]].Tiles.Count; j++)
|
|
|
|
|
{
|
|
|
|
|
for(int b=0; b < this.board.ReadCells.Count(); b++)
|
|
|
|
|
{
|
|
|
|
|
int x = this.board.ReadCells[b].GetX;
|
|
|
|
|
int y = this.board.ReadCells[b].GetY;
|
|
|
|
|
|
|
|
|
|
if (IsMoveCorrect(this.players[PlayerTilesBagPos[i]].Tiles[j], x, y, this.board))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsGameOver(Player player)
|
|
|
|
|
public bool CheckGameOver(Player player)
|
|
|
|
|
{
|
|
|
|
|
OnEndOfGame(new EndOfGameNotifiedEventArgs(player));
|
|
|
|
|
GameRunning = false;
|
|
|
|
|
return true;
|
|
|
|
|
List<int> PlayerTilesBagPos = CheckTilesBag();
|
|
|
|
|
|
|
|
|
|
if (PlayerTilesBagPos.Count() != 0)
|
|
|
|
|
{
|
|
|
|
|
if (!CheckBoardTile(PlayerTilesBagPos))
|
|
|
|
|
{
|
|
|
|
|
OnEndOfGame(new EndOfGameNotifiedEventArgs(player));
|
|
|
|
|
GameRunning = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|