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

186 lines
4.5 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 : IPlayer
{
private TileBag bag;
private bool gameRunning;
private Board board;
public ReadOnlyCollection<Player> PlayerList { get; private set; }
private readonly List<Player> players;
public Game()
{
board = new Board();
bag = new TileBag(3);
players = new List<Player>();
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].NameTag == PlayerTag)
{
return false;
}
}
Player p = new Player(PlayerTag);
players.Add(p);
return true;
}
public Player CreatePlayer(string playerTag)
{
var player = new Player(playerTag);
return player;
}
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; }
}
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++)
{
Random random = new Random();
int val = random.Next(0, bag.TilesBag.Count);
Tile tile = bag.TilesBag[val];
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].NameTag);
}
public bool GameRunning
{
get { return gameRunning; }
}
public bool PlaceTileGame(Tile tile, int x, int y)
{
bool checkremove=false;
bool checkaddcell = board.AddTileInCell(x, y, tile);
if (checkaddcell == true)
{
checkremove = players[GetPlayingPlayerPosition()].RemoveTilePlayer(tile);
}
if (checkaddcell == checkremove)
{
return checkaddcell;
}
return false;
}
public void SetNextPlayer(List<Player> playersList)
{
for(int i = 0; i < PlayerList.Count; i++)
{
if (PlayerList[i].IsPlaying != true) continue;
PlayerList[i].IsPlaying = false;
PlayerList[(i + 1) % PlayerList.Count].IsPlaying = true;
}
}
public void PlaceTile(Player player, Tile tile, int x, int y)
{
throw new NotImplementedException();
}
public bool ContinueToPlay()
{
throw new NotImplementedException();
}
}
}