Yeni Baslayanlar Icin PowerShell Scriptleri – 5 – Basit Bir Quiz Oyunu

Yeni bir script daha!
Bu sefer de online bir soru cevap uygulamasinin sundugu API sayesinde rastgele sorulardan olusan bir set hazirlayip karsidakine sorular soracagiz. Onun verdigi yanitlara gore de dogru ve yanlis cevap sayisini ekrana yazdiracagiz. Yine birden fazla faydali function, method ve cmdlet kullanacagiz.
[crayon]
Clear-Host
$uri = “https://opentdb.com/api.php?amount=3&category=18&difficulty=medium&type=multiple”
$sorular = (Invoke-RestMethod -Method GET -ContentType html -Uri $uri).results
$senecekler = @()
$durum=$true
$dogrular = 0
$yanlislar = 0
foreach($soru in $sorular){
write-Host $soru.category -ForegroundColor Green
write-host “——————” -ForegroundColor Green
write-host “”
Write-Host $soru.question -ForegroundColor Yellow
write-host “”
$secenekler = $soru.incorrect_answers
$secenekler += $soru.correct_answer
$secenekler = $secenekler | Sort-Object {get-random}
$secenekler
write-host “”
$yanit = read-host “hangisi dogru? 1,2,3,4 ?”
if($secenekler[$yanit-1] -eq $soru.correct_answer){
write-host “”
write-host “bravo!, diger soruya geciliyor” -ForegroundColor Green
sleep 2
Clear-Host
$dogrular++
}else{
write-host “”
write-host “uzgunum, yanlis :( , diger soruya geciliyor” -ForegroundColor Red
sleep 2
$yanlislar++
Clear-Host
}
}
Clear-Host
write-host “Dogrular” -ForegroundColor Green
write-host “——–” -ForegroundColor Green
write-host $dogrular -ForegroundColor Green
write-host “”
write-host “Yanlislar” -ForegroundColor Red
write-host “——–” -ForegroundColor Red
write-host $yanlislar -ForegroundColor Red
[/crayon]

Leave a Reply