Skip to content

Commit

Permalink
add brain-prime
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniillGolovin committed May 12, 2024
1 parent dc76c12 commit 5a34088
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ brain-gcd:
brain-progression:
./bin/brain-progression

brain-prime:
./bin/brain-prime

validate:
composer validate

Expand Down
14 changes: 14 additions & 0 deletions bin/brain-prime
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env php

<?php

$autoloadPath1 = __DIR__ . '/../../../autoload.php';
$autoloadPath2 = __DIR__ . '/../vendor/autoload.php';

if (file_exists($autoloadPath1)) {
require_once $autoloadPath1;
} else {
require_once $autoloadPath2;
}

Brain\Games\Prime\play();
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"bin/brain-even",
"bin/brain-calc",
"bin/brain-gcd",
"bin/brain-progression"
"bin/brain-progression",
"bin/brain-prime"
],
"description": "Brain game",
"type": "project",
Expand All @@ -23,7 +24,8 @@
"src/Games/Even.php",
"src/Games/Calc.php",
"src/Games/Gcd.php",
"src/Games/Progression.php"
"src/Games/Progression.php",
"src/Games/Prime.php"
]
},
"scripts": {
Expand Down
48 changes: 48 additions & 0 deletions src/Games/Prime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Brain\Games\Prime;

use function Brain\Engine\gameLogic;
use function cli\line;
use function cli\prompt;

const DESCRIPTION = 'Answer "yes" if given number is prime. Otherwise answer "no".';
const RANDOM_MIN = 2;
const RANDOM_MAX = 20;

function isPrime($number)
{
if ($number < 2) {
return false;
}
for ($i = 2; $i <= $number / 2; $i += 1) {
if ($number % $i == 0) {
return false;
}
}
return true;
}

function getAnswer($question)
{
$answer = isPrime($question) ? 'yes' : 'no';
return $answer;
}

function getRandomNumber()
{
return rand(RANDOM_MIN, RANDOM_MAX);
}

function play()
{
$generateGameData = function () {
$question = getRandomNumber();
$answer = getAnswer($question);

return [$question, $answer];
};

gameLogic(DESCRIPTION, $generateGameData);
return;
}

0 comments on commit 5a34088

Please sign in to comment.