Public/Start-RPSGame.ps1
<#
.Synopsis Start a new Rock-Paper-Scissors game. .DESCRIPTION Rock-Paper-Scissors is a zero sum hand game in which each player simultaneously forms one of three shapes with an outstretched hand. As this is within PowerShell, you select your entry via text input instead. The original rules are as follows: 'Rock' wins against 'Scissors'. 'Paper' wins against 'Rock'. 'Scissors' wins against 'Paper'. If players throw the same value, this results in a tie. .PARAMETER Iterations The number of rounds you want to play. Defaults to '5'. .EXAMPLE Start-RPSGame .EXAMPLE Start-RPSGame -Iterations 10 .INPUTS None .OUTPUTS RPS.Game .NOTES For entertainment purposes only; most likely a waste of your CPU resources. .FUNCTIONALITY For entertainment purposes only; most likely a waste of your CPU resources. #> function Start-RPSGame { [CmdletBinding(DefaultParameterSetName='Gamestate', SupportsShouldProcess=$true, PositionalBinding=$false, ConfirmImpact='Low')] [OutputType([PSObject])] Param ( [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, Position=0, HelpMessage='How many rounds of Rock-Paper-Scissors do you wish to play?', ParameterSetName='Default')] [Alias('Rounds','Turns')] [int]$Iterations=5 ) Begin { # Gamestate Variables: [int]$WinCount=0 [int]$LossCount=0 [int]$TieCount=0 [datetime]$start=(Get-Date) } # END BEGIN Process { if ($pscmdlet.ShouldProcess("Target", "Operation")) { for ($i = 1; $i -le $Iterations; $i++) { # Display the current round entry: Write-Host ([string]::Format("Round: {0} of {1}",$i,$Iterations)) # Get AI's selection for round: $cSelection=(Get-RPSAISelection) # Get user's selection for round: $uSelection=(Get-RPSUserSelection) # Determine the winner: $result=(Get-RPSResult -Player1Choice $uSelection -Player2Choice $cSelection); Write-Host ([string]::Format("You selected {0}. Computer selected {1}. {2}!",$uSelection,$cSelection,$result)) # Update overall score variables: switch ($result) { 'Win' { $WinCount++ break } 'Loss' { $LossCount++ break } 'Tie' { $TieCount++ break } Default{} } } # End of for loop. } $end=(Get-Date); # Collect our results: $rpsProperties = @{} $rpsProperties = @{ 'Player'=$env:USERNAME 'Computer'=$env:COMPUTERNAME 'Iterations'=$Iterations 'Win'=$WinCount 'Loss'=$LossCount 'Tie'=$TieCount 'StartTime'=$start 'EndTime'=$end 'ElapsedTime'=($end-$start) } # Create a new custom object: $rpsObject = New-Object -TypeName 'PsObject' -Property $rpsProperties # Add custom view type to object: $rpsObject.PSObject.TypeNames.Insert(0,'RPS.Game') # Write out the object to the pipeline: Write-Output $rpsObject } # END PROCESS End { } # END END } enum Choice { Rock = 0 Paper = 1 Scissors = 2 } enum Result { Loss = -1 Win = 0 Tie = 1 } |