DiceThrowing.razor.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. namespace Deathwatch.Pages
  2. {
  3. public partial class DiceThrowing
  4. {
  5. private Random? _random;
  6. public int NbOfDice { get; set; } = 1;
  7. public int DiceValue { get; set; } = 1;
  8. public int NbOfCritics { get; set; }
  9. public List<int>? Rolls { get; set; }
  10. public int Total { get; set; }
  11. //protected override async Task OnInitializedAsync()
  12. //{
  13. // _random = new Random();
  14. // Rolls = new List<int>();
  15. // var Task = new Task(() => { Thread.Sleep(1000); });
  16. // await Task;
  17. //}
  18. protected override void OnInitialized()
  19. {
  20. base.OnInitialized();
  21. _random = new Random();
  22. Rolls = new List<int>();
  23. }
  24. public void ThrowDice()
  25. {
  26. var nbOfRolls = NbOfDice;
  27. Total = 0;
  28. Rolls = new List<int>();
  29. for (int i = 0; i <= nbOfRolls; i++)
  30. {
  31. var roll = _random.Next(1, DiceValue);
  32. Total += roll;
  33. Rolls?.Add(roll);
  34. if (roll == DiceValue && roll > 0) nbOfRolls++;
  35. }
  36. }
  37. }
  38. }