<?php
abstract class AbstractQuestion
{
public $text;
public $points;
abstract function checkAnswer($answer);
abstract function printQuestion();
}
class ChoiceQuestion extends AbstractQuestion
{
public $points = 5;
public $options;
public $correctOption;
public $tip;
public function checkAnswer($answer)
{
if ($answer == $this->correctOption) {
echo "Правильный ответ!\n\n";
return $this->points;
} else {
echo "Неправильный ответ.\n";
echo "Подсказка: {$this->tip}\n\n";
return 0;
}
}
public function printQuestion()
{
echo "{$this->text} Стоимость вопроса - {$this->points}.\n";
echo "Варианты ответов:\n";
foreach($this->options as $char=>$word) {
echo "{$char}) {$word}\n";
}
}
}
class NumericQuestion extends AbstractQuestion
{
public $points = 10;
public $answer;
public $deviation;
public function checkAnswer($answer)
{
if ($answer >= $this->answer - $this->deviation && $answer <= $this->answer + $this->deviation) {
echo "Правильный ответ!\n\n";
return $this->points;
} else {
echo "Неправильный ответ.\n";
echo "Правильный ответ: $this->answer\n\n";
return 0;
}
}
public function printQuestion()
{
echo "{$this->text} Стоимость вопроса - {$this->points}.\n";
}
}
function createQuestions()
{
$q = new ChoiceQuestion;
$q->text = "Вторая планета от Солнца?";
$q->points = 10;
$q->options = array('a' => 'Марс','b' => 'Венера','c' => 'Сатурн','d' => 'Юпитер'); $q->correctOption = 'b';
$q->tip = "Богиня любви.";
$questions[] = $q;
$q = new ChoiceQuestion;
$q->text = 'Какой город является столицей Великобритании?';
$q->points = 5;
$q->options = array('a' => 'Париж', 'b' => 'Москва', 'c' => 'Нью-Йорк', 'd' => 'Лондон'); $q->correctOption = 'd';
$q->tip = "Двухэтажные красные автобусы, Биг-Бен.";
$questions[] = $q;
$q = new ChoiceQuestion;
$q->text = 'Кто придумал теорию относительности?';
$q->points = 30;
$q->options = array('a' => 'Джон Леннон', 'b' => 'Джим Моррисон', 'c' => 'Альберт Эйнштейн', 'd' => 'Исаак Ньютон'); $q->correctOption = 'c';
$q->tip = "Этим человеком был...";
$questions[] = $q;
$q = new NumericQuestion;
$q->text = 'Чему равна скорость света в КМ/с ?';
$q->points = 20;
$q->answer = 299792.458;
$q->deviation = 10.5;
$questions[] = $q;
$q = new NumericQuestion;
$q->text = 'Чему равняется число Пи (точность - пять знаков после запятой)?';
$q->points = 10;
$q->answer = 3.14159;
$q->deviation = 0;
$questions[] = $q;
$q = new NumericQuestion;
$q->text = 'Чему равна плотность воды в КГ/м3 ?';
$q->points = 15;
$q->answer = 1000;
$q->deviation = 0;
$questions[] = $q;
return $questions;
}
$q = createQuestions();
$a = array('b', 'd', 'b', 299792, 3.14, 1000); $totalScore = 0;
for ($i = 0; $i < count($a); $i++) { $question = $q[$i];
$answer = $a[$i];
$q[$i]->printQuestion();
echo "Ваш ответ - {$answer}.\n";
$totalScore += $question->checkAnswer($answer);
}
echo "За игру вы заработали {$totalScore} очков";