Skip to content

Commit

Permalink
feat: implement game prime
Browse files Browse the repository at this point in the history
  • Loading branch information
kolotov committed Sep 3, 2023
1 parent aab2663 commit ab4f61c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ brain-gcd:
./bin/brain-gcd

brain-progression:
./bin/brain-progression
./bin/brain-progression

brain-prime:
./bin/brain-prime
9 changes: 9 additions & 0 deletions bin/brain-prime
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env php
<?php

use function BrainGames\Engine\runBrainGame;

require_once __DIR__ . '/../src/bootstrap.php';

$module = 'PrimeGame';
runBrainGame($module);
35 changes: 35 additions & 0 deletions src/Games/PrimeGame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace BrainGames\Games\EvenGame;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

return static function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'Answer "yes" if given number is prime. Otherwise answer "no".';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () {
$isPrime = function ($number) {
if ($number <= 1) {
return false;
}

for ($i = 2; $i * $i <= $number; $i++) {
if ($number % $i === 0) {
return false;
}
}

return true;
};


$question = random_int(1, 100);
$isPrime = $isPrime($question);
return [$question, $isPrime ? 'yes' : 'no'];
};

return $module;
};

0 comments on commit ab4f61c

Please sign in to comment.