DEV Community

Discussion on: Daily Challenge #5 - Ten Minute Walk

Collapse
 
figueroadavid profile image
David Figueroa

Powershell

I went the voluntary choice route with a voluntary initial route, and reversing it to get back.

function New-WalkPattern {
    [cmdletbinding()]
    param(
        [parameter(ValueFromPipelineByPropertyName)]
        [ValidateRange(4,30)]
        [int]$WalkTime = 14
    )

    if ($WalkTime % 2 -gt 0) {
        $WalkTime --
    }
    $WalkTimeArray = [char[]]::new($WalkTime)

    $LastDirection = 'x' #picking an invalid direction on purpose
    $UserSteps = $WalkTime / 2
    $ReverseStepTracker = -1

    $North   = [System.Management.Automation.Host.ChoiceDescription]::New('&North', 'Walk North')
    $East    = [System.Management.Automation.Host.ChoiceDescription]::New('&East', 'Walk East')
    $South   = [System.Management.Automation.Host.ChoiceDescription]::New('&South', 'Walk South')
    $West    = [System.Management.Automation.Host.ChoiceDescription]::New('&West', 'Walk West')
    $Options = [System.Management.Automation.Host.ChoiceDescription[]]($North, $East, $South, $West)
    $Title   = 'Which Direction'
    $Message = 'Pick a direction to walk'

    function Test-IfWalkDirectionIsRepeated {
        param(
            [string]$ThisDirection
        )
        if ($LastDirection -eq $ThisDirection) {
            Write-Warning -Message 'You cannot travel the same direction twice in a row!'
            $Result = $true
        }
        else {
            $LastDirection = $ThisDirection
            $Result = $false
        }
        return $Result
    }

    for ($i = 0; $i -lt $UserSteps; $i++) {
        $Result  = $host.ui.PromptForChoice($Title, $Message, $Options, 0)
        switch ($Result) {
            '0' {
                    if (Test-IfWalkDirectionIsRepeated -ThisDirection 'N')
                    {
                        $i ++
                        $ReverseStepTracker ++
                    }
                    else {
                        $WalkTimeArray[$i]  = 'N'
                        $WalkTimeArray[$ReverseStepTracker] = 'S'
                        $ReverseStepTracker --
                    }
                    break
            }
            '1' {
                    if (Test-IfWalkDirectionIsRepeated -ThisDirection 'E')
                    {
                        $i ++
                        $ReverseStepTracker ++
                    }
                    else {
                        $WalkTimeArray[$i]  = 'E'
                        $WalkTimeArray[$ReverseStepTracker] = 'W'
                        $ReverseStepTracker --
                    }
                    break
            }
            '2' {
                    if (Test-IfWalkDirectionIsRepeated -ThisDirection 'S')
                    {
                        $i ++
                        $ReverseStepTracker ++
                    }
                    else {
                        $WalkTimeArray[$i]  = 'S'
                        $WalkTimeArray[$ReverseStepTracker] = 'N'
                        $ReverseStepTracker --
                    }
                    break
            }
            '3' {
                    if (Test-IfWalkDirectionIsRepeated -ThisDirection 'W')
                    {
                        $i ++
                        $ReverseStepTracker ++
                    }
                    else {
                        $WalkTimeArray[$i]  = 'W'
                        $WalkTimeArray[$ReverseStepTracker] = 'E'
                        $ReverseStepTracker --
                    }
                    break
            }
        }

    }
    Write-Output $('Your walk path is: {0}' -f ($WalkTimeArray -join ','))
}