Guest User

Dice PS

a guest
May 28th, 2021
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Dice {
  2.     #Variables
  3.     hidden [PSCustomObject] $Side = [PSCustomObject] @{
  4.         Char   = $null
  5.         Number = $null
  6.     }
  7.     hidden [int32[]] $ValidNumbers = 1 .. 6
  8.     # Functions
  9.     [char] GetChar() {
  10.         switch ($this.Side.Number) {
  11.             default { return '⚀' }
  12.             2 { return '⚁' }
  13.             3 { return '⚂' }
  14.             4 { return '⚃' }
  15.             5 { return '⚄' }
  16.             6 { return '⚅' }
  17.         }
  18.         return $null
  19.     }
  20.     [int32] GetNumber() {
  21.         return $this.Side.Number
  22.     }
  23.     [void] RollDice() {
  24.         $this.Side.Number = $this.ValidNumbers | Sort-Object { [guid]::NewGuid().Guid } | Select-Object -First 1
  25.         $this.Side.Char = $this.GetChar()
  26.     }
  27.     # Constructor
  28.     Dice() {
  29.         $this.RollDice()
  30.     }
  31. }
  32.  
  33. # Usage
  34. $Dice = [Dice]::new()
  35. $Dice.GetNumber()
  36. $Dice.GetChar()
  37.  
  38. # Resuse same object
  39. $Dice.RollDice()
  40. $Dice.GetNumber()
  41.  
  42. # 1000 dices
  43. $Rolls = 1 .. 1000 | ForEach-Object {
  44.     [PSCustomObject] @{
  45.         Roll = $_
  46.         Dice = [Dice]::new()
  47.     }
  48. }
  49. $Rolls.Dice.GetNumber() | Measure-Object -Average
Advertisement
Add Comment
Please, Sign In to add comment