Quizzer Script

#========================================================================
# Function:      Quiz / Flashcards
#                   
# Created On:    1/24/2014
#
# Created By:    John Booth
#========================================================================
# Initialize variables and clear screen before starting
# -----------------------------------------------------------------------
$Selection = 0
$correct = 0
$incorrect = 0
$percent = 0
While ($Selection -ne 6)
{
cls
Write-Host "Your Score"
Write-Host ""
Write-Host "Correct: $correct and Incorrect: $incorrect"
Write-Host ""
$count = 1
$hash = @{}
$displayHash = @{}

# -----------------------------------------------------------------------
# Read in data and get count
# -----------------------------------------------------------------------
$tester = Import-Csv C:\scripts\Input\test.csv
$defCount = $tester.Count -1

# -----------------------------------------------------------------------
# Pick and display a random definition (also put a blank line after)
# -----------------------------------------------------------------------
$pickQuestionNum = Get-Random -minimum 0 -maximum $defCount
$tester.Item($pickQuestionNum).definition
Write-Host ""

# -----------------------------------------------------------------------
# Generate a hash of answer (position 1) and other random options (2-5)
# -----------------------------------------------------------------------
$hash.add(1,$tester.Item($pickQuestionNum).item)
for ($i=1; $i -le 4; $i++)
    {
    $count = $count + 1
    $randomItem = Get-Random -minimum 0 -maximum $defcount
    $hash.add($count,$tester.Item($randomItem).item)
    }

# -----------------------------------------------------------------------
# Reinitialize counters / variables before running the next loop
# -----------------------------------------------------------------------
$count = 1
$order = Get-Random -Count 10 -InputObject (1..5)


# -----------------------------------------------------------------------
# Generate a hash to display on screen with randomized selections
# -----------------------------------------------------------------------
While ($count -lt 6)
    {
    $arrayPos = $count - 1
    $displayHash.Add($order[$arrayPos], $hash.get_item($count))
    $count = $count + 1
    }

$displayHash.GetEnumerator() | Sort Name
Write-Host ""

# -----------------------------------------------------------------------
# Ask user to make their selection
# -----------------------------------------------------------------------
[int]$selection = Read-Host "Please enter your selection (type 6 to EXIT)"
Write-Host ""

# -----------------------------------------------------------------------
# Compare their selection to the correct answer
# -----------------------------------------------------------------------
If (($displayhash.get_item($selection)) -eq ($hash.get_item(1))) 
    {
    Write-Host "That is correct"
    $Correct++
    }
Elseif ($Selection -eq 6)
    {
    Write-Host "Thanks for playing"
    $percent = ($correct + $incorrect)/$correct
    Write-Host "You got $percent% Correct"
    }
Else
    {
    Write-Host "That is incorrect"
    $Incorrect++
    }
}

No comments:

Post a Comment