<?php


function createQuestions($text, $points, $answers, $correctAnswer) {
    $q = new Question;
    $questions[] = $q;
  
  
    $q->text = $text;
    $q->points = $points;
    $q->answers=$answers;
    $q->correctAnswer=$correctAnswer;
    
    return $questions;
}

function printQuestions($questions)
{
    $i = 1; // номер вопроса

    foreach ($questions as $question) {
        echo "<br>{$i}. {$question->text}<br>";

        echo "Variants of answer:<br>";

        foreach ($question->answers as $letter => $answer) {
            echo "  {$letter}. {$answer}<br>";
        }

        $i++; 
    }
}

function checkAnswers($questions, $answers){
  
    if (count($questions) != count($answers)) {
        die("The amount of questions and answers doesn't fit");
    }

    $pointsTotal = 0; // сколько набрано баллов
    $pointsMax = 0;   // сколько можно набрать баллов при всех правильных ответах
    $correctAnswers = 0; // сколько отвечено верно
    $totalQuestions = count($questions); // Сколько всего вопросов

    // Цикл для обхода вопросов и ответов
    for ($i = 0; $i < count($questions); $i++) {
        $question = $questions[$i]; // Текущий вопрос
        $answer = $answers[$i]; // текущий ответ

        // Считаем максимальную сумму баллов
        $pointsMax += $question->points;

        // Проверяем ответ
        if ($answer == $question->correctAnswer) {
            // Добавляем баллы
            $correctAnswers ++;
            $pointsTotal += $question->points;
        } else {
            // Неправильный ответ
            $number = $i + 1;
            echo "Wrong answer on question №{$number} ({$question->text})<br>";
        }
    }

    // Выведем итог
    echo "Right answers: {$correctAnswers} from {$totalQuestions}, Points: {$pointsTotal} from {$pointsMax} <br>";
        
 
}


$questions = CreateQuestions('Name a town', 5, ['a'=>'Paris', 'b'=>'Moscow', 'c'=>'London'], 'c');

printQuestions($questions);

$questions2 = CreateQuestions('Name an artist', 7, ['a'=>'Sergei', 'b'=>'Danila', 'c'=>'Tolyan'], 'b');

printQuestions($questions2);

checkAnswers($questions, 'c');
checkAnswers($questions2, 'b');