Skip to content

Commit

Permalink
release 1.0.1: fix composer.json and src/Cli.php
Browse files Browse the repository at this point in the history
  • Loading branch information
inkovskiy committed Jul 11, 2017
1 parent 14e8cbc commit 1e02500
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "igor-i/brain-games",
"bin": ["bin/brain-games"],
"bin": [
"bin/brain-games",
"bin/brain-even"
],
"type": "project",
"authors": [
{
Expand Down
62 changes: 60 additions & 2 deletions src/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,67 @@

use function cli\line;

function run()
const MULT = 7;
const INCR = 7;
const MOD = 10;

function welcome()
{
line('Welcome to the Brain Game!');
}

function hello()
{
$name = \cli\prompt('May I have your name?');
line("Hello, %s!", $name);
line("Hello, %s!" . PHP_EOL, $name);

return $name;
}

function theEnd($name, $result = true)
{
if ($result) {
line('Congratulations, %s!', $name);
} else {
line('Let\'s try again, %s!', $name);
}
}

function brainEven()
{
welcome();
line('Answer "yes" if number even otherwise answer "no".' . PHP_EOL);
$name = hello();

$currRandom = MULT;
$result = true;
for ($i = 0; $i < 3; $i++) {
line("Question: %s", $currRandom);
$answer = \cli\prompt('Your answer');
if (isRightAnswer($currRandom, $answer)) {
line("Correct!");
$currRandom = getNextRandom($currRandom);
} else {
line("'%s' is wrong answer ;(. Correct answer was '%s'.", $answer, getCorrectAnswer($currRandom));
$result = false;
break;
}
}

theEnd($name, $result);
}

function getNextRandom($curr)
{
return ($curr * MULT + INCR) % MOD;
}

function getCorrectAnswer($num)
{
return ($num % 2) ? 'no' : 'yes';
}

function isRightAnswer($num, $answer)
{
return ($answer == getCorrectAnswer($num)) ? true : false;
}

0 comments on commit 1e02500

Please sign in to comment.