@ -6,17 +6,16 @@ using System.Text;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using System.Xml.Linq ;
using System.Xml.Linq ;
namespace QwirkleClassLibrary
namespace QwirkleClassLibrary
{
{
public class Game
public class Game : IPlayer
{
{
private TileBag bag ;
private TileBag bag ;
p rivate bool gameRunning ;
p ublic bool GameRunning { get ; private set ; }
private Board board ;
private Board board ;
public ReadOnlyCollection < Player > PlayerList { get ; private set ; }
public ReadOnlyCollection < Player > PlayerList { get ; }
private readonly List < Player > players = new List < Player > ( ) ;
private readonly List < Player > players ;
public Game ( )
public Game ( )
{
{
@ -27,11 +26,9 @@ namespace QwirkleClassLibrary
PlayerList = players . AsReadOnly ( ) ;
PlayerList = players . AsReadOnly ( ) ;
}
}
public bool AddPlayerInGame ( string? P layerTag)
public bool AddPlayerInGame ( string? p layerTag)
{
{
bool nameInvalid = string . IsNullOrWhiteSpace ( PlayerTag ) ;
if ( string . IsNullOrWhiteSpace ( playerTag ) = = true | | this . GameRunning = = true )
if ( nameInvalid = = true | | this . gameRunning = = true | | PlayerTag = = null )
{
{
return false ;
return false ;
}
}
@ -41,32 +38,30 @@ namespace QwirkleClassLibrary
return false ;
return false ;
}
}
for ( int i = 0 ; i < players . Count ; i + + )
for each ( var p in players )
{
{
if ( p layers[ i ] . NameTag = = P layerTag)
if ( p . NameTag = = p layerTag)
{
{
return false ;
return false ;
}
}
}
}
Player p = new Player ( PlayerTag ) ;
players . Add ( CreatePlayer ( playerTag ) ) ;
players . Add ( p ) ;
return true ;
return true ;
}
}
public bool StartGame ( )
public Player CreatePlayer ( string playerTag )
{
{
if ( players . Count < 2 )
var player = new Player ( playerTag ) ;
{
return player ;
return false ;
}
}
this . gameRunning = true ;
public void StartGame ( )
return true ;
{
this . GameRunning = true ;
}
}
public int GetPlayingPlayerPosition ( )
public int GetPlayingPlayerPosition ( )
{
{
for ( int i = 0 ; i < players . Count ; i + + )
for ( int i = 0 ; i < players . Count ; i + + )
@ -79,80 +74,72 @@ namespace QwirkleClassLibrary
return - 1 ;
return - 1 ;
}
}
public int GetNbPlayers
{
get { return players . Count ; }
}
public Tile TileOfPlayerWithPos ( int postile )
public Tile TileOfPlayerWithPos ( int postile )
{
{
return players [ GetPlayingPlayerPosition ( ) ] . Tiles [ postile ] ;
return players [ GetPlayingPlayerPosition ( ) ] . Tiles [ postile ] ;
}
}
public void SetNextPlayer( int old , int neew )
public void GiveTilesToPlayers ( )
{
{
foreach ( var p in players )
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 + + )
for ( int j = 0 ; j < 6 ; j + + )
{
{
Random random = new Random ( ) ;
Random random = new Random ( ) ;
int val = random . Next ( 0 , bag . TilesBag . Count ) ;
int val = random . Next ( 0 , bag . TilesBag . Count ) ;
Tile tile = bag . TilesBag [ val ] ;
p layers[ i ] . AddTilePlayer ( tile ) ;
p . AddTileToPlayer ( bag . TilesBag [ val ] ) ;
bag . RemoveTileInBag ( tile ) ;
bag . RemoveTileInBag ( bag . TilesBag [ val ] ) ;
}
}
}
}
}
}
public string NextPlayer ( )
public bool PlaceTileGame ( Tile tile , int x , int y )
{
{
int posPlayerPlay = GetPlayingPlayerPosition ( ) ;
bool checkremove = false ;
bool checkaddcell = board . AddTileInCell ( x , y , tile ) ;
int posPlayerNextPlay = GetPlayingPlayerPosition ( ) + 1 ;
if ( checkaddcell = = true )
if ( posPlayerNextPlay > GetNbPlayers )
{
{
posPlayerNextPlay = 0 ;
checkremove = players [ GetPlayingPlayerPosition ( ) ] . RemoveTileToPlayer ( tile ) ;
}
}
SetNextPlayer ( posPlayerPlay , posPlayerNextPlay ) ;
if ( checkaddcell = = checkremove )
{
return ( players [ posPlayerNextPlay ] . NameTag ) ;
return checkaddcell ;
}
return false ;
}
}
public bool GameRunning
public string SetFirstPlayer ( )
{
{
get { return gameRunning ; }
players [ 0 ] . IsPlaying = true ;
return players [ 0 ] . NameTag ;
}
}
public bool PlaceTileGame ( Tile tile , int x , int y )
public string SetNextPlayer ( )
{
{
bool checkremove = false ;
int i = GetPlayingPlayerPosition ( ) ;
bool checkaddcell = board . AddTileInCell ( x , y , tile ) ;
if ( checkaddcell = = true )
if ( i = = - 1 )
{
{
checkremove = players [ GetPlayingPlayerPosition ( ) ] . RemoveTilePlayer ( tile ) ;
return SetFirstPlayer ( ) ;
}
}
if ( checkaddcell = = checkremove )
players [ i ] . IsPlaying = false ;
{
players [ ( i + 1 ) % players . Count ] . IsPlaying = true ;
return checkaddcell ;
return players [ GetPlayingPlayerPosition ( ) ] . NameTag ;
}
}
return false ;
public void PlaceTile ( Player player , Tile tile , int x , int y )
{
throw new NotImplementedException ( ) ;
}
}
public bool ContinueToPlay ( )
{
throw new NotImplementedException ( ) ;
}
}
}