<?php

error_reporting(E_ALL);

abstract class Question
{
	protected $text;
	protected $points;
	protected $correctAnswer;

	abstract function getAsString(): string;
	abstract function checkAnswer($answer);

	public function getQuestionText()
	{
		$text = $this->text;
		return $text;
	}
}

class MultipleChoiceQuestion extends Question
{
    protected $answers;
    protected $almostCorrectAnswer;
    protected $hint;

	public function __construct(string $text, float $points, array $answers, string $correctAnswer, string $hint)
	{
		$this->text = $text;
		$this->points = $points;
		$this->answers = $answers;
		$this->correctAnswer = $correctAnswer;
		$this->hint = $hint;
	}

	/* Функция получает на вход почти корректный вопрос и добавляет его в соответвующее свойство */
	public function addAlmostCorrectAnswer($almostCorrectAnswer)
	{
		$this->almostCorrectAnswer = $almostCorrectAnswer;
	}

	/* Функция отдает строку со списком вопросов */
	public function getAsString(): string
	{
		$string = "{$this->text}\nВарианты ответов:\n";

		foreach ($this->answers as $letter => $answer) {
			$string = $string."{$letter}. {$answer}\n";
		}

		return $string;
	}

	/*

	Функция получает на вход ответ. Проверяет его на правильность с учетом почти корректного варианта.
	Отдает в массив результаты проверки с количеством баллов.

	*/
	public function checkAnswer($answer)
	{
		$points = $this->points;

		$isCorrect = 0;
		$isAlmostCorrect = 0;

		if ($answer == $this->correctAnswer) {
			$isCorrect = 1;
		} elseif ($answer == $this->almostCorrectAnswer) {
			$isAlmostCorrect = 1;
		}

		return array($isCorrect, $isAlmostCorrect, $points);
	}

	/* Функция отдает подсказку к вопросу */
	public function getQuestionHint()
	{
		$hint = $this->hint;
		return $hint;
	}

}

class NumericalQuestion extends Question
{
	protected $deviation;

	function __construct(string $text, float $points, float $correctAnswer, float $deviation = 0)
	{
		$this->text = $text;
		$this->points = $points;
		$this->correctAnswer = $correctAnswer;
		$this->deviation = $deviation;
	}

	/* Функция отдает строку со списком вопросов */
	public function getAsString(): string
	{
		return "{$this->text}\n";
	}

	/*

	Функция получает на вход ответ. Проверяет его с учетом возможного отклонения.
	Отдает в массив результат проверки и количество баллов.

	*/
	public function checkAnswer($answer)
	{
		$points = $this->points;

		$isCorrect = 0;

		if ($answer == $this->correctAnswer or $this->deviation > abs($this->correctAnswer - $answer)) {
			$isCorrect = 1;
		}

        return array($isCorrect, $isAlmostCorrect = 0, $points);
	}

}

/* Функция создающая массив с вопросами */
function createQuestions() {
	$questions = [];

	$text = 'Какая планета располагается четвертой по счету от Солнца?';
	$answers = array('a' => 'Венера', 'b' => 'Марс', 'c' => 'Юпитер', 'd' => 'Меркурий');
	$hint = 'Одноименное название носит шоколадный батончик.';
	$q = new MultipleChoiceQuestion($text, 10, $answers, 'b', $hint);

	$questions[] = $q;

	$text = 'Какой город является столицей Великобритании?';
	$answers = array('a' => 'Париж', 'b' => 'Москва', 'c' => 'Нью-Йорк', 'd' => 'Лондон');
	$hint = '%Городнейм% из кэпитал оф грейт британ.';
	$q = new MultipleChoiceQuestion($text, 5, $answers, 'd', $hint);

	$questions[] = $q;

	$text = 'Кто придумал теорию относительности?';
	$answers = array('a' => 'Джон Леннон', 'b' => 'Джим Моррисон', 'c' => 'Альберт Эйнштейн', 'd' => 'Исаак Ньютон');
	$hint = 'Этим парнем был...';
	$q = new MultipleChoiceQuestion($text, 30, $answers, 'c', $hint);
	$q->addAlmostCorrectAnswer('b');

	$questions[] = $q;

	$q = new NumericalQuestion('Чему равна скорость света в км/с?', 15, 299792, 210);

	$questions[] = $q;

	$q = new NumericalQuestion('Чему равно число Пи?', 30, 3.14, 0.01);

	$questions[] = $q;

	$q = new NumericalQuestion('В каком году закончилась вторая мировая война?', 10, 1945);

	$questions[] = $q;

	return $questions;
}

/* Функция выводящая список вопросов с вариантами ответов */
function printQuestions($questions) {

	$number = 1;

	foreach ($questions as $question) {
		echo "\n{$number}. ";
		echo $question->getAsString();

		$number ++;
	}

}

/*

Функция получает на вход массив вопросов и массив ответов. Проверяет ответы, 
считает число баллов и выводит вопросы, на которые дан неправильный ответ.

*/
function checkAnswers($questions, $answers)
{
    if (count($questions) != count($answers)) {
        die("Число ответов и вопросов не совпадает\n");
    }

    $pointsTotal = 0;
    $pointsMax = 0;
    $correctAnswers = 0;

    $totalQuestions = count($questions);

    for ($i = 0; $i < count($questions); $i++) {

        $question = $questions[$i];
        $answer = $answers[$i];

        list($isCorrect, $isAlmostCorrect, $points) = $question->checkAnswer($answer);

        $pointsMax += $points;

        if ($isCorrect) {

            $correctAnswers ++;
            $pointsTotal += $points;

        } elseif ($isAlmostCorrect) {

        	$correctAnswers ++;
            $pointsTotal += $points / 2;

        } else {

            $number = $i + 1;
            echo "\nНеправильный ответ на вопрос №{$number} ({$question->getQuestionText()})\n";

            if (method_exists($question, 'getQuestionHint')) {
            	echo "\nПодсказка: {$question->getQuestionHint()}\n";
            }

        }

    }

    echo "\nПравильных ответов: {$correctAnswers} из {$totalQuestions}, баллов набрано: $pointsTotal из $pointsMax\n";
}

$questions = createQuestions();
printQuestions($questions);
checkAnswers($questions, array('b', 'd', 'b', 300000, 3.14444, 1944));