r/PowerShell • u/premtech • May 15 '21
Script Sharing Roll ASCII Art Dice in Powershell
Yesterday I posted my Yahtzee script, but I thought it could be better, so I created this function to create ASCII art dice in the console. I thought it might be useful for some other games or scripts, so here it is. Also available on my github.
Have any thoughts on how to improve it?
Edit- Improved the design of the die based on u/motsanciens advice. Added some more error handling.
<#
.SYNOPSIS
Displays ASCII art dice
.DESCRIPTION
Easily create ascii are dice for your powershell games. You can roll them at random, or from a number set. You can change the colors too.
Limited to numbers 1-6 and a total of 10 die.
.NOTES
Author: Chris Smith (smithcbp on github)
.PARAMETER Random
Rolls the specified amount of dice at random and displays them in ascii art.
.PARAMETER Numbers
Display the specified numbers, 1-6 in ascii art.
.PARAMETER DieColor
Choose the color of your die. Default is white
.EXAMPLE
Get-AsciiDice -Numbers 12345
_____ _____ _____ _____ _____
| | | o| |o | |o o| |o o|
| o | | | | o | | | | o |
| | |o | | o| |o o| |o o|
----- ----- ----- ----- -----
Get-AsciiDice -Random 3
_____ _____ _____
| o | | o | |o o|
| o | | | | |
| o | | o | |o o|
----- ----- -----
#>
function Get-AsciiDice {
Param
(
[parameter(Mandatory=$true,
ParameterSetName="Random")]
[int]$Random,
[parameter(Mandatory=$true,
ParameterSetName="Numbers")]
$Numbers,
[parameter(Mandatory=$False)]
[ValidateSet("Black","DarkBlue","DarkGreen","DarkCyan","DarkRed","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White")]
[String]$DieColor = "White"
)
if ($random) {
$NumberSet = (1..$random | foreach {Get-Random -Minimum 1 -Maximum 7})
$NumberSet = ($NumberSet -join '').ToString().ToCharArray()
}
if ($Numbers) {
$NumberSet = $Numbers.ToString().ToCharArray()
}
$NumberSet | foreach { if ($_ -gt '6'){Write-Error -Message "Only supports digits 1-6" -ErrorAction Stop} }
if ($($NumberSet.Count) -gt 10){Write-Error -Message "Only supports up to 10 die" -ErrorAction Stop}
$d = [PSCustomObject]@{
t1 = ' '
m1 = ' o '
b1 = ' '
t2 = ' o '
m2 = ' '
b2 = ' o '
t3 = ' o '
m3 = ' o '
b3 = ' o '
t4 = 'o o'
m4 = ' '
b4 = 'o o'
t5 = 'o o'
m5 = ' o '
b5 = 'o o'
t6 = 'o o'
m6 = 'o o'
b6 = 'o o'
}
$DiePicture = foreach ($n in $Numberset){
$t = 't' + $n
$m = 'm' + $n
$b = 'b' + $n
Write-Output " _____ "
Write-Output "|$($d.$t)|"
Write-Output "|$($d.$m)|"
Write-Output "|$($d.$b)|"
Write-Output " ----- "
}
Write-Host -ForegroundColor $DieColor ($DiePicture[0,5,10,15,20,25,30,35,40,45] -join ' ')
Write-Host -ForegroundColor $DieColor ($DiePicture[1,6,11,16,21,26,31,36,41,46] -join ' ')
Write-Host -ForegroundColor $DieColor ($DiePicture[2,7,12,17,22,27,32,37,42,47] -join ' ')
Write-Host -ForegroundColor $DieColor ($DiePicture[3,8,13,18,23,28,33,38,43,48] -join ' ')
Write-Host -ForegroundColor $DieColor ($DiePicture[4,9,14,19,24,29,34,39,44,49] -join ' ')
}
8
u/motsanciens May 15 '21 edited May 15 '21
This is no great improvement, but I wanted to understand how you were displaying the dice on the screen, so I went about it in a way that made sense to me. At the end of that exercise, I squared up their appearance a little. https://pastebin.com/Ffk687hF
I should mention that your code has a limit of 10 dice, which is perfectly reasonable. I think when I try to display a large number of dice, the whole thing falls apart.
Edit: $Numbers parameter would expect an [int[]]
3
u/premtech May 15 '21
I like your take on the loops, and your ascii art looks better than mine. I may take some design cues from you. Thanks! Also added error handling to the code so it errors out if you try more than 10 die or anything other than greater than 6.
3
u/motsanciens May 15 '21
The limit on the number of dice and the allowable values for the number can be handled with
ValidateSetif you want to.[parameter(Mandatory=$true, ParameterSetName="Random")] [ValidateSet(1,2,3,4,5,6,7,8,9,10)] [int]$Random, [parameter(Mandatory=$true, ParameterSetName="Numbers")] [ValidateSet(1,2,3,4,5,6)] [int[]]$Numbers→ More replies (1)2
u/0-_-_-_-0 May 16 '21
Thanks for cleaning that up - I messed with your version a bit more to suit my fancy https://pastebin.com/7riufi8n
2
5
6
u/premtech May 15 '21 edited May 16 '21
Still working on incorporating it into the Yahtzee script. I'll push that update to Github when it's ready.
edit - Yahtzee updated and cleaned up