<?php

error_reporting(E_ALL);

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

	abstract function getAsString();
	abstract function checkAnswer($answer);
}

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

	public function __construct($text, float $points, array $answers, $correctAnswer, $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 = "{$this->text}\nВарианты ответов:\n";

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

		return $string;
	}

	public function checkAnswer($answer)
	{
		$points = $this->points;
		$text = $this->text;
		$hint = $this->hint;

		if ($answer == $this->correctAnswer) {
			return array(1, $points);

		} elseif ($answer == $this->almostCorrectAnswer) {
			return array(1, $points, '', '', 1);

		}

		return array(0, $points, $text, $hint);
	}

}

class NumericalQuestion extends Question
{
	protected $deviation;

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

	public function getAsString()
	{
		return "{$this->text}\n";
	}

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

		if ($answer == $this->correctAnswer) {
			return array(1, $points);

		} elseif ($this->deviation > 0) {

			$leftDeviation = $this->correctAnswer - $this->deviation;
			$rightDeviation = $this->correctAnswer + $this->deviation;

			if ($leftDeviation < $answer and $rightDeviation > $answer) {
				return array(1, $points);
			}

		}

        return array(0, $points, $text);
	}

}

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, 300000);

	$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];

        $resultOfCheck = $question->checkAnswer($answer);

        $pointsMax += $resultOfCheck[1];

        if ($resultOfCheck[0]) {
            $correctAnswers ++;

            if (!empty($resultOfCheck[4])) {
            	$pointsTotal += $resultOfCheck[1] / 2;
            } else {
            	$pointsTotal += $resultOfCheck[1];
            }

        } else {
            $number = $i + 1;
            echo "\nНеправильный ответ на вопрос №{$number} ({$resultOfCheck[2]})\n";

            if (!empty($resultOfCheck[3])) {
            	echo "\nПодсказка: {$resultOfCheck[3]}\n";
            }

        }

    }

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

$questions = createQuestions();
printQuestions($questions);
checkAnswers($questions, array('b', 'c', 'b', 300000, 3.14444444, 1945));