Skip to content

Commit f4ca10c

Browse files
committed
change answer to accept numeric shortcut instead of string answer
1 parent 0fa7014 commit f4ca10c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

game.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22

33
$results = [];
44

5+
function formatAnswers (&$answer, $key) {
6+
$answer = $key + 1 . ') ' . $answer . PHP_EOL;
7+
};
8+
59
function doChoice($situation) {
610

711
$question = key($situation);
8-
$options = $situation[$question];
9-
$validAnswers = array_keys($options);
12+
$situationChoices = $situation[$question];
13+
$potentialAnswers = array_keys($situationChoices);
14+
$validInputs = range(1, count($potentialAnswers));
15+
16+
array_walk($potentialAnswers, 'formatAnswers');
1017

1118
do {
12-
$answer = readline($question .' (' . implode(',', $validAnswers) . ')' . PHP_EOL);
13-
} while ( !in_array($answer, $validAnswers ));
19+
echo $question . PHP_EOL . implode($potentialAnswers) . '=> ';
20+
$answer = rtrim( fgets( STDIN ), "\n" );
21+
} while ( !in_array($answer, $validInputs));
1422

15-
$result = $options[$answer];
23+
$result = array_values($situationChoices)[$answer - 1];
1624

1725
if (!is_array($result)) {
1826
echo $result . PHP_EOL;
@@ -32,7 +40,7 @@ function doChoice($situation) {
3240
'climb' => [
3341
'you scale the castle walls. eventually you come to a window where you see a king sleeping. what will you do?' => [
3442
'enter' => [
35-
'the king hasn\t woken up. what will you do to him?' => [
43+
'the king hasn\'t woken up. what will you do to him?' => [
3644
'wake' => 'the king wakes up and roars angrily. the guards come and beat you to death with guitars',
3745
'kill' => 'as you drive your sword into the kings side his mouth opens and an evil spirit streams out. you\'ve killed the wicked king of Norrland and you are declared a hero'
3846
]

0 commit comments

Comments
 (0)