Play-RPS.ps1

rps

Play Rock, Paper, Scissors vs a the computer via PowerShell!

# Play-RPS.ps1 - Rock, Paper, Scissors in Powershell! 

# TechJeeper 2014 - http://www.techjeeper.com

$wins=0
$losses=0
$ties=0
cls
function RPS-Select
{
$choice = Read-Host "(R)ock/(P)aper/(S)cissors/(Q)uit"

cls
return $choice
}

function RPS-Random
{
$RPSPos = "R", "P", "S"
$RPSComp = $RPSPos[(Get-Random -Minimum 0 -Maximum 3)]
return $RPSComp
}

function Check-Win ($choice, $random)
{
cls
if($choice -eq "R"){$you="Rock"}
if($choice -eq "P"){$you="Paper"}
if($choice -eq "S"){$you="Scissors"}
if($random -eq "R"){$comp="Rock"}
if($random -eq "P"){$comp="Paper"}
if($random -eq "S"){$comp="Scissors"}

Write-Host "You: $you"
Write-Host "Computer: $comp"
Write-Host ""

if ($choice -eq $random){Write-Host "$you ties $comp" -ForegroundColor Yellow;return "T"}
if ($choice = "R" -and $random -eq "P"){Write-Host "Paper beats Rock - You Loose" -ForegroundColor red;return "L"}
if ($choice = "R" -and $random -eq "S"){Write-Host "Rock beats Scissors - You Win"-ForegroundColor green;return "W"}
if ($choice = "P" -and $random -eq "R"){Write-Host "Paper beat Rock - You Win"-ForegroundColor green;return "W"}
if ($choice = "P" -and $random -eq "S"){Write-Host "Scissors beat Paper - You Loose" -ForegroundColor red;return "L"}
if ($choice = "S" -and $random -eq "R"){Write-Host "Rock beat Scissors - You Loose" -ForegroundColor red;return "L"}
if ($choice = "S" -and $random -eq "P"){Write-Host "Scissors beat Paper - You Win" -ForegroundColor green;return "W"}
}
function Out-Score ($wins,$losses,$ties)
{
Write-Host ""
Write-Host "----------" -ForegroundColor White
Write-Host " Wins: $wins" -ForegroundColor Green
Write-Host " Losses: $losses" -ForegroundColor Red
Write-Host " Ties: $ties " -ForegroundColor Yellow
Write-Host "----------" -ForegroundColor White
}

while ($choice -ne "Q")
{
$valid=$FALSE
while($valid -eq $FALSE)
{
$choice = RPS-Select
if($choice -eq "R"){$valid=$TRUE}
if($choice -eq "P"){$valid=$TRUE}
if($choice -eq "S"){$valid=$TRUE}
if($choice -eq "Q"){$valid=$TRUE}
}

if ($choice -eq "Q"){exit}
$random = RPS-Random
$winner = Check-Win $choice $random
if ($winner -eq "T"){$ties++}
if ($winner -eq "L"){$losses++}
if ($winner -eq "W"){$wins++}
Out-Score $wins $losses $ties
}
exit

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *