Huddled Masses
The internet home of Joel "Jaykul" Bennett...
Browse: Home / 2008 Scripting Games – Solution for Beginner’s Event 8 (and 7)

2008 Scripting Games – Solution for Beginner’s Event 8 (and 7)

By Joel 'Jaykul' Bennett on 29-Feb-2008

This continues my series of solution posts for the 2008 Scripting Games with my solution for the Beginner’s Event 7 and
Beginner’s Event 8. I’m wrapping them together because beginner’s event 7 was just a debugging exercise so I just pasted the solution at the bottom1.

Event 8 was the age old “Higher or Lower” guessing game: the script picks a random number between 1 and 50 and then lets the user guess at it. After each guess, you tell them whether their guess was low or high, and when they guess it you tell them they got it, and how many guesses it took.


$r = new-object Random
$answr = $r.Next(1,51)
$count = 0

while(++$count) {
  switch($answr.CompareTo([int](Read-Host "Enter a number between 1 and 50"))) {
     1 { "Too Low"  }
    -1 { "Too High" }
     0 { return "The number was: $answr`nIt took you $count guesses" }
  }
}

It’s a pretty simple script, and the only part that I thought was cool was the way I used the while loop to increment the counter (it’s important to PRE-increment it, so it’s not equal to 0 the first time it’s tested). I used the switch statement so that I wouldn’t need to store the user’s guess. Nothing to be proud of, nothing to really point out. If you didn’t know how to get a random number in .Net, the Next method of the Random object takes a minimum (inclusive) and maximum (exclusive).

1 The only interesting thing in DebugMe.ps1 was that there’s a possible extra bug because the script uses the variable $x without initializing it or taking it as a parameter — $x is a really popular variable ;-P and not initializing it could result in really strange results from this script. I fixed it (within the scope of the rules: only modify existing lines) by explicitly stating the script scope:


foreach($i in Get-ChildItem C:\Scripts -recurse)
{
    if ( ($i.CreationTime -lt ((Get-Date).AddDays(-10))) -and ($i.Extension -eq ".txt") )
    {
         Copy-Item $i.FullName C:\old -whatif
         $i.Name
         $script:x = $script:x + 1
    }
}

""
"Total Files: " + $script:x

Similar Posts:

  • What Scope Am I In?
  • PowerShell 3 – Finally on the DLR!
  • How to Import Binary Modules from Network Shares
  • Parenthesis in PowerShell
  • Arrange – Act – Assert: Intuitive Testing

Posted in Huddled | Tagged 2008 Scripting Games, PowerShell, Scripting, Solutions

« Previous Next »

Lijit Search

Tags

.Net .Net 2008 Scripting Games Automation Bugs Design Development Funny Gadgets GeoShell GUI Huddled Masses Internet licensing Microsoft Modules My Software News Personal PInvoke Pipeline Politics PoshCode PoshConsole PowerBoots PowerShell PowerShell Functions PowerTips Rants Recommender Repository Scripting ShowUI Software Solutions Textile Tips User Group UserInterface WalkThrough WebHosting Windows 7 WordPress WPF Xml

About Huddled Masses

This is web site is dedicated to the musings of Joel Bennett (aka Jaykul) about technology, software, software development, the web, and the world.

Any resemblance of the views expressed and the views of my employer, my terminal, or the view out my window are purely coincidental. The resemblance between them and my own views is non-deterministic. The question of the existence of views in the absence of anyone to hold them is left as an exercise for the reader.

P.S.: I occasionally link to things I think are great. When I do, I occasionally find a "referral code" so I can make a little cash. I promise that I don't link to anything just because of that cash (I wouldn't cross the street for the amount of cash those links bring in, never mind write a whole blog post) ... but I do not promise that things I link to will stay great as time passes, nor that you will agree with me about their greatness!

Archives

  • April 2012
  • February 2012
  • January 2012
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • March 2011
  • February 2011
  • January 2011

Copyright © 2012 Joel Bennett.

Powered by WordPress and Hybrid.