/// <summary> /// Recalcuate the score of the saved dice /// </summary> private void RecalculateSavedDice() { // Reset the dice groups DiceItems.Where(d => d.State == DiceState.Saved).ToList().ForEach(d => d.Group = null); // Reset the scores CurrentRound.SaveScore -= _prevSave; CurrentRound.RiskScore -= _prevRisk; _prevRisk = _prevSave = 0; // Declare temporary variables DiceGroup dg = null; int count = 1; // While groups of dice exist, continue to group them while ((dg = GetDiceCheckGroup(DiceItems.Where(d => d.State == DiceState.Saved && d.Group == null))) != null) { // Apply the grouping and add the appropriate scores dg.Apply(count++); if (dg.IsScoreSaved) { _prevSave += dg.Score; } else { _prevRisk += dg.Score; } } // Reset the scores CurrentRound.SaveScore += _prevSave; CurrentRound.RiskScore += _prevRisk; Update(); }
/// <summary> /// The command handler that rolls the dice /// </summary> /// <param name="sender">The sender object</param> /// <param name="e">The event arguments</param> private void RollDice(object sender, ExecutedRoutedEventArgs e) { // Update round information _prevRisk = _prevSave = 0; // Score all saved dice DiceItems.Where(d => d.State == DiceState.Saved).ToList().ForEach(d => d.State = DiceState.Scored); // Check if all dice scored if (DiceItems.All(d => d.State == DiceState.Scored)) { DiceItems.ToList().ForEach(delegate(Dice d) { d.State = DiceState.Roll; d.Group = null; }); } // Roll all the roll dice DiceItems.Where(d => d.State == DiceState.Roll).ToList().ForEach(d => d.Roll()); Update(); }
/// <summary> /// Update the display of the dice and groups, as well as re-determining the status of the game /// </summary> private void Update() { // Refresh the view state of the application if (DiceCVS != null && DiceCVS.View != null) { DiceCVS.View.Refresh(); } // Check if the game is over if (CurrentRoundIndex >= MainWindow.RoundCount) { Status = FarkleStatus.GameOver; } // Check if any saved dice do not score else if (DiceItems.Any(d => d.State == DiceState.Saved && d.Group == null)) { Status = FarkleStatus.UnscoredDice; } // Check if farkle else if (!DiceItems.Any(d => d.State == DiceState.Saved) && GetDiceCheckGroup(DiceItems.Where(d => d.State == DiceState.Roll)) == null) { Status = FarkleStatus.Farkle; } // Check if dice must be scored before continuing on else if (!DiceItems.Any(d => d.State == DiceState.Saved)) { Status = FarkleStatus.SaveDice; } // Check if the threshold has been reached else if (Settings.Instance.ThresholdEnabled && CurrentRound.RoundScore < Settings.Instance.Threshold) { Status = FarkleStatus.ThresholdUnreached; } // Let the user roll else { Status = FarkleStatus.Roll; } }
/// <summary> /// The command handler that scores a round /// </summary> /// <param name="sender">The sender object</param> /// <param name="e">The event arguments</param> private void ScoreTurn(object sender, ExecutedRoutedEventArgs e) { // Update round information CurrentRound.IsFarkle = Status == FarkleStatus.Farkle; CurrentRound.IsFinal = true; CurrentRoundIndex++; OnPropertyChanged("TotalScore"); // Update the dice and roll them if (CurrentRoundExists) { DiceItems.ToList().ForEach(delegate(Dice d) { d.State = DiceState.Roll; d.Group = null; }); RollDice(sender, e); } else { Update(); } }