/// <summary> /// Handles guess events /// </summary> /// <param name="sender">Object which emitted the event</param> /// <param name="args">Event arguments</param> public void OnNumberGuessedHandler(object sender, PlayerGuessEventArgs args) { lock (_numbersToGuess) { _numbersToGuess.Remove(args.GuessedNumber); } }
public void OnNumberGuessedHandler(object sender, PlayerGuessEventArgs args) { lock (_memorizedNumbers) { if (!_memorizedNumbers.Contains(args.GuessedNumber)) { _memorizedNumbers.Add(args.GuessedNumber); } } }
/// <summary> /// Handles all players guesses and makes related changes in game state /// </summary> /// <param name="sender">Object which emitted the event</param> /// <param name="args">Event arguments</param> private void OnNumberGuessedHandler(object sender, PlayerGuessEventArgs args) { var player = sender as GenericPlayer <IGuessStrategy>; if (player == null) { return; } var currentDelta = Math.Abs(_basket.Weight - args.GuessedNumber); lock (_state) { if (!_finished && _state.Winner == null && _state.AttemptsNumber < Restriction.MaxAttempts) { _state.AttemptsNumber++; if (args.GuessedNumber == _basket.Weight) { _state.Winner = player; // This signal is used for victory case _finalizeEvent.Set(); } else { var previousDelta = Math.Abs(_basket.Weight - _state.ClosestGuess); if (previousDelta > currentDelta || _state.ClosestPlayer == null) { _state.ClosestPlayer = player; _state.ClosestGuess = args.GuessedNumber; } } } else { // Here game is finishing, no sense to guess any more // So we prevent guessing for players threads that are // not aborted yet StopPlayersGuessing(); // This signal is used for attempts exceeded case _finalizeEvent.Set(); } } player.Wait(currentDelta); }