Hey all,

Just started learning powershell and decided the best way to learn was to make a few little games etc.

The following is the fourth one I’ve made and works up until the point it tries to call the data from variables.

It misses out the whole grading and ranking process and skips straight to the end screen.

Any idea’s where I’m going wrong here?

Thanks in advance

#*************************************************************************************************************

Script Name: LotrQuiz.ps1 (Lord of the Rings Trivia Quiz)

Version: 1.0

Author: Peri K Morgan

Date: 24/01/2017

Description: This PowerShell script tests the players knowledge of

The Lord of the Rings trivia through the administration

of a computer quiz made up of 5 questions.

#**************************************************************************************************************

#clear screen
cls

#set write-host as ds
set-alias ds write-host

#define variables used to store player answers
$question1 = “”
$question2 = “”
$question3 = “”
$question4 = “”
$question5 = “”

#define variable to keep track of correct answers
$noCorrect = 0

#display the games opening screen
ds “nnntt WELCOME TO THE" ds "nntt LORD OF THE RINGS" ds "nntt TRIVIA QUIZ" ds "nnntt By P K Morgan”
ds “nnnnnt`t Press Enter To Continue”

#pause script
read-host

#clear screen
cls

#Provide the player with instructions
ds “nn The Lord of the Rings trivia quiz tests your knowledge of n" ds "Lord of the Rings trivia. The quiz consists of 5 multiple choice n”
ds “questions. At the end of the quiz, your answers will be checked and n" ds "you will be assigned a skill level using the following scale. n`n”

ds “tt Score: 5 correct = Gandalf (master)”
ds “tt Score: 4 correct = Aragorn”
ds “tt Score: 3 correct = Legolas”
ds “tt Score: 2 correct = Gimli”
ds “tt Score: 1 correct = Frodo”
ds “tt Score: 0 correct = Samwise (Clueless)”

ds “nnnn`n Press Enter To Continue”

#pause script
read-host

#ask the first question
while (($question1 -ne “a”) -and ($question1 -ne “b”) -and ($question1 -ne “c”) -and ($question1 -ne “d”)) {

cls #clear screen

ds
ds “What is the name of the character, that helps the Hobbits escape the old forest?”
ds
ds “A. Mithrandir”
ds “B. Faramir”
ds “C. Tom Bombadil”
ds “D. Ham Gamgee”
ds
$question1 = read-host “Type the letter representing the correct answer, and press the Enter key.”
}

#clear screen
cls

#second question
while (($question2 -ne “a”) -and ($question2 -ne “b”) -and ($question2 -ne “c”) -and ($question2 -ne “d”)) {

cls #clear screen

ds
ds “What is the name of the first landmark the Hobbits visit?”
ds
ds “A. Weathertop”
ds “B. The Prancing Pony”
ds “C. Rivendell”
ds “D. The Barrow Downs”
ds
$question2 = read-host “Type the letter representing the correct answer, and press the Enter key.”
}

#clear screen
cls

#third question
while (($question3 -ne “a”) -and ($question3 -ne “b”) -and ($question3 -ne “c”) -and ($question3 -ne “d”)) {

cls #clear screen

ds
ds “What is Aragorn called by the men of Bree?”
ds
ds “A. Strinder”
ds “B. Longshanks”
ds “C. Ranger”
ds “D. The Dunedain”
ds
$question3 = read-host “Type the letter representing the correct answer, and press the Enter key.”
}

#clear screen
cls

#forth question
while (($question4 -ne “a”) -and ($question4 -ne “b”) -and ($question4 -ne “c”) -and ($question4 -ne “d”)) {

cls #clear screen

ds
ds “What is the name given to the King of the Wood Elves?”
ds
ds “A. Elrond”
ds “B. Thranduil”
ds “C. Glorfindel”
ds “D. Hunduin”
ds
$question4 = read-host “Type the letter representing the correct answer, and press the Enter key.”
}

#clear screen
cls

#final question
while (($question5 -ne “a”) -and ($question5 -ne “b”) -and ($question5 -ne “c”) -and ($question5 -ne “d”)) {

cls #clear screen

ds
ds “What is the correct name to describe Gandalf, Radaghast and Saruman?”
ds
ds “A. Sorcerers”
ds “B. Wizards”
ds “C. Warlocks”
ds “D. Istari”
ds
$question5 = read-host “Type the letter representing the correct answer, and press the Enter key.”
}

#end game

#clear screen
cls

ds
ds “OK, now press enter to see how you did!”

#pause script
read-host

#clear screen
cls

#grade the answers for each question
if ( $question1 -eq “c” ) { $noCorrect ++ } #Correct answer is C
if ( $question2 -eq “d” ) { $noCorrect ++ } #Correct answer is D
if ( $question3 -eq “a” ) { $noCorrect ++ } #Correct answer is A
if ( $question4 -eq “b” ) { $noCorrect ++ } #Correct answer is B
if ( $question5 -eq “d” ) { $noCorrect ++ } #Correct answer is D

#assign a ranking based on quiz score.
if ($noCorrect -eq 0) {
ds “n You didn't get any questions correct." ds "n`n Your knowledge of the world is no better than Samwise’s.”
}

if ($noCorrect -eq 1) {
ds “n You got 1 question correct." ds "n Like Frodo, you know a little about the world.”
}

if ($noCorrect -eq 2) {
ds “n You got 2 questions correct." ds "n You have done a small amount of travel, not unlike Gimli prior to the forming of the fellowship.”
}

if ($noCorrect -eq 3) {
ds “n You got 3 questions correct." ds "n Suilad, Mae govannan mellon! You are just like Legolas and possess a deep understanding that comes with lonng years.”
}

if ($noCorrect -eq 4) {
ds “n You got 4 correct." ds "n Long study of your ancestry have left you as knowledgable as Aragorn.”
}

if ($noCorrect -eq 5) {
ds “n Congratulation, you got 5 correct." ds "n Are you sure you’re not akin to Gandalf? You clearly possess the knowledge of one of the Istari!”
}

#clear script
cls

#End screen
ds “n Thank you for playing n”
ds “The Lord of the Rings Trivia Quiz.”
ds “`n Press Enter to Exit”

#pause script
read-host

#clear screen
cls

1 Spice up

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton2.pngAlso please have a read here regarding rather long scripts.

Why did you choose to use an alias for write host? I was confused when I was ‘DS’ but i finally saw on the top you set the alias. especially since you are new to Powershell it helps a lot if you keep stuff as original as possible, no abbreviations and shortcuts.

I’m glad it wasn’t just me. Also replace all of those if statements concerning the score with a switch statement.

The trick is to avoid hardcoding for data. Then you can’t ever reuse the script. Instead think about creating a dataset and then having the script react to the data instead of making sequential actions. Think loops.

1 Spice up

Thanks, guys, I will definitely follow the guidelines in the first post and will need to do a bit more reading/learning in regards to the switch statement and loops.

As I said, I am new to PowerShell having never really done any scripting before (little bit of JavaScript back when I learned html/css)

Once again thanks for the great advice.