|
|
|
@ -42,7 +42,7 @@ namespace Models.Game
|
|
|
|
|
// == Events ==
|
|
|
|
|
public event EventHandler<GameStartedEventArgs> GameStarted;
|
|
|
|
|
public event EventHandler<GameEndedEventArgs> GameEnded;
|
|
|
|
|
public event EventHandler BoardUpdated;
|
|
|
|
|
public event EventHandler<BoardsUpdateEventArgs> BoardUpdated;
|
|
|
|
|
public event EventHandler<DiceRolledEventArgs> DiceRolled;
|
|
|
|
|
public event EventHandler<OperationChosenEventArgs> OperationChosen;
|
|
|
|
|
public event EventHandler<CellChosenEventArgs> CellChosen;
|
|
|
|
@ -159,7 +159,7 @@ namespace Models.Game
|
|
|
|
|
/// If the operation is MULTIPLICATION, it returns the product of the values of the two dice.
|
|
|
|
|
/// If the operation is not one of the operations, it throws an ArgumentOutOfRangeException.
|
|
|
|
|
/// </returns>
|
|
|
|
|
private int ResultOperation(Operation o)
|
|
|
|
|
public int ResultOperation(Operation o)
|
|
|
|
|
{
|
|
|
|
|
int result = o switch
|
|
|
|
|
{
|
|
|
|
@ -182,13 +182,25 @@ 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, int result)
|
|
|
|
|
private bool PlaceResult(Cell playerChoice, int result)
|
|
|
|
|
{
|
|
|
|
|
if (Turn == 1 || GameRules.NearCellIsValid(playerChoice, UsedMap.Boards))
|
|
|
|
|
{
|
|
|
|
|
playerChoice.Value = result;
|
|
|
|
|
BoardUpdated?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
for (int i = 0; i < UsedMap.Boards.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (UsedMap.Boards[i].X == playerChoice.X && UsedMap.Boards[i].Y == playerChoice.Y)
|
|
|
|
|
{
|
|
|
|
|
if (UsedMap.Boards[i].Value != null)
|
|
|
|
|
return false;
|
|
|
|
|
UsedMap.Boards[i].Value = result;
|
|
|
|
|
BoardUpdated?.Invoke(this, new BoardsUpdateEventArgs(UsedMap.Boards));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//playerChoice.Value = result;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -242,10 +254,10 @@ namespace Models.Game
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ends the game.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void EndGame()
|
|
|
|
|
private void EndGame(int? pts)
|
|
|
|
|
{
|
|
|
|
|
_isRunning = false;
|
|
|
|
|
GameEnded?.Invoke(this, new GameEndedEventArgs(CurrentPlayer));
|
|
|
|
|
GameEnded?.Invoke(this, new GameEndedEventArgs(CurrentPlayer, pts));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -257,7 +269,17 @@ namespace Models.Game
|
|
|
|
|
{
|
|
|
|
|
if (Turn == 20)
|
|
|
|
|
{
|
|
|
|
|
EndGame();
|
|
|
|
|
foreach(var cells in UsedMap.Boards)
|
|
|
|
|
{
|
|
|
|
|
GameRules.IsZoneValidAndAddToZones(cells, UsedMap);
|
|
|
|
|
AddToRopePath(cells, GameRules.EveryAdjacentCells(cells, UsedMap.Boards));
|
|
|
|
|
}
|
|
|
|
|
int? points = GameRules.FinalCalculusOfZones(UsedMap.Zones);
|
|
|
|
|
for (int i = 0; i < UsedMap.RopePaths.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
points += GameRules.ScoreRopePaths(UsedMap.RopePaths[i]);
|
|
|
|
|
}
|
|
|
|
|
EndGame(points);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -284,23 +306,31 @@ namespace Models.Game
|
|
|
|
|
/// <param name="result"></param>
|
|
|
|
|
/// <exception cref="InvalidCellCoordinatesException"></exception>
|
|
|
|
|
/// <exception cref="InvalidCellException"></exception>
|
|
|
|
|
public void HandlePlayerChoice(Cell cell, int result)
|
|
|
|
|
public bool HandlePlayerChoice(Cell cell, int result)
|
|
|
|
|
{
|
|
|
|
|
if (cell.X < 0 || cell.X >= UsedMap.Boards.Count / 6 || cell.Y < 0 || cell.Y >= 6)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidCellCoordinatesException("Invalid cell coordinates. Please choose again.");
|
|
|
|
|
return false;
|
|
|
|
|
//throw new InvalidCellCoordinatesException("Invalid cell coordinates. Please choose again.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!GameRules.IsCellValid(cell, UsedMap.Boards))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidCellException("Cell is not valid. Please choose again.");
|
|
|
|
|
return false;
|
|
|
|
|
//throw new InvalidCellException("Cell is not valid. Please choose again.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlaceResult(cell, result);
|
|
|
|
|
bool res = PlaceResult(cell, result);
|
|
|
|
|
if (!res)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
//throw new InvalidPlaceResultException("Cell is not valid for place result. Please choose again.");
|
|
|
|
|
}
|
|
|
|
|
GameRules.IsZoneValidAndAddToZones(cell, UsedMap);
|
|
|
|
|
AddToRopePath(cell, GameRules.EveryAdjacentCells(cell, UsedMap.Boards));
|
|
|
|
|
CellChosen?.Invoke(this, new CellChosenEventArgs(cell, result));
|
|
|
|
|
BoardUpdated?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
return true;
|
|
|
|
|
//BoardUpdated?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|