Advertisement
Guest User

guessingGame

a guest
Feb 10th, 2012
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement