Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell scripting help

Posted on 2013-12-14
3
Medium Priority
?
403 Views
Last Modified: 2013-12-15
I am trying to make a fortune game decide answers at random, but be mad if someone asks something about the lottery.  I am very close, but just can't seem to finish this up.  What am I missing?

#Clear the Windows command console screen
Clear-Host

#Define the variables used in this script to collect player inputs
$question = ""   #This variables will store the player's question
$status = "Play"  #This variable will be used to control game termination
$randomNo = New-Object System.Random  #This variable stores a random object
$answer = 0  #This variable stores a randomly genrated number
$time = (Get-Date).Hour  #This variable stores the current hour of the day


#Display the game's opening screen
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host "               W E L C O M E   T O   T H E   W I N D O W S"
Write-Host
Write-Host
Write-Host
Write-Host "            P O W E R S H E L L   F O R T U N E   T E L L E R"
Write-Host
Write-Host
Write-Host   
Write-Host "                         
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host " Press Enter to continue."

#Pause script execution and wait for the player to press the Enter key
Read-Host


#Clear the Windows command console screen
Clear-Host  

#Provide the player with instructions
Write-Host
Write-Host " The fortune teller is a very busy and impatient mystic. Make"
Write-Host 
Write-Host " your questions brief and simple and only expect to receive"
Write-Host
Write-host " Yes / No styled answers. Also do not ask me if you will win"   
Write-Host 
Write-Host " the lottery or not.  I am tired of that one"
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host " Press Enter to continue."

#Pause script execution and wait for the player to press the Enter key
Read-Host

#Continue game play until the player decides to stop
while ($status -ne "Stop") {

  #Ask the player the first question
  while ($question -eq ""){

    Clear-Host  #Clear the Windows command console screen

    Write-Host

    $question = read-host " What is your question? I hate questions about the lottery! "
 
  }
  $question = ""  #Reset variable to a empty string


  #Using the Random object, get a random number between 1 and 4
  If ($answer = "lottery") { "What the heck are you doing?!?!?! I told you I hate lottery questions! Please restate the question."}
  else { 
  $answer = $randomNo.Next(1, 5)}
  
  #Select an answer based on the time and random number
  #If it is the afternoon the fortune teller will be a little cranky
  if ($time -gt 12) {
    Write-Host
    if ($answer -eq 1) { " Grrrr. The answer is no!" }
    if ($answer -eq 2) { " Grrrr. The answer is never" }
    if ($answer -eq 3) { " Grrrr. The answer is unclear!" }
    if ($answer -eq 4) { " Grrrr. The answer is yes!" }
  }
  #If it is morning the fortune teller will be in a good mood
  else {
    Write-Host
    if ($answer -eq 1) { " Ah. The answer is yes!" }
    if ($answer -eq 2) { " Ah. The answer is Always" }
    if ($answer -eq 3) { " Ah. The answer is uncertain!" }
    if ($answer -eq 4) { " Ah. The answer is no!" }
  }}


  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host " Press Enter to continue."

  #Pause script execution and wait for the player to press the Enter key
  Read-Host

  #Clear the Windows command console screen
  Clear-host

  Write-Host

  #Prompt the player to continue or quit
  $reply = read-host " Press Enter to ask another question or type Q to quit."
  if ($reply -eq "q") { $status = "Stop" }



#Clear the Windows command console screen
Clear-Host

#Provide the player with instructions
Write-Host 
Write-Host " Very well then. Please return again to get all your questions"
Write-Host " answered." 
Write-Host
Write-Host 
Write-Host
Write-Host
Write-Host
Write-Host 
Write-Host
Write-Host
Write-Host  
Write-Host 
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host 
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host " Press Enter to continue."

#Pause script execution and wait for the player to press the Enter key
Read-Host

#Clear the Windows command console screen
Clear-Host  

Open in new window

0
Comment
Question by:tomtom9898
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Accepted Solution

by:
Dale Harris earned 2000 total points
ID: 39719513
Here's some minor corrections:

#Clear the Windows command console screen
Clear-Host

#Define the variables used in this script to collect player inputs
$question = ""   #This variables will store the player's question
$status = "Play"  #This variable will be used to control game termination
$randomNo = New-Object System.Random  #This variable stores a random object
$answer = 0  #This variable stores a randomly genrated number
$time = (Get-Date).Hour  #This variable stores the current hour of the day


#Display the game's opening screen
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host "               W E L C O M E   T O   T H E   W I N D O W S"
Write-Host
Write-Host
Write-Host
Write-Host "            P O W E R S H E L L   F O R T U N E   T E L L E R"
Write-Host
Write-Host
Write-Host   
Write-Host                       
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host " Press Enter to continue."

#Pause script execution and wait for the player to press the Enter key
Read-Host


#Clear the Windows command console screen
Clear-Host  

#Provide the player with instructions
Write-Host
Write-Host " The fortune teller is a very busy and impatient mystic. Make"
Write-Host 
Write-Host " your questions brief and simple and only expect to receive"
Write-Host
Write-host " Yes / No styled answers. Also do not ask me if you will win"   
Write-Host 
Write-Host " the lottery or not.  I am tired of that one"
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host " Press Enter to continue."

#Pause script execution and wait for the player to press the Enter key
Read-Host

#Continue game play until the player decides to stop
while ($status -ne "Stop") {

  #Ask the player the first question
  while ($question -eq ""){

    Clear-Host  #Clear the Windows command console screen

    Write-Host

    $question = read-host " What is your question? I hate questions about the lottery! "
 
  }
  


  #Using the Random object, get a random number between 1 and 4
  If ($question.contains("lottery")) { "What the heck are you doing?!?!?! I told you I hate lottery questions! Please restate the question."}
  else { 
  $answer = $randomNo.Next(1, 5)}
  
  #Select an answer based on the time and random number
  #If it is the afternoon the fortune teller will be a little cranky
  if ($time -gt 12) {
    Write-Host
    if ($answer -eq 1) { " Grrrr. The answer is no!" }
    if ($answer -eq 2) { " Grrrr. The answer is never" }
    if ($answer -eq 3) { " Grrrr. The answer is unclear!" }
    if ($answer -eq 4) { " Grrrr. The answer is yes!" }
  }
  #If it is morning the fortune teller will be in a good mood
  else {
    Write-Host
    if ($answer -eq 1) { " Ah. The answer is yes!" }
    if ($answer -eq 2) { " Ah. The answer is Always" }
    if ($answer -eq 3) { " Ah. The answer is uncertain!" }
    if ($answer -eq 4) { " Ah. The answer is no!" }
  }


  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host
  Write-Host " Press Enter to continue."

  #Pause script execution and wait for the player to press the Enter key
  Read-Host

  #Clear the Windows command console screen
  Clear-host

  Write-Host

  #Prompt the player to continue or quit
  $reply = read-host " Press Enter to ask another question or type Q to quit."
  if ($reply -eq "q") { $status = "Stop" }
	$question = ""  #Reset variable to a empty string
}#end the first while loop


#Clear the Windows command console screen
Clear-Host

#Provide the player with instructions
Write-Host 
Write-Host " Very well then. Please return again to get all your questions"
Write-Host " answered." 
Write-Host
Write-Host 
Write-Host
Write-Host
Write-Host
Write-Host 
Write-Host
Write-Host
Write-Host  
Write-Host 
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host 
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host " Press Enter to continue."

#Pause script execution and wait for the player to press the Enter key
Read-Host

#Clear the Windows command console screen
Clear-Host  

Open in new window


This now works.  You had the question turning back into "" right after asking a question, and you were closing the loop too soon.  Overall, great script!  I enjoyed it!

Regards,

Dale Harris
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39719950
Gosh. The answer is never!
May I suggest some improvements? For example, I  would do the mood stuff more like this:
    #If it is morning the fortune teller will be in a good mood
    #If it is the afternoon the fortune teller will be a little cranky
    $mood =  if ($time -gt 12) { ' Grrrr.' } else { ' Ah.' }
    $text = @('no!', 'never', 'unclear!', 'yes!')[$answer-1]
    Write-Host "$mood The answer is $text"

Open in new window

And instead of writing several lines with Write-Host, I would do
Write-Host @"

This is a line

Another line
"@

Open in new window

0
 

Author Closing Comment

by:tomtom9898
ID: 39720044
Fantastic, knew I was missing something!  Thanks again for the help!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 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