-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc76c12
commit 5a34088
Showing
4 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |