return true; } } class MatchMarker extends Marker { function mark($response) { return $this->test == $response; } } class RegexpMarker extends Marker { function mark($response) { return preg_match($this->test, $response); } } $n = "<br />"; $markers = [new RegexpMarker("/Пят./"), new MatchMarker("Пять"), new MarkLogicMarker('$input equals "Пять"')]; foreach ($markers as $marker) { print get_class($marker) . $n; $question = new TextQuestion("Сколько лучей у кремлевской звезды?", $marker); foreach (["Пять", "Четыре"] as $response) { print "Ответ: {$response}: "; if ($question->mark($response)) { print "Правильно!" . $n; } else { print "Неверно!" . $n; } } }
{ function mark($response) { return preg_match("{$this->test}", $response); } } $questions = array("how many beans make five?" => '$input equals "five" or $input equals "5"', "give a childs name?" => '$input equals "jake" or $input equals "holly"', "live begins at" => '$input equals "forty" or $input equals "40"'); foreach ($questions as $prompt => $mark) { print "{$prompt}\n"; $marker = new MarkLogicMarker($mark); $result = false; do { $fh = fopen('php://stdin', 'r'); $line = fgets($fh); $line = trim($line); $question = new TextQuestion($prompt, $marker); if ($result = $question->mark($line)) { print "well done!\n"; } else { print "wrong, try again\n"; } } while (!$result); } /* while ( $fh = fopen( 'php://stdin', 'r' ) ) { $line = fgets( $fh ); print $line; } $markers = array( new RegexpMarker( "/f.ve/" ), new MatchMarker( "five" ), new MarkLogicMarker( '$input equals "five"' )