Solved

Guess my number game

Posted on 2014-01-05
3
1,056 Views
Last Modified: 2014-02-02
I am trying to modify a script to when a user selects Q for quit, the game says I have quit and then shows statistics.  Which I believe I have working correctly, however, for some reason I am missing the closing loop or something.  Can anyone see what I missed?

#Clear the Windows command console screen
Clear-Host

#Define variables used in this script
$number = 0       #Keeps track of the game's secret number
$noOfGuesses = 0  #Keeps track of the number of guesses made
$randomNo = New-Object System.Random  #This variable stores a random object
$playGame = "Yes" #Controls when to quit the game
$status = "Play"  #Controls the current round of play
$guess = 0        #Stores the player's guess
$reply = ""       #Stores the player's response when asked to play again

#Display the game's opening screen
Write-Host "`n`n`n`n`t     W E L C O M E   T O   T H E   G U E S S   M Y"
Write-Host "`n`n`n`t`t`tN U M B E R   G A M E"
Write-Host "`n`n`n`n`n`n`n`n`n`n Press Enter to continue."

#Pause the game until the player presses the Enter key
Read-Host

#Loop until the player decides to quit the game
while ($playGame -ne "No") {

  #Generate the game's random number (between 1 - 100)
  $number = $randomNo.Next(1, 101)

  #Clear the Windows command console screen
  Clear-Host

  #Loop until the player guesses the secret number
  while ($status -ne "Stop") {

    #Prompt the player to guess a number
    while ($guess -eq "") {

      Clear-Host  #Clear the Windows command console screen

      Write-Host

      #Collect the player's guess
      $guess = Read-Host " Enter a number between 1 and 100 or type Q to quit"

    }

    #Keep track of the number of guesses made so far
    $noOfGuesses++
    
    if($guess -eq "Q") { #The player decided to quit this game
        Clear-Host
        Write-Host "`n`n"
        Write-Host " Game is now over, thanks for play Guess My Number!"
        Write-Host "`n`n`n`n`n`n`n`n`n`n`n`n`n`n`n`n"
        Write-Host " Press ENTER to view game stats and quit the game."
        
        Read-Host #Pause for the player
        
        $playGame = "No" #Variable to indicate game is over
        
        Write-Host "`n Game Statistics"
        Write-Host " ------------------------------------------------------------"
        Write-Host "`n You guessed $noOfGuesses times.`n"
        Write-Host " ------------------------------------------------------------"
        Write-Host "`n`n`n`n`n`n`n`n`n`n`n`n`n`n Press Enter to continue."

  #Pause the game until the player presses the Enter key
  Read-Host

  #Clear the Windows command console screen
  Clear-Host

    continue
    
    }
    if ($guess -lt $number) {  #The player's guess was too low

      Clear-Host  #Clear the Windows command console screen
      Write-Host "`n Sorry. Your guess was too low. Press Enter to" `
        "guess again."
      $guess = ""  #Reset the player's guess
      Read-Host  #Pause the game until the player presses the Enter key

    }
    elseif ($guess -gt $number) {  #The player's guess was to high

      Clear-Host  #Clear the Windows command console screen
      Write-Host "`n Sorry. Your guess was too high. Press Enter to" `
        "guess again."
      $guess = ""  #Reset the player's guess
      Read-Host  #Pause the game until the player presses the Enter key

    }
    else {  #The player has guessed the game's secret number

      Clear-Host  #Clear the Windows command console screen
      Write-Host "`n Congratulations. You guessed my number! Press Enter" `
        "to continue."
      $status = "Stop"  #Reset the player's guess
      Read-Host  #Pause the game until the player presses the Enter key

    }

  }

  #Clear the Windows command console screen
  Clear-Host

  #Display the game's opening screen
  Write-Host "`n Game Statistics"
  Write-Host " ------------------------------------------------------------"
  Write-Host "`n The secret number was: $number."
  Write-Host "`n You guessed it in $noOfGuesses guesses.`n"
  Write-Host " ------------------------------------------------------------"
  Write-Host "`n`n`n`n`n`n`n`n`n`n`n`n`n`n Press Enter to continue."

  #Pause the game until the player presses the Enter key
  Read-Host

  #Clear the Windows command console screen
  Clear-Host

  $reply = ""  #Stores the player's response when asked to play again

  #Prompt the player to play another round
  while ($reply -eq "") {

    Clear-Host  #Clear the Windows command console screen

    Write-Host

    #Collect the player's answer
    $reply = Read-Host " Would you like to play again? (Y/N) "

     
    #Validate player input, allowing only Y and N as acceptable responses
    if (($reply -ne "Y") -and ($reply -ne "N")) {

      $reply = ""  #Reset the variable to its default value

    }

  }

  #The player has elected to play again
  if ($reply -eq "Y") {

    #Reset variables to their default values
    $number = 0
    $noOfGuesses = 0
    $status = "Play"
    $guess = 0

  }
  else {   #The player has decided to quit playing

    $playGame = "No"  #Modify variable indicating that it is time to 
                      #terminate game play

  }  

}

#Clear the Windows command console screen
Clear-Host

Open in new window

0
Comment
Question by:tomtom9898
3 Comments
 
LVL 5

Expert Comment

by:Dontmilkthis
Comment Utility
My first guess would be to change the 'continue' on line 71 to an 'Exit'.

There is other options to achieve the goal of ending the script, but this is easiest.

you'd have to set $status to "Stop" at line 72 and then some more if statements after the while loop to determine if it was stopped due to guessing the number or stopped due to wanting to quit.

this gives you the option of returning to the welcome screen if you so desire. the first 'Exit' option just closes the script.
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
There's probably other things I would change, but the main problem you're facing now is on line 71.
On 71 you should use break instead of continue.
With the continue statement you just move on to the next iteration of the loop that you are in.  Just an FYI, you can also break out of parent loops if you label the loop (see the about_break help). Other issues I would say is the count is one too many, and even when you quit it says you guessed the number.

One last piece of advice, when troubleshooting, remove or comment out all the clear-host commands, they just make it harder to see what is happening and what effect any changes you have made have had.
0
 

Author Closing Comment

by:tomtom9898
Comment Utility
Thanks for the help!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now