Skip to content

Commit c0d01f1

Browse files
committed
first project / step ninth / added Prime game, added prime bin, returned Cli.php file
1 parent 3f1a5c0 commit c0d01f1

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ brain-gcd:
1616
brain-progression:
1717
bin/brain-progression
1818

19+
brain-prime:
20+
bin/brain-prime
21+
1922
validate:
2023
composer validate
2124

bin/brain-games

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
<?php
44

55
require_once __DIR__ . '/../vendor/autoload.php';
6-
require_once __DIR__ . '/../src/Engine.php';
6+
require_once __DIR__ . '/../src/Cli.php';
7+
8+
Src\Cli\RunGreeting();

bin/brain-prime

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env php
2+
3+
<?php
4+
5+
require_once __DIR__ . '/../vendor/autoload.php';
6+
require_once __DIR__ . '/../src/Engine.php';
7+
require_once __DIR__ . '/../src/Games/Prime.php';
8+
9+
runGame('Prime');

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"bin/brain-even",
1818
"bin/brain-calc",
1919
"bin/brain-gcd",
20-
"bin/brain-progression"
20+
"bin/brain-progression",
21+
"bin/brain-prime"
2122
],
2223

2324
"require-dev": {

src/Cli.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Src\Cli;
4+
5+
use function cli\line;
6+
use function cli\prompt;
7+
8+
function RunGreeting()
9+
{
10+
line('Welcome to the Brain Game!');
11+
$name = prompt('May I have your name?');
12+
line("Hello, %s!", $name);
13+
}

src/Games/Prime.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
function getRules(): string
4+
{
5+
return 'Answer "yes" if given number is prime. Otherwise answer "no".';
6+
}
7+
8+
function generateQuestion(): int
9+
{
10+
return random_int(1, 101);
11+
}
12+
13+
function getCorrectAnswer($question): string
14+
{
15+
$number = (int)$question;
16+
if ($number < 2) {
17+
return 'no';
18+
}
19+
20+
for ($i = 2; $i * $i <= $number; $i++) {
21+
if ($number % $i == 0) {
22+
return 'no';
23+
}
24+
}
25+
26+
return 'yes';
27+
}

0 commit comments

Comments
 (0)