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.
243 lines
6.1 KiB
243 lines
6.1 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;
|
|
|
|
namespace QwirkleClassLibrary
|
|
{
|
|
public class Game : IPlayer, IRules
|
|
{
|
|
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<Score> ScoreList => scores.AsReadOnly();
|
|
private readonly List<Score> scores = new();
|
|
|
|
public Game()
|
|
{
|
|
bag = CreateTileBag(3);
|
|
board = CreateBoard();
|
|
}
|
|
|
|
public bool AddPlayerInGame(string? playerTag)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(playerTag) == true || this.GameRunning == true)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (players.Count >= 4)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
foreach (var p in players)
|
|
{
|
|
if (p.NameTag == playerTag)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
players.Add(CreatePlayer(playerTag));
|
|
scores.Add(new Score(players[players.Count-1]));
|
|
|
|
return true;
|
|
}
|
|
|
|
public Player CreatePlayer(string playerTag)
|
|
{
|
|
var player = new Player(playerTag);
|
|
return player;
|
|
}
|
|
|
|
public Board CreateBoard()
|
|
{
|
|
board = new Board(12, 12);
|
|
return board;
|
|
}
|
|
|
|
public TileBag CreateTileBag(int nbSet)
|
|
{
|
|
bag = new TileBag(nbSet);
|
|
return bag;
|
|
}
|
|
|
|
public bool StartGame()
|
|
{
|
|
if (players.Count < 2 || players.Count >= 5) return false;
|
|
this.GameRunning = true;
|
|
return true;
|
|
}
|
|
|
|
public Player GetPlayingPlayer()
|
|
{
|
|
if(GetPlayingPlayerPosition() == -1)
|
|
{
|
|
throw new ArgumentException();
|
|
}
|
|
return players[GetPlayingPlayerPosition()];
|
|
}
|
|
|
|
public int GetPlayingPlayerPosition()
|
|
{
|
|
for (int i = 0; i < players.Count; i++)
|
|
{
|
|
if (players[i].IsPlaying == true)
|
|
{
|
|
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 + 1);
|
|
|
|
p.AddTileToPlayer(bag.TilesBag[val]);
|
|
bag.RemoveTileInBag(bag.TilesBag[val]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public string SetFirstPlayer()
|
|
{
|
|
if (GameRunning == true)
|
|
{
|
|
players[0].IsPlaying = true;
|
|
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;
|
|
|
|
return players[GetPlayingPlayerPosition()].NameTag;
|
|
}
|
|
|
|
public bool PlaceTile(Player player, Tile tile, int x, int y)
|
|
{
|
|
if (board.AddTileInCell(x, y, tile) == true)
|
|
{
|
|
return player.RemoveTileToPlayer(tile) == true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
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 + 1);
|
|
|
|
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) == false)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (DrawTiles(player) == false)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
foreach (var t in tilesToSwap)
|
|
{
|
|
bag.AddTileInBag(t);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool IsMoveCorrect(Tile t, int x, int y, Board b)
|
|
{
|
|
if (b.HasOccupiedCase() == false)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
var surroundingTiles = new List<Tile?>();
|
|
|
|
surroundingTiles.Add(b.GetCell(x + 1, y)?.GetTile);
|
|
surroundingTiles.Add(b.GetCell(x - 1, y)?.GetTile);
|
|
surroundingTiles.Add(b.GetCell(x, y + 1)?.GetTile);
|
|
surroundingTiles.Add(b.GetCell(x, y - 1)?.GetTile);
|
|
|
|
foreach (var tile in surroundingTiles)
|
|
{
|
|
if (tile == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (tile.GetColor != t.GetColor && tile.GetShape != t.GetShape)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool IsGameOver()
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
} |