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.
sae201_qwirkle/Qwirkle/QwirkleClassLibrary/Game.cs

166 lines
3.8 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;
namespace QwirkleClassLibrary
{
public class Game
{
private TileBag bag;
private bool gameRunning;
private Board board;
public ReadOnlyCollection<Player> PlayerList { get; private set; }
private readonly List<Player> players = new List<Player>();
public Game()
{
board = new Board();
bag = new TileBag(3);
gameRunning = false;
PlayerList = players.AsReadOnly();
}
public bool AddPlayerInGame(string? PlayerTag)
{
bool nameInvalid = string.IsNullOrWhiteSpace(PlayerTag);
if (nameInvalid == true || this.gameRunning == true || PlayerTag == null)
{
return false;
}
if (players.Count >= 4)
{
return false;
}
for (int i = 0; i < players.Count; i++)
{
if (players[i].GetNameTag == PlayerTag)
{
return false;
}
}
Player p = new Player(PlayerTag);
players.Add(p);
return true;
}
public bool StartGame()
{
if (players.Count < 2)
{
return false;
}
this.gameRunning = true;
return true;
}
public int GetPlayingPlayerPosition()
{
for (int i = 0; i < players.Count; i++)
{
if (players[i].IsPlaying == true)
{
return i;
}
}
return -1;
}
public int GetNbPlayers
{
get { return players.Count; }
}
// a passer en program cs
public string ShowTileOfPlayer(int posplayer)
{
List<Tile> tiles = players[posplayer].Tiles.ToList();
string r = ("Tile of " + posplayer + " : ");
StringBuilder stringBuilder = new StringBuilder(); // A UTILISER !!!
foreach (Tile tile in tiles)
{
r = (r + " " + tile.NameColorTile());
}
return r;
}
public Tile TileOfPlayerWithPos(int postile)
{
return players[GetPlayingPlayerPosition()].Tiles[postile];
}
public void SetNextPlayer(int old, int neew)
{
if (old >= 0 || old != -1)
{
players[old].IsPlaying = false;
}
players[neew].IsPlaying = true;
}
public void TilsBagPlayer()
{
for (int i = 0; i < players.Count; i++)
{
for (int j = 0; j < 6; j++)
{
Tile tile = bag.TilesBag[j];
players[i].AddTilePlayer(tile);
bag.RemoveTileInBag(tile);
}
}
}
public string NextPlayer()
{
int posPlayerPlay = GetPlayingPlayerPosition();
int posPlayerNextPlay = GetPlayingPlayerPosition() + 1;
if (posPlayerNextPlay >= GetNbPlayers)
{
posPlayerNextPlay = 0;
}
SetNextPlayer(posPlayerPlay, posPlayerNextPlay);
return (players[posPlayerNextPlay].GetNameTag);
}
public bool GameRunning
{
get { return gameRunning; }
}
public bool PlaceTileGame(Tile tile, int x, int y)
{
bool r = board.AddTileInCell(x, y, tile);
players[GetPlayingPlayerPosition()].RemoveTilePlayer(tile);
return r;
}
}
}