@ -23,6 +23,8 @@ namespace Models.Game
[DataContract]
public class Game : INotifyPropertyChanged
{
public bool IsPreviousGameNotFinished { get ; private set ; }
/* Persistence Interface */
public IPersistence PersistenceManager { get ; set ; }
@ -104,6 +106,7 @@ namespace Models.Game
public Operation PlayerOperation { get ; set ; }
public Cell PlayerCell { get ; set ; }
public int Resultat { get ; set ; }
public Rules . Rules GameRules { get ; }
@ -135,22 +138,56 @@ namespace Models.Game
Maps . Add ( map ) ;
}
/// <summary>
/// Deletes the last game in the list of games. Use for avoiding stack not finished games.
/// </summary>
/// <returns></returns>
public bool DeleteGame ( )
{
if ( Games . Count = = 0 )
{
return false ;
}
Games . RemoveAt ( Games . Count - 1 ) ; // Remove the last game
return true ;
}
/// <summary>
/// Adds a new best score to the list of best scores. Or updates it if it already exists.
/// </summary>
/// <param name="finalScore">The final score of the game.</param>
public void AddBestScore ( int finalScore )
{
BestScore bs = new BestScore ( UsedMap . Name , CurrentPlayer , 1 , finalScore ) ;
foreach ( var score in BestScores )
{
if ( ! bs . Equals ( score ) ) continue ;
var existingScore = BestScores . FirstOrDefault ( score = > score . Equals ( bs ) ) ;
score . IncrGamesPlayed ( ) ;
score . UpdateScore ( finalScore ) ;
return ;
if ( existingScore ! = null )
{
existingScore . IncrGamesPlayed ( ) ;
existingScore . UpdateScore ( finalScore ) ;
}
else
{
BestScores . Add ( bs ) ;
}
BestScores . Add ( bs ) ;
BestScores . OrderByDescending ( p = > p . Score ) ;
// Sorting the best scores
List < BestScore > sortedScores = BestScores . OrderByDescending ( score = > score . Score ) . ToList ( ) ;
for ( int i = 0 ; i < sortedScores . Count ; i + + )
{
if ( ! BestScores [ i ] . Equals ( sortedScores [ i ] ) )
{
BestScores . Move ( BestScores . IndexOf ( sortedScores [ i ] ) , i ) ;
}
}
}
/// <summary>
/// Removes a player from the list of players.
/// </summary>
/// <param name="playerName"></param>
/// <returns>True if the player was removed successfully, false otherwise.</returns>
public bool RemovePlayer ( string playerName )
{
Player player = Players . FirstOrDefault ( p = > p . Pseudo = = playerName ) ;
@ -163,6 +200,12 @@ namespace Models.Game
return true ;
}
/// <summary>
/// Modifies the pseudo of a player.
/// </summary>
/// <param name="pseudo"></param>
/// <param name="newpseudo"></param>
/// <returns></returns>
public bool ModifyPlayer ( string pseudo , string newpseudo )
{
foreach ( var index in Players )
@ -178,6 +221,10 @@ namespace Models.Game
return false ;
}
/// <summary>
/// Removes a game from the list of games.
/// </summary>
/// <param name="playerName"></param>
public void CheckAndRemoveBestScoresDependencies ( string playerName )
{
List < BestScore > bs = new List < BestScore > ( ) ;
@ -194,6 +241,11 @@ namespace Models.Game
}
}
/// <summary>
/// Modifies the pseudo of a player in the best scores.
/// </summary>
/// <param name="playerName"></param>
/// <param name="newPlayerName"></param>
public void CheckAndChangeBestScoresDependencies ( string playerName , string newPlayerName )
{
foreach ( var bestScore in BestScores )
@ -213,6 +265,10 @@ namespace Models.Game
}
foreach ( var game in data . Item2 )
{
if ( game . IsRunning )
{
IsPreviousGameNotFinished = true ;
}
Games . Add ( game ) ;
}
foreach ( var map in data . Item3 )
@ -317,7 +373,7 @@ namespace Models.Game
/// </summary>
/// <param name="playerChoice">The cell chosen by the player to place the result.</param>
/// <param name="result">The result of the dice operation to be placed in the cell.</param>
private void PlaceResult ( Cell playerChoice )
private void PlaceResult ( Cell playerChoice , int result )
{
IEnumerable < Cell > ValidCell =
from cell in UsedMap . Boards
@ -328,14 +384,16 @@ namespace Models.Game
{
if ( item . X = = playerChoice . X & & item . Y = = playerChoice . Y )
{
if ( PlayerCell. Value > 12 | | ( PlayerCell . Value > 6 & & item . IsDangerous = = true ) )
if ( result > 12 | | ( result > 6 & & item . IsDangerous = = true ) )
{
item . SetPenalty ( ) ;
PlayerCell . SetPenalty ( ) ;
}
item . Value = PlayerCell. Value ;
item . Value = result ;
return ;
}
}
}
@ -398,7 +456,6 @@ namespace Models.Game
{
IsRunning = true ;
GameStarted ? . Invoke ( this , new GameStartedEventArgs ( CurrentPlayer ) ) ;
//GameLoop();
}
/// <summary>
@ -413,17 +470,13 @@ namespace Models.Game
/// <summary>
/// The main game loop that runs while the game is active.
/// </summary>
p rivate void GameLoop ( )
p ublic void GameLoop ( )
{
while ( IsRunning )
{
RollAllDice ( ) ;
int res = PlayerChooseOperation ( ) ;
PlayerOption ? . Invoke ( this , new PlayerOptionEventArgs ( UsedMap . Boards . ToList ( ) , res , Turn ) ) ;
Resultat = PlayerChooseOperation ( ) ;
PlayerSelectionCell ( ) ;
PlayerCell . Value = res ;
AddToRopePath ( PlayerCell , UsedMap . Boards . ToList ( ) ) ;
PlaceResult ( PlayerCell ) ;
BoardUpdated ? . Invoke ( this , new BoardsUpdateEventArgs ( UsedMap . Boards . ToList ( ) ) ) ;
Turn + + ;
}
@ -436,6 +489,7 @@ namespace Models.Game
{
PlayerChooseOp ? . Invoke ( this , new PlayerChooseOperationEventArgs ( PlayerOperation ) ) ;
}
PlayerOption ? . Invoke ( this , new PlayerOptionEventArgs ( UsedMap . Boards . ToList ( ) , ResultOperation ( PlayerOperation ) , Turn ) ) ;
return ResultOperation ( PlayerOperation ) ;
}
@ -447,6 +501,7 @@ namespace Models.Game
PlayerChooseCell ? . Invoke ( this , new PlayerChooseCellEventArgs ( PlayerCell ) ) ;
}
MarkOperationAsChecked ( PlayerOperation ) ;
PlaceResult ( PlayerCell , Resultat ) ;
}
/// <summary>