RosettaCodeData/Task/24-game/PowerShell/24-game.psh

74 lines
1.7 KiB
Text
Raw Permalink Normal View History

2013-04-10 22:43:41 -07:00
CLS
Function isNumeric ($x)
{
$x2 = 0
$isNum = [System.Int32]::TryParse($x,[ref]$x2)
Return $isNum
}
$NumberArray = @()
2013-10-27 22:24:23 +00:00
While( $NumberArray.Count -lt 4 ){
$NumberArray += Random -Minimum 1 -Maximum 10
}
Write-Host @"
Welcome to the 24 game!
2013-04-10 22:43:41 -07:00
2013-10-27 22:24:23 +00:00
Here are your numbers: $($NumberArray -join ",").
Use division, multiplication, subtraction and addition to get 24 as a result with these 4 numbers.
"@
2013-04-10 22:43:41 -07:00
Do
{
$Wrong = 0
$EndResult = $null
$TempChar = $null
$TempChar2 = $null
$Count = $null
2013-10-27 22:24:23 +00:00
$AllowableCharacters = $NumberArray + "+-*/()".ToCharArray()
2013-04-10 22:43:41 -07:00
$Result = Read-Host
Foreach($Char in $Result.ToCharArray())
{
2013-10-27 22:24:23 +00:00
If( $AllowableCharacters -notcontains $Char ){ $Wrong = 1 }
2013-04-10 22:43:41 -07:00
}
2013-10-27 22:24:23 +00:00
2013-04-10 22:43:41 -07:00
If($Wrong -eq 1)
{
Write-Warning "Wrong input! Please use only the given numbers."
}
Foreach($Char in $Result.ToCharArray())
{
If((IsNumeric $TempChar) -AND (IsNumeric $Char))
{
Write-Warning "Wrong input! Combining two or more numbers together is not allowed!"
}
$TempChar = $Char
}
Foreach($Char in $Result.ToCharArray())
{
If(IsNumeric $Char)
{
$Count++
}
}
If($Count -eq 4)
{
$EndResult = Invoke-Expression $Result
If($EndResult -eq 24)
{
Write-Host "`nYou've won the game!"
}
Else
{
Write-Host "`n$EndResult is not 24! Too bad."
}
}
Else
{
Write-Warning "Wrong input! You did not supply four numbers."
}
}
While($EndResult -ne 24)