Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Dice {
- #Variables
- hidden [PSCustomObject] $Side = [PSCustomObject] @{
- Char = $null
- Number = $null
- }
- hidden [int32[]] $ValidNumbers = 1 .. 6
- # Functions
- [char] GetChar() {
- switch ($this.Side.Number) {
- default { return '⚀' }
- 2 { return '⚁' }
- 3 { return '⚂' }
- 4 { return '⚃' }
- 5 { return '⚄' }
- 6 { return '⚅' }
- }
- return $null
- }
- [int32] GetNumber() {
- return $this.Side.Number
- }
- [void] RollDice() {
- $this.Side.Number = $this.ValidNumbers | Sort-Object { [guid]::NewGuid().Guid } | Select-Object -First 1
- $this.Side.Char = $this.GetChar()
- }
- # Constructor
- Dice() {
- $this.RollDice()
- }
- }
- # Usage
- $Dice = [Dice]::new()
- $Dice.GetNumber()
- $Dice.GetChar()
- # Resuse same object
- $Dice.RollDice()
- $Dice.GetNumber()
- # 1000 dices
- $Rolls = 1 .. 1000 | ForEach-Object {
- [PSCustomObject] @{
- Roll = $_
- Dice = [Dice]::new()
- }
- }
- $Rolls.Dice.GetNumber() | Measure-Object -Average
Advertisement
Add Comment
Please, Sign In to add comment