You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
559 lines
16 KiB
559 lines
16 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Collections;
|
|
using System.Collections.Immutable;
|
|
using QwirkleClassLibrary.Tiles;
|
|
using QwirkleClassLibrary.Boards;
|
|
using QwirkleClassLibrary.Events;
|
|
using QwirkleClassLibrary.Players;
|
|
|
|
namespace QwirkleClassLibrary.Games
|
|
{
|
|
|
|
public class Game : IPlayer, IRules
|
|
{
|
|
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;
|
|
|
|
public ReadOnlyCollection<Player> PlayerList => players.AsReadOnly();
|
|
private readonly List<Player> players = new();
|
|
|
|
public ReadOnlyCollection<Cell> CellsUsed => cellUsed.AsReadOnly();
|
|
private readonly List<Cell> cellUsed = new();
|
|
|
|
public event EventHandler<AddPlayerNotifiedEventArgs>? PlayerAddNotified;
|
|
|
|
protected virtual void OnPlayerNotified(AddPlayerNotifiedEventArgs args)
|
|
=> PlayerAddNotified?.Invoke(this, args);
|
|
|
|
public event EventHandler<NextPlayerNotifiedEventArgs>? NextPlayerNotified;
|
|
|
|
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 event EventHandler<EndOfGameNotifiedEventArgs>? EndOfGameNotified;
|
|
|
|
protected virtual void OnEndOfGame(EndOfGameNotifiedEventArgs args)
|
|
=> EndOfGameNotified?.Invoke(this, args);
|
|
|
|
public Game()
|
|
{
|
|
bag = CreateTileBag(3);
|
|
board = CreateBoard();
|
|
|
|
}
|
|
|
|
public bool AddPlayerInGame(string? playerTag)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(playerTag))
|
|
{
|
|
OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The name is null or white space."));
|
|
return false;
|
|
}
|
|
|
|
if (GameRunning)
|
|
{
|
|
OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is running."));
|
|
return false;
|
|
}
|
|
|
|
if (players.Count >= 4)
|
|
{
|
|
OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is full."));
|
|
return false;
|
|
}
|
|
|
|
foreach (var p in players)
|
|
{
|
|
if (p.NameTag == playerTag)
|
|
{
|
|
OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : Name alreay taken"));
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
Player pl = CreatePlayer(playerTag);
|
|
|
|
players.Add(pl);
|
|
scoreBoard.Add(pl, 0);
|
|
OnPlayerNotified(new AddPlayerNotifiedEventArgs("Player was correctly added"));
|
|
return true;
|
|
}
|
|
|
|
public Player CreatePlayer(string playerTag)
|
|
{
|
|
var player = new Player(playerTag);
|
|
|
|
return player;
|
|
}
|
|
|
|
public Board GetBoard() { return board; }
|
|
|
|
public Board CreateBoard()
|
|
{
|
|
board = new Board(15, 12);
|
|
return board;
|
|
}
|
|
|
|
public TileBag CreateTileBag(int nbSet)
|
|
{
|
|
bag = new TileBag(nbSet);
|
|
return bag;
|
|
}
|
|
|
|
public void StartGame()
|
|
{
|
|
if (players.Count < 2 || players.Count >= 5) return;
|
|
GameRunning = true;
|
|
}
|
|
|
|
public void AddCellUsed(Cell? c)
|
|
{
|
|
if (c != null) cellUsed.Add(c);
|
|
}
|
|
|
|
public void EmptyCellUsed()
|
|
{
|
|
cellUsed.Clear();
|
|
}
|
|
|
|
public Player GetPlayingPlayer()
|
|
{
|
|
if (GetPlayingPlayerPosition() == -1)
|
|
{
|
|
throw new ArgumentException("No player play.");
|
|
}
|
|
return players[GetPlayingPlayerPosition()];
|
|
}
|
|
|
|
public int GetPlayingPlayerPosition()
|
|
{
|
|
for (int i = 0; i < players.Count; i++)
|
|
{
|
|
if (players[i].IsPlaying)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public Tile TileOfPlayerWithPos(int postile)
|
|
{
|
|
return players[GetPlayingPlayerPosition()].Tiles[postile];
|
|
}
|
|
|
|
public void GiveTilesToPlayers()
|
|
{
|
|
foreach (var p in players)
|
|
{
|
|
for (int j = 0; j < 6; j++)
|
|
{
|
|
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count);
|
|
|
|
p.AddTileToPlayer(bag.TilesBag[val]);
|
|
bag.RemoveTileInBag(bag.TilesBag[val]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public string SetFirstPlayer()
|
|
{
|
|
if (GameRunning)
|
|
{
|
|
players[0].IsPlaying = true;
|
|
OnNextPlayer(new NextPlayerNotifiedEventArgs(players[0]));
|
|
return players[0].NameTag;
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentException("Game is not running");
|
|
}
|
|
|
|
}
|
|
|
|
public string SetNextPlayer()
|
|
{
|
|
int i = GetPlayingPlayerPosition();
|
|
|
|
if (i == -1)
|
|
{
|
|
return SetFirstPlayer();
|
|
}
|
|
|
|
players[i].IsPlaying = false;
|
|
players[(i + 1) % players.Count].IsPlaying = true;
|
|
OnNextPlayer(new NextPlayerNotifiedEventArgs(players[(i + 1) % players.Count]));
|
|
|
|
return players[GetPlayingPlayerPosition()].NameTag;
|
|
}
|
|
|
|
public bool PlaceTile(Player player, Tile tile, int x, int y)
|
|
{
|
|
if (!IsMoveCorrect(tile, x, y, board)) return false;
|
|
if (board.AddTileInCell(x, y, tile))
|
|
{
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "was correctly placed !"));
|
|
AddCellUsed(board.GetCell(x, y));
|
|
return player.RemoveTileToPlayer(tile);
|
|
}
|
|
|
|
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>
|
|
/// <param name="player"></param>
|
|
/// <returns></returns>
|
|
public bool DrawTiles(Player player)
|
|
{
|
|
while (player.Tiles.Count < 6)
|
|
{
|
|
if (bag.TilesBag.Count == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count);
|
|
|
|
player.AddTileToPlayer(bag.TilesBag[val]);
|
|
bag.RemoveTileInBag(bag.TilesBag[val]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool SwapTiles(Player player, List<Tile> tilesToSwap)
|
|
{
|
|
if (tilesToSwap.Count == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
foreach (var t in tilesToSwap)
|
|
{
|
|
if (!player.RemoveTileToPlayer(t))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!DrawTiles(player))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
foreach (var t in tilesToSwap)
|
|
{
|
|
bag.AddTileInBag(t);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool CheckExtendedSurroundingCells(Tile tile, int x, int y, int dx, int dy, Board b)
|
|
{
|
|
for (int i = 1; i < 7; i++)
|
|
{
|
|
var extendedCell = b.GetCell(x + i * dx, y + i * dy);
|
|
|
|
if (extendedCell?.GetTile == null)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (extendedCell.GetTile.GetColor != tile.GetColor && extendedCell.GetTile.GetShape != tile.GetShape)
|
|
{
|
|
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)
|
|
{
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Tile already placed on the same line / column !"));
|
|
return false;
|
|
}
|
|
|
|
if (i == 6)
|
|
{
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Row/Column already are 6 tiles long !"));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool CheckTilesInLine(List<Cell> cells, Board b, int x, int y)
|
|
{
|
|
if (cells.Count == 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
var x1 = cells[0].GetX;
|
|
var y1 = cells[0].GetY;
|
|
|
|
if (cells.Count < 2 && (x1 == x || y1 == y))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (x1 != x && y1 != y)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var x2 = cells[1].GetX;
|
|
var y2 = cells[1].GetY;
|
|
|
|
if (x1 == x2)
|
|
{
|
|
return x == x1;
|
|
}
|
|
if (y1 == y2)
|
|
{
|
|
return y == y1;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool IsMoveCorrect(Tile t, int x, int y, Board b)
|
|
{
|
|
if (!b.HasOccupiedCase())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
var surroundingCells = new List<Cell?>
|
|
{
|
|
b.GetCell(x + 1, y),
|
|
b.GetCell(x - 1, y),
|
|
b.GetCell(x, y + 1),
|
|
b.GetCell(x, y - 1)
|
|
};
|
|
|
|
foreach (var cell in surroundingCells)
|
|
{
|
|
if (cell?.GetTile == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (cell.GetTile.GetColor != t.GetColor && cell.GetTile.GetShape != t.GetShape)
|
|
{
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : Colors / Shapes do not match with the surrounding tiles !"));
|
|
return false;
|
|
}
|
|
|
|
if (cell.GetTile.GetColor == t.GetColor && cell.GetTile.GetShape == t.GetShape)
|
|
{
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " is already placed on the same line / column !"));
|
|
|
|
return false;
|
|
}
|
|
|
|
var dx = cell.GetX - x;
|
|
var dy = cell.GetY - y;
|
|
|
|
if (!CheckExtendedSurroundingCells(t, x, y, dx, dy, b))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
if (!CheckTilesInLine(cellUsed, b, x, y))
|
|
{
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "isn't on the same line as the ones previously placed !"));
|
|
return false;
|
|
}
|
|
|
|
if (!surroundingCells.Any(cell => cell?.GetTile != null))
|
|
{
|
|
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You can't place a tile that isn't adjacent to another one !"));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
public int GetPlayerScore(Player player, ReadOnlyCollection<Cell> cellsPlayed, Board b)
|
|
{
|
|
if (cellsPlayed.Count == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int score = cellsPlayed.Count;
|
|
|
|
if (cellsPlayed.Count == 6)
|
|
{
|
|
score += 6;
|
|
}
|
|
|
|
int cellsX = cellsPlayed[0].GetX;
|
|
int cellsY = cellsPlayed[0].GetY;
|
|
|
|
foreach (var cell in cellsPlayed)
|
|
{
|
|
if (cellsX != cell.GetX && cellsX != -1)
|
|
{
|
|
cellsX = -1;
|
|
}
|
|
|
|
else if (cellsY != cell.GetY && cellsY != -1)
|
|
{
|
|
cellsY = -1;
|
|
}
|
|
}
|
|
|
|
score += cellsPlayed.Sum(cell => CalculateAdjacentScore(cell, b, cellsPlayed, cellsX, cellsY));
|
|
|
|
|
|
if (!scoreBoard.TryAdd(player, score))
|
|
{
|
|
scoreBoard[player] += score;
|
|
}
|
|
|
|
return score;
|
|
}
|
|
|
|
public int CalculateAdjacentScore(Cell cell, Board b, ReadOnlyCollection<Cell> cellsPlayed, int cellsX, int cellsY)
|
|
{
|
|
int score = 0;
|
|
|
|
var surroundingCells = new[]
|
|
{
|
|
b.GetCell(cell.GetX + 1, cell.GetY),
|
|
b.GetCell(cell.GetX - 1, cell.GetY),
|
|
b.GetCell(cell.GetX, cell.GetY + 1),
|
|
b.GetCell(cell.GetX, cell.GetY - 1)
|
|
};
|
|
|
|
foreach (var adjacentCell in surroundingCells)
|
|
{
|
|
if (adjacentCell?.GetTile == null || cellsPlayed.Contains(adjacentCell))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
int dx = adjacentCell.GetX - cell.GetX;
|
|
int dy = adjacentCell.GetY - cell.GetY;
|
|
|
|
score += CalculateLineScore(cell, dx, dy, b, cellsX, cellsY, cellsPlayed.Count);
|
|
}
|
|
|
|
return score;
|
|
}
|
|
|
|
public int CalculateLineScore(Cell cell, int dx, int dy, Board b, int cellsX, int cellsY, int nbCellsPlayed)
|
|
{
|
|
int score = 0;
|
|
|
|
for (int i = 1; i < 6; i++)
|
|
{
|
|
var extendedCell = b.GetCell(cell.GetX + i * dx, cell.GetY + i * dy);
|
|
|
|
if (extendedCell?.GetTile == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (dx != 0 && cellsY != -1 && nbCellsPlayed + i == 6 || dy != 0 && cellsX != -1 && nbCellsPlayed + i == 6)
|
|
{
|
|
score += 6;
|
|
}
|
|
|
|
score++;
|
|
}
|
|
|
|
if (dx == 0 && cellsX == -1 || dy == 0 && cellsY == -1)
|
|
{
|
|
score += 1;
|
|
}
|
|
|
|
return score;
|
|
}
|
|
|
|
|
|
public List<int> CheckTilesBag()
|
|
{
|
|
List<int> playerTilesBagPos = [];
|
|
|
|
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 < players[playerTilesBagPos[i]].Tiles.Count; j++)
|
|
{
|
|
for (int b = 0; b < board.ReadCells.Count; b++)
|
|
{
|
|
int x = board.ReadCells[b].GetX;
|
|
int y = board.ReadCells[b].GetY;
|
|
|
|
if (IsMoveCorrect(players[playerTilesBagPos[i]].Tiles[j], x, y, board))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
public bool CheckGameOver(Player player)
|
|
{
|
|
List<int> playerTilesBagPos = CheckTilesBag();
|
|
|
|
if (playerTilesBagPos.Count != 0 && !CheckBoardTile(playerTilesBagPos))
|
|
{
|
|
OnEndOfGame(new EndOfGameNotifiedEventArgs(player));
|
|
GameRunning = false;
|
|
scoreBoard[player] += 6;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
} |