Skip to content

Commit 58e899d

Browse files
committed
changed all rand to random_int in Game files, add type of return in game functions
1 parent fc8efb9 commit 58e899d

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

src/Games/Calc.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
<?php
22

3-
function getRules()
3+
function getRules(): string
44
{
55
return 'What is the result of the expression?';
66
}
77

8-
function generateQuestion()
8+
function generateQuestion(): string
99
{
10-
$firstOperand = rand(1, 20);
11-
$secondOperand = rand(1, 20);
10+
$firstOperand = random_int(1, 20);
11+
$secondOperand = random_int(1, 20);
1212
$actions = ["+", "-", "*"];
13-
$operation = $actions[rand(0, 2)];
13+
$operation = $actions[random_int(0, 2)];
1414
return "$firstOperand $operation $secondOperand";
1515
}
1616

17-
function getCorrectAnswer($question)
17+
function getCorrectAnswer($question): int
1818
{
1919
list($firstOperand, $operation, $secondOperand) = explode(" ", $question);
20+
$firstOperand = (int)$firstOperand;
21+
$secondOperand = (int)$secondOperand;
2022
switch ($operation) {
2123
case "+":
2224
return $firstOperand + $secondOperand;

src/Games/Even.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22

3-
function getRules()
3+
function getRules(): string
44
{
55
return 'Answer "yes" if the number is even, otherwise answer "no".';
66
}
77

8-
function generateQuestion()
8+
function generateQuestion(): int
99
{
10-
return rand(1, 20);
10+
return random_int(1, 20);
1111
}
1212

13-
function getCorrectAnswer($question)
13+
function getCorrectAnswer($question): string
1414
{
1515
return $question % 2 ? 'no' : 'yes';
1616
}

src/Games/Gcd.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
<?php
22

3-
function getRules()
3+
function getRules(): string
44
{
55
return 'Find the greatest common divisor of given numbers.';
66
}
77

8-
function generateQuestion()
8+
function generateQuestion(): string
99
{
1010
if (rand(1, 3) === 1) {
11-
// 33% случаев - гарантированный нетривиальный НОД
12-
$base = rand(2, 25);
13-
$firstNumber = $base * rand(2, 10);
14-
$secondNumber = $base * rand(2, 10);
11+
$base = random_int(2, 25);
12+
$firstNumber = $base * random_int(2, 10);
13+
$secondNumber = $base * random_int(2, 10);
1514
} else {
16-
// 67% случаев - полностью случайные
17-
$firstNumber = rand(1, 100);
18-
$secondNumber = rand(1, 100);
15+
$firstNumber = random_int(1, 100);
16+
$secondNumber = random_int(1, 100);
1917
}
2018
return "$firstNumber $secondNumber";
2119
}
2220

23-
function getCorrectAnswer($question)
21+
function getCorrectAnswer($question): int
2422
{
2523
list($secondNumber, $firstNumber ) = explode(" ", $question);
2624

0 commit comments

Comments
 (0)