Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

guessingGame

By: a guest on Feb 10th, 2012  |  syntax: Power Shell  |  size: 0.71 KB  |  views: 124  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #assign our initial variables for the game
  2. [int]$lowerLimit = 0
  3. [int]$upperLimit = 100
  4. [int]$myGuess = 50
  5. [int]$timesRun = 0
  6. [int]$solved = 0
  7.  
  8. While ($solved -eq 0){
  9.         $queryString = "Is your number " +  $myGuess + "? (higher/lower/correct)"
  10.         $response = read-host $queryString
  11.         switch ($response) {
  12.                 "higher" {
  13.                         $lowerLimit = $myGuess
  14.                         $myGuess = ($lowerLimit + $upperLimit) / 2
  15.                         $timesRun++
  16.                 }
  17.                 "lower" {
  18.                         $upperLimit = $myGuess
  19.                         $myGuess = ($lowerLimit + $upperLimit) / 2
  20.                         $timesRun++
  21.                 }
  22.                 "correct" {
  23.                         $timesRun++
  24.                         write-host "Hooray, I won in only $timesRun guesses!"
  25.                         $solved = 1
  26.                 }
  27.                 default {
  28.                         write-host "That is not a valid reply!"
  29.                         write-host ""
  30.                 }
  31.         }
  32.        
  33. }
clone this paste RAW Paste Data