Skip to content

Commit a483205

Browse files
committed
project check. ALL edition
1 parent 350c533 commit a483205

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

src/Games/Calc.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace BrainGames\Calc;
4+
5+
use function BrainGames\Engine\startGame;
6+
7+
//-- Игра - "Калькулятор"
8+
function initGameData($count = 3): void
9+
{
10+
$gameParams = [];
11+
$gameParams["rules"] = 'What is the result of the expression?';
12+
$operations = ["+", "-", "*"];
13+
for ($counterAnswers = 0; $counterAnswers < $count; $counterAnswers++) {
14+
$num = random_int(1, 100);
15+
$num2 = random_int(1, 100);
16+
$thisOperation = $operations[random_int(0, 2)];
17+
$gameParams["expectedAnswer"][] = calculate($num, $num2, $thisOperation);
18+
$gameParams["questions"][] = "{$num} {$thisOperation} {$num2}";
19+
}
20+
21+
startGame($gameParams);
22+
}
23+
24+
function calculate($x, $y, $operator)
25+
{
26+
$operations = [
27+
'+' => fn($x, $y) => $x + $y,
28+
'-' => fn($x, $y) => $x - $y,
29+
'*' => fn($x, $y) => $x * $y,
30+
];
31+
32+
return $operations[$operator]($x, $y);
33+
}

src/Games/Even.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace BrainGames\Even;
4+
5+
use function BrainGames\Engine\startGame;
6+
7+
//-- Игра - "Проверка на чётность"
8+
function initGameData(): void
9+
{
10+
$gameParams = [];
11+
$gameParams["rules"] = 'Answer "yes" if the number is even, otherwise answer "no".';
12+
$count = 3;
13+
for ($counterAnswers = 0; $counterAnswers < $count; $counterAnswers++) {
14+
$num = random_int(1, 100);
15+
$gameParams["expectedAnswer"][] = isEven($num) ? "yes" : "no";
16+
$gameParams["questions"][] = $num;
17+
}
18+
19+
startGame($gameParams);
20+
}
21+
//--- Функция проверки на чётность
22+
function isEven(int $num): bool
23+
{
24+
return $num % 2 === 0;
25+
}

src/Games/Gcd.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace BrainGames\Gcd;
4+
5+
use function BrainGames\Engine\startGame;
6+
7+
//-- Игра - Общий делитель -- "НОД"
8+
function initGameData(): void
9+
{
10+
$gameParams = [];
11+
$gameParams["rules"] = 'Find the greatest common divisor of given numbers.';
12+
$count = 3;
13+
for ($counterAnswers = 0; $counterAnswers < $count; $counterAnswers++) {
14+
$num = random_int(1, 100);
15+
$num2 = random_int(1, 100);
16+
$gameParams["expectedAnswer"][] = getGcd($num, $num2);
17+
$gameParams["questions"][] = "{$num} {$num2}";
18+
}
19+
20+
startGame($gameParams);
21+
}
22+
23+
//-- Проверка на общий делитель
24+
function getGcd(int $num1, int $num2): int
25+
{
26+
$a = $num1;
27+
$b = $num2;
28+
while ($b != 0) {
29+
$temp = $b;
30+
$b = $a % $b;
31+
$a = $temp;
32+
}
33+
return abs($a);
34+
}

src/Games/Prime.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace BrainGames\Prime;
4+
5+
use function BrainGames\Engine\startGame;
6+
7+
//-- Игра - "Простое ли число?"
8+
function initGameData(): void
9+
{
10+
$gameParams = [];
11+
$gameParams["rules"] = 'Answer "yes" if given number is prime. Otherwise answer "no".';
12+
$count = 3;
13+
for ($counterAnswers = 0; $counterAnswers < $count; $counterAnswers++) {
14+
$num = random_int(1, 100);
15+
$gameParams["expectedAnswer"][] = isPrime($num) ? 'yes' : 'no';
16+
$gameParams["questions"][] = $num;
17+
}
18+
19+
startGame($gameParams);
20+
}
21+
22+
//-- Проверка на простое число
23+
function isPrime(int $number): bool
24+
{
25+
if ($number <= 1) {
26+
return false;
27+
}
28+
29+
if ($number == 2) {
30+
return true;
31+
}
32+
33+
if ($number % 2 == 0) {
34+
return false;
35+
}
36+
37+
for ($i = 3; $i <= sqrt($number); $i += 2) {
38+
if ($number % $i == 0) {
39+
return false;
40+
}
41+
}
42+
43+
return true;
44+
}

src/Games/Progression.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace BrainGames\Progression;
4+
5+
use function BrainGames\Engine\startGame;
6+
7+
//-- Игра - "Арифметическая прогрессия"
8+
function initGameData(): void
9+
{
10+
$gameParams = [];
11+
$gameParams["rules"] = 'What number is missing in the progression?';
12+
$count = 3;
13+
for ($counterAnswers = 0; $counterAnswers < $count; $counterAnswers++) {
14+
$progression = generateProgression();
15+
$hiddenElement = hideElement($progression);
16+
$gameParams["expectedAnswer"][] = $hiddenElement["hidden_value"];
17+
$gameParams["questions"][] = implode(" ", $hiddenElement["progression"]);
18+
}
19+
20+
startGame($gameParams);
21+
}
22+
//-- Генерируем прогрессию
23+
function generateProgression(
24+
int $length = 10,
25+
int $minStep = 2,
26+
int $maxStep = 5,
27+
int $minStart = 1,
28+
int $maxStart = 20
29+
): array {
30+
$start = rand($minStart, $maxStart);
31+
$step = rand($minStep, $maxStep);
32+
33+
$progression = [];
34+
for ($i = 0; $i < $length; $i++) {
35+
$progression[] = $start + $i * $step;
36+
}
37+
38+
return $progression;
39+
}
40+
//-- Скрыть элемент
41+
function hideElement(array $progression): array
42+
{
43+
$hiddenIndex = rand(0, count($progression) - 1);
44+
$hiddenValue = $progression[$hiddenIndex];
45+
$progression[$hiddenIndex] = '..';
46+
47+
return [
48+
'progression' => $progression,
49+
'hidden_index' => $hiddenIndex,
50+
'hidden_value' => $hiddenValue
51+
];
52+
}

0 commit comments

Comments
 (0)