Build a Number Line

A year or two ago, I wrote my one and only game in Windows PowerShell. It’s 1 to 100; maybe you played it as a kid. The computer chooses a random number between 1 to 100. You make guesses and every time you’re wrong, the computer will tell you if you should guess higher, or lower, on your next turn. This continues until you guess the correct number. Once you’re a winner, it’ll display the number of attempts it took, the number of games played, and your average attempts per game. Here’s an image that shows that it only took me three guesses (the average, over time, seems to be around 6 to 7).

build-a-number-line01

In the last few months, my daughter has begun to occasionally ask me to play. She’s a smart, little 4-year-old, but the more she plays, the more I wish I had built in an easier mode, or two. Those may be coming.

As we were sitting there last night, she mentioned that she couldn’t find her number line. I had drawn one a few months ago that she’d use to help determine the range of numbers she should choose from next. Well, it went missing. As I try to solve all my worldly problems with PowerShell, I threw together a quick function that would allow us to draw a quick number line in a separate console. Now, she could easily locate a number in the middle of her closest high number and closest low number. Here’s the function, now.

Function Show-NumberLine {
    Param ([int]$Num1, [int]$Num2)
    $Num1..$Num2 | ForEach-Object {$NumberLine += "$_-"}
    "<--$NumberLine->"
}

Show-NumberLine -Num1 1 -Num2 20
<--1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-->
Show-NumberLine -Num1 1 -Num2 10
<--1-2-3-4-5-6-7-8-9-10-->

One of these days, I’ll add to the 1 to 100 function, and enable an easier mode, or two, to perhaps include the number line for beginner users. In the meantime, you can download the function here: https://gallery.technet.microsoft.com/The-1-to-100-Game-5288e279 and use number line function separately, if you need it. 😉

Leave a Reply

Your email address will not be published.